Spring Cloud feign使用okhttp3

指南

maven

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

配置文件

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

配置

@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了,不需要额外配置。

posted @ 2020-02-10 18:59  阿晨聊技术  阅读(2572)  评论(0编辑  收藏  举报