随笔 - 547  文章 - 1  评论 - 293  阅读 - 33万

xxlJob排查 卡住了 - Feign或RestTemplate接口请求未设置超时时间导致

xxlJob排查 卡住了 - Feign或RestTemplate接口请求未设置超时时间导致

CloseableHttpClient 连接超时导致XxlJob调度阻塞,影响调度任务的执行
https://www.cnblogs.com/oktokeep/p/18205283

##修改配置:
方案1
微服务设置超时时间:
# 设置连接超时时间(秒)
feign.client.config.default.connectTimeout = 15000
# 设置读取超时时间(秒)
feign.client.config.default.readTimeout = 15000

方案2
设置restTemplate超时时间

复制代码
import com.google.common.base.Charsets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean("restTemplate")
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(30000);
        httpRequestFactory.setReadTimeout(30000);
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.getMessageConverters()
                .set(1, new StringHttpMessageConverter(Charsets.UTF_8));
        return restTemplate;
    }
}
复制代码

恢复数据的方法
1.使用postman来请求
2.数据按日期获取,恢复数据, 通过单元类批量来处理。 设置开始和结束时间段区间。按日期来调用

伪代码

复制代码
        String beginDate = "2025-01-04";
        String endDate = "2025-02-13";
        String result = "";
        LocalDate beginLd = LocalDateTimeUtil.parseLocalDate(beginDate,"yyyy-MM-dd");
        LocalDate endLd = LocalDateTimeUtil.parseLocalDate(endDate,"yyyy-MM-dd");
        while (beginLd.isBefore(endLd) || beginLd.isEqual(endLd)){
            result += beginLd.toString() + "开始执行" + LocalDateTime.now();
            try {
//              taskService.couponTask(1,beginLd.toString());
                System.out.println("执行do..." + beginLd.toString());
                Thread.sleep(2000);
            }catch (Exception e) {
                e.printStackTrace();
            }
            result += beginLd.toString() + "执行完成" + LocalDateTime.now();
            beginLd = beginLd.plusDays(1);
        }
        System.out.println("result=" + result);
复制代码

补充:
Feign在整合Ribbon的时候,为了统一配置,就默认将自己的超时时间交由Ribbon管理
所以,在默认情况下,Feign的超时时间可以由Ribbon配置
而Ribbon默认连接和读超时时间只有1s,所以在默认情况下,Feign的超时时间只有1s。


eureka feign默认超时时间
Feign 客户端在使用 Eureka 时,默认的超时时间是 1 秒。这个超时时间可以通过配置文件来调整。 >> 默认的就是连接超时10s,读超时60s
在 application.yml 或 application.properties 中,可以通过以下配置来设置 Feign 的超时时间:
# application.yml
# 设置 Feign 客户端的连接超时时间(秒)
feign.client.config.default.connectTimeout=5000
# 设置 Feign 客户端的读取超时时间(秒)
feign.client.config.default.readTimeout=5000

或者使用 application.properties 格式:
# application.properties
# 设置 Feign 客户端的连接超时时间(毫秒)
feign.client.config.default.connectTimeout=5000
# 设置 Feign 客户端的读取超时时间(毫秒)
feign.client.config.default.readTimeout=5000
以上配置将 Feign 的连接超时时间和读取超时时间分别设置为 5 秒。如果你使用的是不同的客户端配置,需要将 default 替换为相应的配置名。

posted on   oktokeep  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2021-03-03 es创建索引及别名更新mapping方法 elasticsearch [nested] nested object under path [XXX] is not of nested type
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示