背景
最近公司的spark离线任务稳定有报错日志:
java.lang.RuntimeException: org.apache.http.NoHttpResponseException: demo.com:80 failed to respond
通过该报错日志我们可以判断是离线任务中使用的 httpclient 调用失败了,使用的组件版本如下:
- httpclient-4.5.12
- httpcore-4.4.13
问题分析
遇到问题首先是在对应的官网查找资料,而不是一头扎进各种博客论坛,这里引用官网的一段关于 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.
官网的描述很详细,即出现次异常的原因是,服务器负载过多,导致虽然服务器可以接受请求,但是没有足够多的工作线程来处理请求,因此直接断开了链接而不给客户端发送返回。出现这种情况后我们可以进行重试的处理。
解决方法
加入重试机制
可以通过如下代码实现自己的重试机制:
HttpRequestRetryHandler retryHandler = (exception, executionCount, context) -> {
if (executionCount > 5) {
System.out.println("Maximum tries reached for client http pool ");
return false;
}
if (exception instanceof NoHttpResponseException
|| exception instanceof ConnectTimeoutException
|| exception instanceof SocketTimeoutException
) {
System.out.println("http开始重试 " + executionCount + " call");
return true;
}
return false;
};
httpClient = HttpClients.custom()
.setRetryHandler(retryHandler)
.build();
Reference
- 记HttpClient的NoHttpResponse问题
- 解决httpclient的NoHttpResponseException异常
- Exception handling
- get NoHttpResponseException for load testing
|