springboot 设置接口超时

1、配置文件 application.properties中加了,意思是设置超时时间为20000ms即20s,

spring.mvc.async.request-timeout=20000

2、config配置类

public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(20000);
configurer.registerCallableInterceptors(timeoutInterceptor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
}

3、RestTemplate超时

设置配置HttpComponentsClientHttpRequestFactory中的RequestConfig属性
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; @Slf4j
@Configuration
public class RestTemplateConfig { @Bean
@ConfigurationProperties(prefix = "rest.connection")
public HttpComponentsClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory();
} @Bean
public RestTemplate customRestTemplate(){
return new RestTemplate(httpRequestFactory());
}
}

也可以:

@Beanpublic SimpleClientHttpRequestFactory httpRequestFactory() {
  SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  requestFactory.setConnectTimeout(1000);
  requestFactory.setReadTimeout(1000);
  return requestFactory;
} @Bean
public RestTemplate customRestTemplate(){
  return new RestTemplate(httpRequestFactory());
}

application.properties:

#restTemplate配置
# 连接不共用的时候,等待连接超时。
rest.connection.connectionRequestTimeout=30000
#  建立连接超时
rest.connection.connectTimeout=30000
# 建立连接成功后 从服务器读取超时
rest.connection.readTimeout=30000

或者

#restTemplate配置
rest.connection.connection-request-timeout=30000
rest.connection.connect-timeout=30000
rest.connection.read-timeout=30000

来源于:

https://blog.csdn.net/qq_35860138/article/details/88941558

https://blog.csdn.net/weixin_34114823/article/details/86015073

springboot设置接口超时的更多相关文章

  1. 【轮询】【ajax】【js】【spring boot】ajax超时请求:前端轮询处理超时请求解决方案 + spring boot服务设置接口超时时间的设置

    场景描述: ajax设置timeout在本机测试有效,但是在生产环境等外网环境无效的问题 1.ajax的timeout属性设置 前端请求超时事件[网络连接不稳定时候,就无效了] var data = ...

  2. springboot设置session超时和session监听

    2.0版本以下设置session超时时间 1.  springboot 2.0版本以下配置session超时 1.1 application.properties配置文件: spring.sessio ...

  3. Springboot 设置session超时

    使用版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...

  4. Springboot设置session超时时间

    按优先级高到低说: 第一种: spring boot 启动类里面: package com.mycenter; import org.mybatis.spring.annotation.MapperS ...

  5. postman接口超时设置,用于debug等断点调试

    Settings->General->Request Timeout in ms(0 for infinity):设置请求超时的时间,默认为0

  6. Retrofit2.0 设置 连接超时

    Retrofit2.0 这个网络请求框架使用了很久了,最近一次出现一个小插曲. 有一个接口,返回的数据量因为业务的原因 会返回很大的数据量,此时网络不大好的情况下,会出现请求失败的情况 也就是回调了 ...

  7. 转 HttpClient 设置连接超时时间

    要: HttpClient 4.5版本升级后,设置超时时间的API又有新的变化,请大家关注. HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳 ...

  8. HttpClient设置连接超时时间

    https://www.cnblogs.com/winner-0715/p/7087591.html 使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间.这两个参数很重要,目的是为 ...

  9. 【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】

    参考地址:https://www.jianshu.com/p/6f3ee90ab7d3 示例: public static void main(String[] args) throws Interr ...

随机推荐

  1. 【POJ 1655】 Balancing Act

    [题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...

  2. P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team

    题目描述 After Farmer Don took up Frisbee, Farmer John wanted to join in the fun. He wants to form a Fri ...

  3. Android之APP模块编译

    一,如何把app编进系统 a.源码编译,在packages/apps目录下有安卓原生的app,以Bluetooth为例,源码根目录下有Android.mk文件: packages\apps\Bluet ...

  4. 879C

    贪心 题目看错了...还以为是从操作序列中选5个...然后半个小时没了... 我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论.千万不用特判!!!之前忘了删了就fst... #include ...

  5. sql 日历

    /* Formatted on 2013/9/7 20:45:28 (QP5 v5.185.11230.41888) */ SELECT MIN (DECODE (TO_CHAR (DAYS, 'D' ...

  6. 洛谷 - P1141 - 01迷宫 - dfs

    https://www.luogu.org/problemnew/show/P1141 能互相到达的格子的答案自然是一样的,第一次dfs标记联通块,第二次dfs把cnt传递到整个联通卡并顺手消除vis ...

  7. 洛谷 - P1801 - 黑匣子 - 对顶堆

    这道题是提高+省选-的难度,做出来的话对数据结构题目的理解会增加很多. 可以使用一种叫做对顶堆的东西,对顶堆是在线维护第n小的logn的算法.大概的思路是,假如我们要找的是第n小,我们就维护一个大小为 ...

  8. 洛谷 P4125 [WC2012]记忆中的水杉树【扫描线+set+线段树】

    我没有找到能在bzojAC的代码--当然我也WA了--但是我在洛谷过了,那就假装过了吧 minmax线段树一开始写的只能用min更新min,max更新max,实际上是可以互相更新的-- 首先看第二问, ...

  9. Cloudera Manager是啥?主要是干啥的?

    简单来说,Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个小时内,运维人员从数十人降低到几人以内,极大的提 ...

  10. android 带checkbox的List

    可实现点击内容即可选中 http://blog.csdn.net/harvic880925/article/details/40475367