SpringBoot Rest Api 設定超時時間
阿新 • • 發佈:2018-11-25
專案有一對外開放api,外網訪問經常出現超時,剛接觸spring boot不久,內建的tomcat不像原先那樣在server.xml中設定request超時時間。
後來查了些資料,在配置檔案application.properties中加了spring.mvc.async.request-timeout=20000,意思是設定超時時間為20000ms即20s,超時問題的確不怎麼發生了。
還有另外一種設定方式,如下:
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(); } }
沒有親自實踐過,寫這裡做個記錄。