一般情况下,Feign
和Ribbon
我们都是结合使用的。Feign
是网络请求客户端,Ribbon
用于负载均衡,feign底层实现了ribbon
- //RibbonClientConfiguration.java
- @Bean
- @ConditionalOnMissingBean
- public IClientConfig ribbonClientConfig() {
- DefaultClientConfigImpl config = new DefaultClientConfigImpl();
- config.set(CommonClientConfigKey.ConnectTimeout, 1000);
- config.set(CommonClientConfigKey.ReadTimeout, 1000);
- return config;
- }
全局配置:
- ribbon.ConnectTimeout=1000
- ribbon.ReadTimeout=3000
单个配置:
- feignClientName.ribbon.ConnectTimeout=1000
- feignClientName.ribbon.ReadTimeout=3000
新增配置类:
- @Configuration
- public class CustomConfiguration {
- @Bean
- public Request.Options feignRequestOptions() {
- return new Request.Options(connectTimeoutMillis, readTimeoutMillis);
- }
- }
在被FeignClient
注解的类中添加配置:
- @FeignClient(name = "user-api", configuration = CustomConfiguration.class)
- public interface TestFeign {
- @GetMapping(value = "/getName")
- @ApiOperation(value = "测试数据", httpMethod = "GET")
- String getName();
- }
其中,代码注解配置比文件配置方式优先级更高。
Feign
默认是不会进行重试的!
- //FeignClientsConfiguration.java
- @Bean
- @ConditionalOnMissingBean
- public Retryer feignRetryer() {
- return Retryer.NEVER_RETRY;
- }
自定义重试策略
- @Bean
- public Retryer feignRetryer() {
- return new Retryer.Default(period, maxPeriod, maxAttempts);
- }
同时Feign的HTTP客户端支持三种框架:
HttpURLConnection、HttpClient、OkHttp;Feign
使用的默认客户端是HttpURLConnection
,且没有SSL
认证:
- //feign.Client.java
- public interface Client{
- public static class Default implements Client {
- HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
- final HttpURLConnection connection =
- (HttpURLConnection) new URL(request.url()).openConnection();
- ......
- }
- }
- }
Apache
的HttpClient
新增依赖:这里的版本号,要跟io.github.openfeign:feign-core
的版本,保持一致。
-
io.github.openfeign -
feign-httpclient -
10.1.0
feign.compression.response.enabled=true
这里设置为true之后,spring容器就会去加载自定义的httpClient
:
- //HttpClientFeignLoadBalancedConfiguration.java
- //若为true,就会来加载这里
- @Bean
- @ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "true")
- public CloseableHttpClient customHttpClient(HttpClientConnectionManager httpClientConnectionManager,
- FeignHttpClientProperties httpClientProperties) {
- HttpClientBuilder builder = HttpClientBuilder.create().disableCookieManagement().useSystemProperties();
- this.httpClient = createClient(builder, httpClientConnectionManager, httpClientProperties);
- return this.httpClient;
- }
- //若为false,加载默认的HttpURLConnection
- @Bean
- @ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "false", matchIfMissing = true)
- public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, HttpClientConnectionManager httpClientConnectionManager,
- FeignHttpClientProperties httpClientProperties) {
- this.httpClient = createClient(httpClientFactory.createBuilder(), httpClientConnectionManager, httpClientProperties);
- return this.httpClient;
- }
OkHttp
新增依赖:
这里的版本号,要跟io.github.openfeign:feign-core
的版本,保持一致。
-
io.github.openfeign -
feign-okhttp -
10.1.0
新增配置:
- feign.httpclient.enabled=false
- feign.okhttp.enabled=true