Python 官方文档:入门教程 => 点击学习
目录排查:Java Config配置配置文件指定RestTemplate 设置超时时间注意点总结RestTemplate未设置超时时间,导致RabbitMQ队列大量堆积,消费者假死,
RestTemplate未设置超时时间,导致RabbitMQ队列大量堆积,消费者假死,不进行消费,类似线程堵塞。
从日志排查问题,在从进入消费者到结束期间,会调用业务服务,然而通过开始时间至结束时间,花费的时间竟然高达100多秒,查看业务服务未发现异常。出现这种问题的原因也有可能在消费期间网络波动导致Http连接出现问题,一直处于等待状态。由此判断可能是HTTP超时时间未设置导致的问题。
本文生产环境SpringBoot版本 2.1.5.RELEASE,采用的是Java config配置
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate getRestTemplate() {
//配置HTTP超时时间
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(6000);
httpRequestFactory.setConnectTimeout(6000);
httpRequestFactory.setReadTimeout(6000);
return new RestTemplate(httpRequestFactory);
}
}
custom.rest.connection.connection-request-timeout=3000
custom.rest.connection.connect-timeout=3000
custom.rest.connection.read-timeout=3000
@Configuration
public class AppConfig{
@Bean
@ConfigurationProperties(prefix = "custom.rest.connection")
public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory();
}
@Bean
public RestTemplate customRestTemplate(){
return new RestTemplate(customHttpRequestFactory());
}
}
重新部署后消费者迅速开始消费,堆积的消息已经消费完。
1、保证系统中只有一个RestTemplate的配置;不然可能与你的预期不一致。
2、永远不要太相信自己写的代码;多打印日志才能真正知道调用时间;
long s = System.currentTimeMillis();
try {
responseEntity = restTemplate.exchange();
} catch (Exception e) {
long costTime = System.currentTimeMillis()-s;
log.error("调用**服务异常,花费时间:{},错误:{}",costTime, e.getMessage(), e);
}
到此这篇关于springboot RestTemplate设置超时时间的文章就介绍到这了,更多相关Springboot RestTemplate设置超时时间内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Springboot RestTemplate设置超时时间的简单方法
本文链接: https://www.lsjlt.com/news/163100.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0