httpclient4.4简单初始化httpclient的方式:
HttpClient httpClient = HttpClientBuilder.create().build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);
使用连接池初始化httpclient的方式:
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(10);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(connManager);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
HttpClient httpClient = HttpClientBuilder.create().build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);
但在测试过程中发现,有时候出现如下的异常,但重试一次就没有问题:
Caused by: org.apache.http.NoHttpResponseException: 10.0.0.25:8080 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
网上查了相关的报错信息,找到官网上的描述
org.apache.commons.httpclient.NoHttpResponseException java.io.IOException +- org.apache.commons.httpclient.NoHttpResponseExceptionIn some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This may cause the server to drop the connection to the client without giving any response. HttpClient throws NoHttpResponseException when it encounters such a condition. In most cases it is safe to retry a method that failed with NoHttpResponseException.
根据描述主要就是因为服务端断开连接,但并没有通知客户端,导致下次请求该服务时httpclient继续使用该连接导致报错。
查看了tomcat7的文档,发现在Connector中keepAliveTimeout可以配置连接空闲多久就关闭该连接,它的值默认是和connectionTimeout一样,而在server.xml中connectionTimeout值默认为20s。
通过验证,确实是在第一次请求20秒后,再请求就会报出NoHttpResponseException的异常。
通过查看debug日志,以上代码生成的connection默认是keep alive的,通过如下代码可以将httpclient生成的connection的alive时间可配置。
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
return 20 * 1000; // tomcat默认keepAliveTimeout为20s
}
};
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(20, TimeUnit.SECONDS);
connManager.setMaxTotal(200);
connManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10 * 1000)
.setSocketTimeout(10 * 1000)
.setConnectionRequestTimeout(10 * 1000)
.build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(connManager);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
httpClientBuilder.setKeepAliveStrategy(connectionKeepAliveStrategy);
HttpClient httpClient = httpClientBuilder.build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate = new RestTemplate(requestFactory);
主要是增加keepalive的策略,但这又带来一个问题,所有的连接只有20秒,无法使用长连接的性能优势,在查看API时发现PoolingHttpClientConnectionManager提供有
@Override
public void closeIdleConnections(final long idleTimeout, final TimeUnit tunit) {
if (this.log.isDebugEnabled()) {
this.log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
}
this.pool.closeIdle(idleTimeout, tunit);
}
的方法,后续研究能否使用该API解决短链接的问题。