SpringBoot設(shè)置接口超時(shí)的方法小結(jié)
1、配置文件
application.properties中加了,意思是設(shè)置超時(shí)時(shí)間為20000ms即20s,
spring.mvc.async.request-timeout=20000
2、config配置類(lèi)
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超時(shí)
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配置# 連接不共用的時(shí)候,等待連接超時(shí)。 rest.connection.connectionRequestTimeout=30000# 建立連接超時(shí) rest.connection.connectTimeout=30000# 建立連接成功后 從服務(wù)器讀取超時(shí) rest.connection.readTimeout=30000
或者
#restTemplate配置 rest.connection.connection-request-timeout=30000 rest.connection.connect-timeout=30000 rest.connection.read-timeout=30000
推薦文章:
http://www.dbjr.com.cn/article/167638.htm
來(lái)源于:
https://blog.csdn.net/qq_35860138/article/details/88941558
https://blog.csdn.net/weixin_34114823/article/details/86015073
到此這篇關(guān)于SpringBoot設(shè)置接口超時(shí)的方法小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot接口超時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)List中對(duì)象排序的方法
這篇文章主要介紹了java實(shí)現(xiàn)List中對(duì)象排序的方法,涉及Java中的遍歷與對(duì)象操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11java多線(xiàn)程編程之向線(xiàn)程傳遞數(shù)據(jù)的三種方法
在多線(xiàn)程的異步開(kāi)發(fā)模式下,數(shù)據(jù)的傳遞和返回和同步開(kāi)發(fā)模式有很大的區(qū)別。由于線(xiàn)程的運(yùn)行和結(jié)束是不可預(yù)料的,因此,在傳遞和返回?cái)?shù)據(jù)時(shí)就無(wú)法象函數(shù)一樣通過(guò)函數(shù)參數(shù)和return語(yǔ)句來(lái)返回?cái)?shù)據(jù)2014-01-01java樂(lè)觀鎖原理與實(shí)現(xiàn)案例分析
這篇文章主要介紹了java樂(lè)觀鎖原理與實(shí)現(xiàn),結(jié)合具體案例形式分析了樂(lè)觀鎖的原理及java使用樂(lè)觀鎖實(shí)現(xiàn)自動(dòng)派單功能的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10使用SpringBoot中的Schedule定時(shí)發(fā)送郵件的方法
在SpringBoot中,你可以使用@Scheduled注解來(lái)創(chuàng)建定時(shí)任務(wù),@Scheduled注解可以應(yīng)用于方法上,表示這個(gè)方法是一個(gè)定時(shí)任務(wù),可以根據(jù)指定的時(shí)間間隔或固定時(shí)間執(zhí)行,本文就給大家介紹一下如何使用SpringBoot中的Schedule定時(shí)發(fā)送郵件,需要的朋友可以參考下2023-08-08log4j升級(jí)log4j2遇到的問(wèn)題及解決方式
這篇文章主要介紹了log4j升級(jí)log4j2遇到的問(wèn)題及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12