在项目实际运行中,偶发异常:org.apache.http.NoHttpResponseException。
官网解释是:
In 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.
意思就是 当服务器端由于负载过大等情况发生时,可能会导致在收到请求后无法处理(比如没有足够的线程资源),会直接丢弃链接而不进行处理。此时客户端就回报错:NoHttpResponseException。
官方建议出现这种情况时,可以选择重试。但是重试一定要限制重试次数,避免雪崩。
修改方案:
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(config)
.setConnectionManager(getPoolManager())
//不使用这种方式,不方便看日志,使用下面自定义的retry
// .setRetryHandler(new DefaultHttpRequestRetryHandler(3,true))
.setRetryHandler((exception, executionCount, context) -> {
if (executionCount > 3) {
log.warn("Maximum tries reached for client http pool ");
return false;
if (exception instanceof NoHttpResponseException //NoHttpResponseException 重试
|| exception instanceof ConnectTimeoutException //连接超时重试
// || exception instanceof SocketTimeoutException //响应超时不重试,避免造成业务数据不一致
log.warn("NoHttpResponseException on " + executionCount + " call");
return true;
return false;
.build();
参考资料:
官方文档
httpclient4.4 出现NoHttpResponseException的异常解决
[get NoHttpResponseException for load testing
](https://stackoverflow.com/questions/10570672/get-nohttpresponseexception-for-load-testing/10680629#10680629)
httpclient版本:4.5.2在项目实际运行中,偶发异常:org.apache.http.NoHttpResponseException。官网解释是:In some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process
主要现象:
项目偶发出现org.apache.http.NoHttpResponseException: The target server failed to respond异常
定位原因:
查阅资料,此异常属于长连接keep-Alive的一种异常现象。当服务端某连接闲置超过keep-Alive超时时间后,服务端会关闭连接,进行四次挥手。如果此时,客户端再次拿此连接来访问服务端就会报NoHttpResponseExceptio
针对跟外部对接,使用httpclient进行请求可能会报NoHttpResponseException,原因有可能是当时target服务器负载过大,或者服务端连接空闲自动挂起,或者客户端的http请求机制跟服务端不一致(这次就是这个原因,客户端使用socket的空闲连接,服务器端是短连接)。
刚开始发现NoHttpResponseException,网上也查了一些资料,设置HttpClients...
HttpClient httpClient = HttpClientBuilder.create().build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTem...
当使用 HttpClient 进行网络请求时,可能会遇到超时异常。处理超时异常的方法有以下几种:
1. 设置超时时间:在创建 HttpClient 实例时,可以设置连接超时时间和读取超时时间,以确保请求在规定时间内得到响应。例如:
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000) // 连接超时时间为5秒
.setSocketTimeout(5000) // 读取超时时间为5秒
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(config)
.build();
2. 使用连接池:使用连接池可以提高 HttpClient 的性能和稳定性,同时也可以减少超时异常的发生。例如:
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(100); // 最大连接数为100
connManager.setDefaultMaxPerRoute(20); // 每个路由的最大连接数为20
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connManager)
.build();
3. 重试机制:在请求失败时,可以进行重试,以增加请求成功的概率。例如:
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 3) { // 最多重试3次
return false;
if (exception instanceof InterruptedIOException) { // 超时异常
return true;
if (exception instanceof UnknownHostException) { // 未知主机异常
return false;
if (exception instanceof ConnectTimeoutException) { // 连接超时异常
return true;
if (exception instanceof SSLException) { // SSL异常
return false;
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) { // 幂等请求
return true;
return false;
CloseableHttpClient httpClient = HttpClients.custom()
.setRetryHandler(retryHandler)
.build();
以上是处理 HttpClient 超时异常的几种方法,具体选择哪种方法,需要根据实际情况进行判断。
spring中 allowBeanDefinitionOverriding(spring.main.allow-bean-definition-overriding) 分析
xunkoo:
解决httpclient的NoHttpResponseException异常
weixin_56873659: