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. 【USACO 2006 November Gold】Corn Fields

    [题目链接] 点击打开链接 [算法] 状压DP [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 12 #def ...

  2. 洛谷P3354 [IOI2005]Riv 河流——“承诺”DP

    题目:https://www.luogu.org/problemnew/show/P3354 状态中要记录一个“承诺”,只需相同承诺之间相互转移即可: 然后就是树形DP的套路了. 代码如下: #inc ...

  3. 将svn的项目转移到另外一个仓库中

    前几天在做一个项目的时候,因为需要,需要将Server A 上SVN仓库 repos1中的项目pro1迁移到Server B 上的SVN仓库中,首先想到的是:通过复制,但是仔细一想,这样是不可能的:然 ...

  4. 【转】PL/SQL 使用技巧

    ref:http://blog.chinaunix.net/uid-21592001-id-3082675.html [转]plsql developer 使用技巧 Oracle数据库相信已成为很多企 ...

  5. 微信公众号开发及时获取当前用户Openid及注意事项

    目录 (一)微信公众号开发之VS远程调试 (二)微信公众号开发之基础梳理 (三)微信公众号开发之自动消息回复和自定义菜单 (四)微信公众号开发之网页授权获取用户基本信息 (五)微信公众号开发之网页中及 ...

  6. 算法学习--Day2

    今天要多学一些内容了,昨天就写了一点sort和struct的用法,今天写了两道关于日期的题目,记录在这里. 题目描述 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 ...

  7. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

  8. Ubuntu 18.04 LTS 常见问题解决 2

    1 每次重启后都有system program problem detected sudo gedit /etc/default/apport 然后将打开的文件中的enabled=1改为0,原因暂时不 ...

  9. Nginx系列篇一:linux中安装Nginx

    提示: 如遇到yum或者wget的问题, 请详见--->杂集:更换centos yum源 请详见--->杂集:关于VMware中linux使用NAT模式配置 1.安装nginx需要的环境 ...

  10. Educational Codeforces Round 46 (Rated for Div. 2) A. Codehorses T-shirts

    Bryce1010模板 http://codeforces.com/problemset/problem/1000/A 题意: 问你将一种类型的衣服转换成另一种的最小次数. #include<b ...