httpurlconnection 设置超时_restTemplate超时时间引发的生产事故

博客讲述了在一次服务故障中,由于RabbitMQ队列未正常ACK导致的消息堆积问题。经过排查,发现是由于RestTemplate在调用三方服务时未设置超时时间,使用了默认的SimpleClientHttpRequestFactory,这可能导致HTTP请求无限期挂起,进而阻塞队列。解决方案是为RestTemplate配置连接和读取超时时间。
摘要由CSDN通过智能技术生成

船长:

工作地:上海

上海非码网络架构师

1、现象

今天下午公司客户群里突然报警,说订单没有推送服务商,经排查发现是rabbitmq堵住了,查询elk和监控没有发现业务异常或超时日志。

通过rabbitmq后面发现一个队列有异常,队列不ack,未消费数量一直增长,怀疑服务假死,只能保留现场,重启服务,下面是服务重启前后队列截图

d6f8bebe8f17bbb1dc39604e0634e869.png

ab2c7a3c798881afdf632d3034820d90.png

2、分析

为什么服务重启后队列立马消费一空,证明三方服务商的接口没有问题,经过代码查找发现调用三方用的是restTemplate,核心代码如下:

@Servicepublic class OrderStatusChangePushService {    private static Logger logger = LoggerFactory.getLogger(OrderStatusChangePushService.class);    @Autowired    private RestTemplate restTemplate;    public ResponseEntity notify2Thirdparty(OrderInfoReqs orderInfo, String callbackUrl, AssortmentOpenApiEncryptDto encryptConfig, String operateType) {        PlatformBaseRequest request = getRequest(orderInfo, encryptConfig, operateType);        HttpHeaders headers = new HttpHeaders();        headers.setContentType(MediaType.APPLICATION_JSON);        HttpEntity platformBaseRequestHttpEntity = new HttpEntity<>(request, headers);        ResponseEntity exchange = null;        exchange = restTemplate.exchange(callbackUrl, HttpMethod.POST, platformBaseRequestHttpEntity, PlatformBaseResponse.class);         return exchange;     }}
@SpringBootApplicationpublic class PlatformOrderConsumerApplication extends SpringBootServletInitializer {    @Bean    RestTemplate restTemplate() {        return new RestTemplate();    }    public static void main(String[] args) {        SpringApplication.run(PlatformOrderConsumerApplication.class, args);    }}


发现restTemplate用的是直接new的,未重写连接池也未设置超时时间。看源码得知底层用的jdk的httpurlconnection,若readTimeOut和connectionTimeOut没有设置,那请求是没有超时时间的,导致请求一直hang住。

a3d6e01acc5e2ccc99351ae7b82b3a46.png

329737915ba96f4ddc84ca9aee3f382a.png

3、结论

restTemplate没有设置超时时间,导致单挑消息不能ack,hang住了整个队列(因为业务需求,一个队列只能单线程消费)

restTemplate如果没有重写连接池,默认用的SimpleClientHttpRequestFactory,SimpleClientHttpRequestFactory默认的readTimeOut=-1,connectionTimeOut=-1, 导致jdk的HttpUrlConnection是不会超时,进而hang死整个队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值