Spring Cloud feign使用okhttp3

spring cloud feign使用okhttp3

指南

maven

1
2
3
4
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>

配置文件

1
2
feign.httpclient.enabled=false
feign.okhttp.enabled=true

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {

@Autowired
OkHttpLoggingInterceptor okHttpLoggingInterceptor;

@Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool())
// .addInterceptor();
.build();
}
}

实践

不需要额外编写FeignOkHttpConfig,feign本身已经存在FeignOkHttpAutoConfiguration了,不需要额外配置。