栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

org.apache.http.NoHttpResponseException故障排查

org.apache.http.NoHttpResponseException故障排查

文章目录

背景问题分析解决方法

加入重试机制 Reference

背景

最近公司的spark离线任务稳定有报错日志:

java.lang.RuntimeException: org.apache.http.NoHttpResponseException: demo.com:80 failed to respond

通过该报错日志我们可以判断是离线任务中使用的 httpclient 调用失败了,使用的组件版本如下:

httpclient-4.5.12httpcore-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     // 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 handlingget NoHttpResponseException for load testing
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/747611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号