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超时

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;
}

@Beanpublic 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://www.jb51.net/article/167638.htm

来源于:

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

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

到此这篇关于SpringBoot设置接口超时的方法小结的文章就介绍到这了,更多相关SpringBoot接口超时内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot设置接口超时时间的方法

    SpringBoot设置接口访问超时时间有两种方式 一.在配置文件application.properties中加了spring.mvc.async.request-timeout=20000,意思是设置超时时间为20000ms即20s, 二.还有一种就是在config配置类中加入: public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureAsyncSupport(fin

  • 关于Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)

    背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯狂爆出异常提示:Command timed out after 6 second(s)...... Caused by: io.lettuce.core.RedisCommandTimeoutException: Command timed out after 6 second(s) at io.lettuce

  • 关于SpringBoot整合redis使用Lettuce客户端超时问题

    参考的博客 问题起因 做毕设的时候,使用到Lettuce连接redis,一段时间后不操作,再去操作redis,会报连接超时错误,在其重连后又可使用. 原因是:Lettuce 自适应拓扑刷新(Adaptive updates)与定时拓扑刷新(Periodic updates) 是默认关闭的导致问题的出现 解决的方案 1.重写连接工厂实例,更改其LettuceClientConfiguration 为开启拓扑更新 @Configuration public class RedisConfig { @

  • 详解SpringBoot中Session超时原理说明

    一:前言: 最近支付后台登录一段时间后如果没有任何操作,总是需要重新登录才可以继续访问页面,出现这个问题的原因就是session超时,debug代码后发现session的超时时间是1800s.也就是说当1800秒内没有任何操作,session就会出现超时现象.那这个超时时间是如何设置的呢?然后该如何重新设置此超时时间呢?系统又如何判断session超时的呢?接下来就一一进行解答. 二:系统session超时时间如何默认的? 说明:获取session超时时间的方法为"request.getSess

  • 基于springboot服务间Feign调用超时的解决方案

    解决springboot服务间Feign调用超时问题概述 1.起因 在完成项目功能需求的开发,经过自己测试以及通过测试组测试通过后,昨晚正式部署到线上环境进行正式运行前的最后一次的测试.但是在测试中,由A服务调用B服务接口时,***通过Feign调用(其实就是http请求,当A服务调用B服务时,如果不配置超时时间,那么A发出请求后,B应该立即响应,否则A服务会认为B已经断开连接)出现***连接超时的错误,错误信息:Read timed out- 2.原因 用idea开发debug模式调试代码时,

  • 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

  • Springboot设置默认访问路径方法实现

    前言 当使用springboot与其他框架结合编写web前后端时,可能存在这样的需求:我想在访问10.10.10.100时,实际上需要访问10.10.10.100/hello页面.(端口已省略,自行设置) 解决 方案1 - 实现WebMvcConfigurer接口 搜过很多博客,里面的内容虽然可以用.但是基本上都是基于继承WebMvcConfigurerAdapter类实现的,而官方的源码里面已经不推荐使用该类了. 下面给出我的解决方案,很简单: import org.springframewo

  • SpringBoot设置动态定时任务的方法详解

    之前写过文章记录怎么在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务执行时间,实在不太灵活. 经过网上搜索学习后,特此记录如何在SpringBoot项目中实现动态定时任务. 因为只是一个demo,所以只引入了需要的依赖: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <ar

  • 用JS动态设置CSS样式常见方法小结(推荐)

    用JS来动态设置CSS样式,常见的有以下几种 1. 直接设置style的属性 某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign) 如果想保留 - 号,就中括号的形式 element.style['text-align'] = '100px'; element.style.height = '100px'; 2. 直接设置属性(只能用于某些属性,相关样式会自动识别) element.setAttribute('height', 100);

  • C#访问SqlServer设置链接超时的方法

    本文实例讲述了C#访问SqlServer设置链接超时的方法.分享给大家供大家参考.具体实现方法如下: 下面这段代码设置超时时间为60秒,默认为30秒 using (connection) { SqlCommand sqlcommand = connection.CreateCommand(); sqlcommand.CommandTimeout = 60; //默认时间为 30 秒 sqlcommand.CommandText = sqlText; ... 希望本文所述对大家的C#程序设计有所帮

  • vue-router之nuxt动态路由设置的两种方法小结

    方法一:router-link <div class="slide-item" v-for="user in shareData.users"> <nuxt-link :to="'/community/member/'+ user.id"> <img src="../../static/head.png" alt=""> <p>{{user.nickname}

  • SpringBoot设置默认主页的方法步骤

    1.若采用渲染引擎,JSP等VIEW渲染技术,可以通过addViewController的方式解决. 即: @Configuration public class DefaultView extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/Blog").setVi

  • Java网络编程教程之设置请求超时的方法

    一.引言 随着企业系统的发展,应用多采用分布式结构,严重依赖于网络的稳定性.但由于网络天生的不稳定性,系统开发过程中需要考虑网络不稳定情况下如何保证应用的鲁棒性. 设置网络超时是其中一种保证应用健壮性的手段. 设置网络超时设置后,请求在设定时间能未完成将被强制终止,保证程序不出现无限制的线程阻塞情况,有效的提高了应用的可用性. 下面话不多说了,来一起看看详细的介绍吧. 二.未设置超时与设置超时情况对比 1. 网络请求图例: 网络请求超时案例 2. 设置超时时间后,请求图例: 网络请求超时案例-设

  • SpringBoot设置编码UTF-8的两种方法

    上篇文章给大家介绍了springboot全局字符编码设置解决乱码问题 感兴趣的朋友可以点击查看,下面通过两种方式给大家介绍SpringBoot 设置编码UTF-8的方法,具体内容如下所示: 第一种  通过过滤器来设置 @Configuration public class UtfConfig { @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBea

随机推荐