• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot设置接口超时的方法小结

springboot 搞代码 4年前 (2022-01-09) 11次浏览 已收录 0个评论
文章目录[隐藏]

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

或者

本文来源gaodai#ma#com搞*代#码9网#

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

推荐文章:

来源于:

https://blog.gaodaima.com/qq_35860138/article/details/88941558

https://blog.gaodaima.com/weixin_34114823/article/details/86015073

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot设置接口超时的方法小结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址