当前位置: 首页>后端>正文

feign client三个客户端的自动装配

feign client客户端的自动装配
我们在之前的文章中说了Feign的客户端有httpclient,OkHttpClient,Client.Default,下面就这三个客户端的自动装配进行分析一下。

FeignRibbonClientAutoConfiguration
FeignRibbonClientAutoConfiguration这个类配置FeignClient,它具有负载均衡能力

@ConditionalOnClass({ ILoadBalancer.class, Feign.class })
@Configuration
@AutoConfigureBefore(FeignAutoConfiguration.class)
@EnableConfigurationProperties({ FeignHttpClientProperties.class })
//Order is important here, last should be the default, first should be optional
// see https://github.com/spring-cloud/spring-cloud-netflix/issues/2086#issuecomment-316281653
@Import({ HttpClientFeignLoadBalancedConfiguration.class,
OkHttpFeignLoadBalancedConfiguration.class,
DefaultFeignLoadBalancedConfiguration.class })
public class FeignRibbonClientAutoConfiguration {

@Bean
@Primary
@ConditionalOnMissingBean
@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
public CachingSpringLoadBalancerFactory cachingLBClientFactory(
SpringClientFactory factory) {
return new CachingSpringLoadBalancerFactory(factory);
}

@Bean
@Primary
@ConditionalOnMissingBean
@ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate")
public CachingSpringLoadBalancerFactory retryabeCachingLBClientFactory(
SpringClientFactory factory,
LoadBalancedRetryFactory retryFactory) {
return new CachingSpringLoadBalancerFactory(factory, retryFactory);
}

@Bean
@ConditionalOnMissingBean
public Request.Options feignRequestOptions() {
return LoadBalancerFeignClient.DEFAULT_OPTIONS;
}
}
当ILoadBalancer类和Feign类存在的时候,FeignRibbonClientAutoConfiguration可以自动配置,同时导入HttpClientFeignLoadBalancedConfiguration、OkHttpFeignLoadBalancedConfiguration、DefaultFeignLoadBalancedConfiguration三个客户端的负载均衡配置类

HttpClientFeignLoadBalancedConfiguration
从HttpClientFeignLoadBalancedConfiguration的类上的注解我们可以看到

@Configuration
@ConditionalOnClass(ApacheHttpClient.class)
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
class HttpClientFeignLoadBalancedConfiguration {
}
存在ApacheHttpClient类和feign.httpclient.enabled=true可以实现httpclient的自动装配

DefaultFeignLoadBalancedConfiguration
@Configuration
class DefaultFeignLoadBalancedConfiguration {
@Bean
@ConditionalOnMissingBean
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
SpringClientFactory clientFactory) {
return new LoadBalancerFeignClient(new Client.Default(null, null),
cachingFactory, clientFactory);
}
}
从源码中我们可以看到默认自动装配是这个,从Client.Default看出实例使用HttpURLConnection来发起远程调用请求。

OkHttpFeignLoadBalancedConfiguration
@Configuration
@ConditionalOnClass(OkHttpClient.class)
@ConditionalOnProperty(value = "feign.okhttp.enabled")
class OkHttpFeignLoadBalancedConfiguration {
}
通过OkHttpFeignLoadBalancedConfiguration类上的注解,可以看到需要满足的条件的是存在OkHttpClient类,并且feign.okhttp.enabled=true,这样自动装配才会生效,实例内部通过OKHttp3完成http请求。

总结
本篇文章主要对Feign的三个客户端的自动配置类进行了源码层面的分析,对应的类的自动装配类分别是OkHttpFeignLoadBalancedConfiguration、DefaultFeignLoadBalancedConfiguration、HttpClientFeignLoadBalancedConfiguration,根据自动装配类的类上的注解,我们可以看出要想让哪个自动装配类生效,需要我们配置的条件是什么,以及需要引入的包有哪些,除了这个三个客户端外还有一个客户端是LoadBalancerFeignClient,它的内部是通过ribbon的负载均衡调度各个客户端完成Http请求的发送,而不是真正的客户端类。


https://www.xamrdz.com/backend/3tu1938420.html

相关文章: