SpringCloud中FeignClient自定義配置
前言
最近公司新老項目對接過程中使用feginClient進(jìn)行調(diào)用時遇到了很多問題,在此做些簡短的總結(jié)記錄
一、Feign的配置原理
當(dāng)我們配置一個feignClient的時候,通常的寫法是這樣的
@FeignClient("xxxClient")
public interface xxxClient{
}
當(dāng)我們做這個實際上就是生成一個默認(rèn)的FeignClient, 其配置在org.springframework.cloud.openfeign 包下的FeignClientsConfiguration
@Configuration
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Autowired(required = false)
private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();
@Autowired(required = false)
private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();
@Autowired(required = false)
private Logger logger;
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
}
@Bean
@ConditionalOnMissingBean
public Encoder feignEncoder() {
return new SpringEncoder(this.messageConverters);
}
@Bean
@ConditionalOnMissingBean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
}
@Bean
public FormattingConversionService feignConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
feignFormatterRegistrar.registerFormatters(conversionService);
}
return conversionService;
}
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled")
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
@Bean
@ConditionalOnMissingBean
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
public Feign.Builder feignBuilder(Retryer retryer) {
return Feign.builder().retryer(retryer);
}
@Bean
@ConditionalOnMissingBean(FeignLoggerFactory.class)
public FeignLoggerFactory feignLoggerFactory() {
return new DefaultFeignLoggerFactory(logger);
}
}
可以看到這上面所有注入的bean都有一個注解@ConditionalOnMissingBean,也就沒有自定義則觸發(fā)創(chuàng)建Bean
這一段Bean的注入在FeignClientFactoryBean中的這段代碼, 當(dāng)服務(wù)啟動時觸發(fā)
protected Feign.Builder feign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(type);
// @formatter:off 從spring上下文中獲取對應(yīng)的Bean
Feign.Builder builder = get(context, Feign.Builder.class)
// required values
.logger(logger)
.encoder(get(context, Encoder.class))
.decoder(get(context, Decoder.class))
.contract(get(context, Contract.class));
// @formatter:on
configureFeign(context, builder);
return builder;
}
二、自定義配置
這就意味著如果我們需要自定義FeignClient的相關(guān)配置可以直接注入其中一個bean就可以了
類似于
@Configuration
public class FeignClientsConfiguration {
@Bean
public Decoder feignDecoder() {
return new ResultDecoder();
}
@Bean
public Encoder feignEncoder() {
return new ParamEncoder();
}
@Bean
public Contract feignContract() {
return new DefaultContract();
}
}
如上所示, 我分別注入了Decoder、Encoder、Contract, 而其他幾項依然還是feign默認(rèn)值.
三、專有配置
那么如果我想為一個client單獨(dú)加一些配置又應(yīng)該如何做呢?
點開@FeignClient可以看到里面有一項configuration
/**
* A custom <code>@Configuration</code> for the feign client. Can contain override
* <code>@Bean</code> definition for the pieces that make up the client, for instance
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
*
* @see FeignClientsConfiguration for the defaults
*/
Class<?>[] configuration() default {};
所以我們可以寫一個如下的configuration
public class SelfFeignClientsConfiguration {
@Bean
@ConditionalOnMissingBean(name = "SelfFeignDecoder")
public Decoder SelfFeignDecoder() {
return new JacksonDecoder();
}
}
再在feignClient上引入
@FeignClient(name = "xxxClient", configuration = SelfFeignClientsConfiguration.class)
public interface xxxClient{
}
如此即可完成對xxxClient更細(xì)粒度的配置.
到此這篇關(guān)于SpringCloud中FeignClient自定義配置的文章就介紹到這了,更多相關(guān)SpringCloud FeignClient配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud中的@FeignClient注解使用詳解
- springcloud之FeignClient使用詳解
- SpringCloud @FeignClient注入Spring容器原理分析
- SpringCloud之@FeignClient()注解的使用詳解
- SpringCloud FeignClient 超時設(shè)置
- SpringCloud全面解析@FeignClient標(biāo)識接口的過程
- SpringCloud引入feign失敗或找不到@EnableFeignClients注解問題
- SpringCloud @FeignClient參數(shù)的用法解析
- SpringCloud之@FeignClient()注解的使用方式
相關(guān)文章
Spring Boot Web 靜態(tài)文件緩存處理的方法
本篇文章主要介紹了Spring Boot Web 靜態(tài)文件緩存處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
SpringBoot 集成 Jasypt 對數(shù)據(jù)庫加密以及踩坑的記錄分享
這篇文章主要介紹了SpringBoot 集成 Jasypt 對數(shù)據(jù)庫加密以及踩坑,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Java Spring @Autowired的這些騷操作,你都知道嗎
這篇文章主要介紹了徹底搞明白Spring中的自動裝配和Autowired注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
Java中finally和return的關(guān)系實例解析
這篇文章主要介紹了Java中finally和return的關(guān)系實例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
redis setIfAbsent和setnx的區(qū)別與使用說明
這篇文章主要介紹了redis setIfAbsent和setnx的區(qū)別與使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot調(diào)用支付寶第三方接口(沙箱環(huán)境)
這篇文章主要介紹了springboot+調(diào)用支付寶第三方接口(沙箱環(huán)境),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Linux中配置Java環(huán)境變量實現(xiàn)過程
文章介紹了在Linux系統(tǒng)上安裝Java 1.8的步驟:下載、傳輸、解壓至/opt目錄,配置環(huán)境變量(當(dāng)前用戶或全局),使用update-alternatives設(shè)置默認(rèn)版本,并通過命令驗證安裝是否成功2025-07-07
了解Maven的<relativePath/>標(biāo)簽用法
這篇文章主要介紹了了解Maven的<relativePath/>標(biāo)簽用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

