默认下OpenFeign使用URLConnection 请求连接,每次都需要创建、销毁连接
1、添加ApacheHttpClient依赖
<!-- 使用Apache HttpClient替换Feign原生httpclient--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId></dependency>
2、openFeign连接池要求
org.springframework.cloud.openfeign.FeignAutoConfiguration.HttpClientFeignConfiguration这个类,代码如下:
要求如下:
@ConditionalOnClass(ApacheHttpClient.class),必须要有ApacheHttpClient这个类才会生效,
并且feign.httpclient.enabled这个配置要设置为true。
3、配置文件开启
feign:httpclient:# 开启 Http Clientenabled: truemax-connections: 200 # httpclient处理的最大连接数量max-connections-per-route: 50 # 单个路径连接的最大数量connection-timeout: 2000 # 超时等待