HttpClient连接及其连接池配置
- 整个线程池中最大连接数 MAX_CONNECTION_TOTAL = 800
- 路由到某台主机最大并发数,是MAX_CONNECTION_TOTAL(整个线程池中最大连接数)的一个细分 ROUTE_MAX_COUNT = 500
- 重试次数,防止失败情况 RETRY_COUNT = 3
- 客户端和服务器建立连接的超时时间 CONNECTION_TIME_OUT = 5000
- 客户端从服务器读取数据的超时时间 READ_TIME_OUT = 7000
- 从连接池中获取连接的超时时间 CONNECTION_REQUEST_TIME_OUT = 5000
- 连接空闲超时,清楚闲置的连接 CONNECTION_IDLE_TIME_OUT = 5000
- 连接保持存活时间 DEFAULT_KEEP_ALIVE_TIME_MILLIS = 20 * 1000
MaxtTotal DefaultMaxPerRoute区别
MaxtTotal是整个池子的大小;
DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分;
比如:MaxtTotal=400,DefaultMaxPerRoute=200,而我只连接到http://hjzgg.com时,到这个主机的并发最多只有200;而不是400;而我连接到http://qyxjj.com 和 http://httls.com时,到每个主机的并发最多只有200;即加起来是400(但不能超过400)。所以起作用的设置是DefaultMaxPerRoute。
从连接池获取连接的流程
Spring 使用
创建ClientHttpRequestFactory
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
httpClientBuilder.setSSLContext(sslContext)
.setMaxConnTotal(MAX_CONNECTION_TOTAL)
.setMaxConnPerRoute(ROUTE_MAX_COUNT)
.evictIdleConnections(CONNECTION_IDLE_TIME_OUT, TimeUnit.MILLISECONDS);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT, true));
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
CloseableHttpClient client = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(client);
clientHttpRequestFactory.setConnectTimeout(CONNECTION_TIME_OUT);
clientHttpRequestFactory.setReadTimeout(READ_TIME_OUT);
clientHttpRequestFactory.setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT);
clientHttpRequestFactory.setBufferRequestBody(false);
return clientHttpRequestFactory;
}
创建RestTemplate
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
// 修改StringHttpMessageConverter内容转换器
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
httpclient的连接池会占用tomcat的连接数,所以值不要太大