SpringBoot設(shè)置接口超時的方法小結(jié)
1、配置文件
application.properties中加了,意思是設(shè)置超時時間為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# 建立連接成功后 從服務(wù)器讀取超時 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
來源于:
https://blog.csdn.net/qq_35860138/article/details/88941558
https://blog.csdn.net/weixin_34114823/article/details/86015073
到此這篇關(guān)于SpringBoot設(shè)置接口超時的方法小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot接口超時內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java多線程編程之向線程傳遞數(shù)據(jù)的三種方法
在多線程的異步開發(fā)模式下,數(shù)據(jù)的傳遞和返回和同步開發(fā)模式有很大的區(qū)別。由于線程的運行和結(jié)束是不可預(yù)料的,因此,在傳遞和返回數(shù)據(jù)時就無法象函數(shù)一樣通過函數(shù)參數(shù)和return語句來返回數(shù)據(jù)2014-01-01使用SpringBoot中的Schedule定時發(fā)送郵件的方法
在SpringBoot中,你可以使用@Scheduled注解來創(chuàng)建定時任務(wù),@Scheduled注解可以應(yīng)用于方法上,表示這個方法是一個定時任務(wù),可以根據(jù)指定的時間間隔或固定時間執(zhí)行,本文就給大家介紹一下如何使用SpringBoot中的Schedule定時發(fā)送郵件,需要的朋友可以參考下2023-08-08