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;
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解决短链接的问题。
摘要: spring resttemplate使用httpclient4.4 连接池,出现org.apache.http.NoHttpResponseException: 10.0.0.25:8080 failed to respond异常的解决方案。以及httpclient4.4版本的连接池配置httpclient4.4简单初始化httpclient的方式:HttpClient
failed to respond 的原因是因为:Http 的 KeepAlive参数引起。
具体可以参考:https://blog.csdn.net/qq826654664jx/article/details/100864080
解决思路:http连接不允许复用;
解决代码:
package com.phkj.equipment.util;
import com.aliyun.oss.common.utils.HttpUtil;
import org.apache.http.Const...
HttpClient 默认使用长连接+连接池。
Http客户端无法及时响应服务端关闭连接,这次如果继续使用这条连接去请求服务端就会导致 org.apache.http.NoHttpResponseException。
虽然HttpClient采取了很多方式避免这种情况,例如请求前会校验超时时间、会读socket检测连接是否还在,但是边界情况总是无法避免(这一刻判断连接可用后,在请求发出去后,接
error @achrinza/node-ipc@9.2.2: The engine “node“ is incompatible with this module. Expected version
j-dict-select-tag 下拉框 change事件无效
AD Ldap pwdLastSet不能更新到指定日期