欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud中FeignClient自定義配置

 更新時(shí)間:2025年08月15日 11:19:05   作者:一樂(lè)小哥  
使用FeignClient時(shí)需了解其默認(rèn)配置機(jī)制,通過(guò)注入Decoder、Encoder等Bean實(shí)現(xiàn)自定義配置,下面就來(lái)介紹一下如何使用,感興趣的可以了解一下

前言

最近公司新老項(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式

    Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式

    這篇文章主要介紹了Spring Security如何優(yōu)雅的增加OAuth2協(xié)議授權(quán)模式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java -jar設(shè)置添加啟動(dòng)參數(shù)實(shí)現(xiàn)方法

    java -jar設(shè)置添加啟動(dòng)參數(shù)實(shí)現(xiàn)方法

    這篇文章主要介紹了java -jar設(shè)置添加啟動(dòng)參數(shù)實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 關(guān)于jdk9、jdk10、jdk11、jdk12、jdk13新特性說(shuō)明

    關(guān)于jdk9、jdk10、jdk11、jdk12、jdk13新特性說(shuō)明

    這篇文章主要介紹了關(guān)于jdk9、jdk10、jdk11、jdk12、jdk13新特性說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java8 ArrayList之forEach的使用

    Java8 ArrayList之forEach的使用

    這篇文章主要介紹了Java8 ArrayList之forEach的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot彩色日志配置方式

    SpringBoot彩色日志配置方式

    這篇文章主要介紹了SpringBoot彩色日志配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • JDK8安裝與配置實(shí)踐超詳細(xì)指南

    JDK8安裝與配置實(shí)踐超詳細(xì)指南

    本文詳細(xì)介紹了在Windows?64位系統(tǒng)上安裝和配置JDK8的步驟,包括JDK8下載、環(huán)境變量設(shè)置及安裝驗(yàn)證,同時(shí)提供了JDK8新特性如Lambda表達(dá)式、StreamAPI等的概覽,旨在幫助Java開(kāi)發(fā)者有效利用JDK8新特性進(jìn)行開(kāi)發(fā),需要的朋友可以參考下
    2024-10-10
  • Java實(shí)現(xiàn)注冊(cè)郵箱激活賬戶(hù)實(shí)例代碼

    Java實(shí)現(xiàn)注冊(cè)郵箱激活賬戶(hù)實(shí)例代碼

    本篇文章主要介紹了Java實(shí)現(xiàn)郵箱激活賬戶(hù)實(shí)例代碼,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,有需要的小伙伴可以參考下。
    2017-07-07
  • Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口

    Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口

    這篇文章主要介紹了Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 解決Intellij IDEA覆蓋tomcat配置的問(wèn)題

    解決Intellij IDEA覆蓋tomcat配置的問(wèn)題

    分析并解決Intellij IDEA覆蓋tomcat配置的問(wèn)題/解決修改server.xml無(wú)效的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧
    2021-02-02
  • Java遞歸遍歷樹(shù)形結(jié)構(gòu)的實(shí)現(xiàn)代碼

    Java遞歸遍歷樹(shù)形結(jié)構(gòu)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java遞歸遍歷樹(shù)形結(jié)構(gòu)的相關(guān)資料,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2016-03-03

最新評(píng)論