Spring Cloud Feign 使用方法与性能优化
1. feign自定义Configuration和root 容器有效隔离。
- 用@Configuration注解
- 不能在主@ComponentScan (or @SpringBootApplication)范围内,从其包名上分离
- 注意避免包扫描重叠,最好的方法是明确的指定包名
2. Spring Cloud Netflix 提供了默认的Bean类型:
- Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
- Encoder feignEncoder: SpringEncoder
- Logger feignLogger: Slf4jLogger
- Contract feignContract: SpringMvcContract
- Feign.Builder feignBuilder: HystrixFeign.Builder
3. Spring Cloud Netflix没有提供默认值,但仍然可以在feign上下文配置中创建:
- Logger.Level
- Retryer
- ErrorDecoder
- Request.Options
- Collection
4. 自定义feign的消息编码解码器:
不要在如下代码中getObject方法内new 对象,外部会频繁调用getObject方法。
1 |
ObjectFactory<HttpMessageConverters> messageConvertersObjectFactory = new ObjectFactory<HttpMessageConverters>() { |
5. 注意测试环境和生产环境,注意正确使用feign日志级别。
6. apacheHttpclient或者其他client的正确配置:
- apacheHttpclient自定义配置放在spring root context,不要在FeignContext,否则不会起作用。
- apacheHttpclient 连接池配置合理地连接和其他参数
7. Feign配置
1 |
#Hystrix支持,如果为true,hystrix库必须在classpath中 #请求和响应GZIP压缩支持 # 日志支持 |
8. Logger.Level支持
必须为每一个Feign Client配置来告诉Feign如何输出日志,可选:
- NONE, No logging (DEFAULT).
- BASIC, Log only the request method and URL and the response status code and execution time.
- HEADERS, Log the basic information along with request and response headers.
- FULL, Log the headers, body, and metadata for both requests and responses.
9. FeignClient.fallback 正确的使用方法
配置的fallback class也必须在FeignClient Configuration中实例化,否则会报java.lang.IllegalStateException: No fallback instance of type class
异常。
例子:
1 |
@FeignClient(name = "hello", fallback = HystrixClientFallback.class) public class HystrixClientFallback implements HystrixClient { @Configuration @Bean } |
10. 使用Feign Client 和@RequestMapping时,注意事项
当前工程中有和Feign Client中一样的Endpoint时,Feign Client的类上不能用@RequestMapping注解否则,当前工程该endpoint http请求且使用accpet时会报404.
下面的例子:
有一个 Controller
1 |
@RestController @PostMapping("balance") |
有一个Feign Client
1 |
@FeignClient( @RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) } |
if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :
如果 @RequestMapping注解被用在FeignClient类上,当像如下代码请求/v1/card/balance时,注意有Accept header:
1 |
Content-Type: application/json POST http://localhost:7913/v1/card/balance |
那么会返回 404。
如果不包含Accept header时请求,则是OK:
1 |
Content-Type:application/json |
或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:
1 |
@FeignClient( public interface CardFeignClient { @RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) } |
Spring Cloud Feign 使用方法与性能优化的更多相关文章
- 笔记:Spring Cloud Feign 其他配置
请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...
- 使用Spring Cloud Feign
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就 ...
- Spring cloud Feign 深度学习与应用
简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...
- 笔记:Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- 笔记:Spring Cloud Feign Hystrix 配置
在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...
- 笔记:Spring Cloud Feign 声明式服务调用
在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...
- 第六章:声明式服务调用:Spring Cloud Feign
Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...
- Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- Spring Cloud feign
Spring Cloud feign使用 前言 环境准备 应用模块 应用程序 应用启动 feign特性 综上 1. 前言 我们在前一篇文章中讲了一些我使用过的一些http的框架 服务间通信之Http框 ...
随机推荐
- Kali学习笔记29:默认安装漏洞
文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 默认安装漏洞: 早期Windows默认自动开启很多服务 ...
- shell 中let无法使用的原因
运行 sh let.sh 时,却显示 let: not found 百度之后知道: /bin/sh指向了dash而不是bash,dash不支持let命令. 解决方法: 法1.使用 bash ...
- [EXP]Microsoft Windows 10 (Build 17134) - Local Privilege Escalation (UAC Bypass)
#include "stdafx.h" #include <Windows.h> #include "resource.h" void DropRe ...
- Kibana简单使用教程
ELK平台日志查看教程 1. 访问地址:http://xxx:5601/app/kibana 我们主要使用的是右边Discover功能,默认显示的是183tpp(可设置)最近15分钟日志信息. 2. ...
- 远程计算机或设备将不接受连接(电脑能连接网络、QQ能登陆、浏览器无法使用)
第一种方法 1.win+r 2.输入regedit,打开注册表 3.查找Internet Settings(在HKEY_CURRENT_USER\Software\Microsoft\Windows\ ...
- mysql 开发基础系列5 运算符
1. 算术运算符 2.比较运算符 注意NULL值不能用= 运算符 SELECT * FROM t WHERE c =NULL; -- 需要改成 SELECT * FROM t WHERE c IS N ...
- Android内存管理篇 - adj的概念与进程adj级别控制
本文主要介绍Android的lowmemorykiller的oom_adj的相关概念,以及根据一些案例来阐述了解oom_adj对于做Android应用开发的重要意义. 一.lowmeorykiller ...
- Chapter 4 Invitations——7
The next day, I was surprised that Jessica wasn't her usual gushing self in Trig and Spanish. 第二天,我很 ...
- 谈谈 JAVA 的对象序列化
所谓的『JAVA 对象序列化』就是指,将一个 JAVA 对象所描述的所有内容以文件 IO 的方式写入二进制文件的一个过程.关于序列化,主要涉及两个流,ObjectInputStream 和 Objec ...
- 初识MongoBD
一.安装 我使用的系统是Ubuntu16.04,不同版本系统参照官网安装步骤.安装官网4个步骤安装好MongoDB并启动. sudo apt-key adv --keyserver hkp://key ...