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中 # 日志支持  | 
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  | 
 
 public class HystrixClientFallback implements HystrixClient { 
  | 
10. 使用Feign Client 和@RequestMapping时,注意事项
当前工程中有和Feign Client中一样的Endpoint时,Feign Client的类上不能用@RequestMapping注解否则,当前工程该endpoint http请求且使用accpet时会报404.
下面的例子:
有一个 Controller
| 
 1  | 
 
 
  | 
有一个Feign Client
| 
 1  | 
 
 
 }  | 
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  | 
 
 public interface CardFeignClient { 
 }  |