OpenFeign切换OkHttp作为HttpClient

152

引入依赖

<!-- 不需要设置版本号,由springcloud统一管理 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

okhttp 配置

@Slf4j
@Configuration
@ConditionalOnProperty(name = "feign.okhttp.enabled", havingValue = "true")
public class OkHttpClientConfig {

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                // 链接超时时间
                .connectTimeout(5, TimeUnit.SECONDS)
                // 读取超时时间
                .readTimeout(5,TimeUnit.SECONDS)
                // 是否重试
                .retryOnConnectionFailure(true)
                // 设置连接池. 可以设置连接池的连接数、存活时间等参数
                .connectionPool(new ConnectionPool())
                .build();
    }

}

配置文件用启用okhttp 关闭httpclient

feign:
  httpclient:
    enabled: false
  okhttp:
    enabled: true