SpringCloud中FeignClient自定義配置
前言
最近公司新老項(xiàng)目對(duì)接過(guò)程中使用feginClient進(jìn)行調(diào)用時(shí)遇到了很多問(wèn)題,在此做些簡(jiǎn)短的總結(jié)記錄
一、Feign的配置原理
當(dāng)我們配置一個(gè)feignClient的時(shí)候,通常的寫(xiě)法是這樣的
@FeignClient("xxxClient")
public interface xxxClient{
}
當(dāng)我們做這個(gè)實(shí)際上就是生成一個(gè)默認(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都有一個(gè)注解@ConditionalOnMissingBean,也就沒(méi)有自定義則觸發(fā)創(chuàng)建Bean
這一段Bean的注入在FeignClientFactoryBean中的這段代碼, 當(dāng)服務(wù)啟動(dòng)時(shí)觸發(fā)
protected Feign.Builder feign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(type);
// @formatter:off 從spring上下文中獲取對(duì)應(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)配置可以直接注入其中一個(gè)bean就可以了
類(lèi)似于
@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, 而其他幾項(xiàng)依然還是feign默認(rèn)值.
三、專(zhuān)有配置
那么如果我想為一個(gè)client單獨(dú)加一些配置又應(yīng)該如何做呢?
點(diǎn)開(kāi)@FeignClient可以看到里面有一項(xiàng)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 {};
所以我們可以寫(xiě)一個(gè)如下的configuration
public class SelfFeignClientsConfiguration {
@Bean
@ConditionalOnMissingBean(name = "SelfFeignDecoder")
public Decoder SelfFeignDecoder() {
return new JacksonDecoder();
}
}
再在feignClient上引入
@FeignClient(name = "xxxClient", configuration = SelfFeignClientsConfiguration.class)
public interface xxxClient{
}
如此即可完成對(duì)xxxClient更細(xì)粒度的配置.
到此這篇關(guān)于SpringCloud中FeignClient自定義配置的文章就介紹到這了,更多相關(guān)SpringCloud FeignClient配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud中的@FeignClient注解使用詳解
- springcloud之FeignClient使用詳解
- SpringCloud @FeignClient注入Spring容器原理分析
- SpringCloud之@FeignClient()注解的使用詳解
- SpringCloud FeignClient 超時(shí)設(shè)置
- SpringCloud全面解析@FeignClient標(biāo)識(shí)接口的過(guò)程
- SpringCloud引入feign失敗或找不到@EnableFeignClients注解問(wèn)題
- SpringCloud @FeignClient參數(shù)的用法解析
- SpringCloud之@FeignClient()注解的使用方式
相關(guān)文章
Spring Boot Web 靜態(tài)文件緩存處理的方法
本篇文章主要介紹了Spring Boot Web 靜態(tài)文件緩存處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Spring?JPA聯(lián)表查詢(xún)之注解屬性詳解
這篇文章主要為大家介紹了Spring?JPA聯(lián)表查詢(xún)之注解屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
SpringBoot 集成 Jasypt 對(duì)數(shù)據(jù)庫(kù)加密以及踩坑的記錄分享
這篇文章主要介紹了SpringBoot 集成 Jasypt 對(duì)數(shù)據(jù)庫(kù)加密以及踩坑,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Java對(duì)象不使用時(shí)賦值null的意義詳解
這篇文章主要介紹了java對(duì)象不再使用時(shí)賦值null的意義,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java Spring @Autowired的這些騷操作,你都知道嗎
這篇文章主要介紹了徹底搞明白Spring中的自動(dòng)裝配和Autowired注解的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-09-09
Java中finally和return的關(guān)系實(shí)例解析
這篇文章主要介紹了Java中finally和return的關(guān)系實(shí)例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
redis setIfAbsent和setnx的區(qū)別與使用說(shuō)明
這篇文章主要介紹了redis setIfAbsent和setnx的區(qū)別與使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot調(diào)用支付寶第三方接口(沙箱環(huán)境)
這篇文章主要介紹了springboot+調(diào)用支付寶第三方接口(沙箱環(huán)境),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Linux中配置Java環(huán)境變量實(shí)現(xiàn)過(guò)程
文章介紹了在Linux系統(tǒng)上安裝Java 1.8的步驟:下載、傳輸、解壓至/opt目錄,配置環(huán)境變量(當(dāng)前用戶(hù)或全局),使用update-alternatives設(shè)置默認(rèn)版本,并通過(guò)命令驗(yàn)證安裝是否成功2025-07-07
了解Maven的<relativePath/>標(biāo)簽用法
這篇文章主要介紹了了解Maven的<relativePath/>標(biāo)簽用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

