Spring Cloud feign使用okhttp3

ITLearner發表於2020-02-10

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

相關文章