自定义Feign的配置
Feign的默认配置类是FeignClientsConfiguration,其内部定义了Feign默认使用的编码器、解码器、契约、重试机制等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @Bean @ConditionalOnMissingBean public Decoder feignDecoder() { return new OptionalDecoder( new ResponseEntityDecoder(new SpringDecoder(this.messageConverters))); }
@Bean @ConditionalOnMissingBean @ConditionalOnMissingClass("org.springframework.data.domain.Pageable") public Encoder feignEncoder() { return new SpringEncoder(this.messageConverters); }
@Bean @ConditionalOnMissingBean public Contract feignContract(ConversionService feignConversionService) { return new SpringMvcContract(this.parameterProcessors, feignConversionService); }
@Bean @ConditionalOnMissingBean public Retryer feignRetryer() { return Retryer.NEVER_RETRY; }
|
但是有时候默认的并不满足全部的场景,我们需要自定义来进行配置,可以通过@FeignClient注解中的configuration属性来配置自定义的feign配置类,其优先级高于FeignClientsConfiguration
自定义Feign
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Configuration public class FeignConfiguration {
@Bean public Contract feignContract() { log.debug("原生feign [bean注册]"); return new feign.Contract.Default(); }
@Bean public FallBackImpl fallBackImpl() { return new FallBackImpl(); } }
|
如果该配置类为该项目下所有feign共用的,那么该类需要在@componentScan扫描下,这样就可以覆盖原本的默认配置,如果不希望该配置共用,只是个别feign使用,则该类不可以在@componentScan扫描下,而是要在所需要的feign中单独进行配置@FeignClient(configuration=FeignConfiguration.class)