Springboot調整接口響應返回時長詳解(解決響應超時問題)
配置Http會話超時
可以通過兩種方式為Spring Boot應用程序配置HTTP會話超時。
application.properties中配置會話超時
最簡單的方法是在你的application.properties中加入?yún)?shù)server.servlet.session.timeout。比如說
server.servlet.session.timeout=60s
還要注意的是,Tomcat不允許你將超時時間設置得少于60秒。
以程序方式配置會話超時
假設我們想讓我們的HttpSession只持續(xù)兩分鐘。為了實現(xiàn)這一點,我們可以在我們的WebConfiguration類中添加一個EmbeddedServletContainerCustomizer Bean,內容如下。
@Configuration public class WebConfiguration { @Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setSessionTimeout(2, TimeUnit.MINUTES); } }; } }
這里再給出一個使用Java 8和lambda表達式的捷徑寫法。
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() { return (ConfigurableEmbeddedServletContainer container) -> { container.setSessionTimeout(2, TimeUnit.MINUTES); }; }
在應用程序啟動期間,Spring Boot自動配置檢測到EmbeddedServletContainerCustomizer,并調用customize(…)方法,傳遞對Servlet容器的引用。
配置接口訪問超時
SpringBoot設置接口訪問超時時間有兩種方式
一、配置文件方式
在配置文件application.properties
中加了spring.mvc.async.request-timeout=120000
,意思是設置超時時間為120000ms即120s
# [設置接口的超時時間] spring.mvc.async.request-timeout=120000
二、配置Config配置類
還有一種就是在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(); } }
采用上面的兩種配置之一后,重新運行服務,調用接口最大等待的響應時間為上面設置的120s。
需要避免踩到的坑
如果按上述配置后,還是會出現(xiàn)超時情況,有可能是以下幾種技術的問題,需要對應設置一下。
tomcat的設置
上文中是springboot開發(fā)環(huán)境,使用了內置的tomcat。而在實際生產環(huán)境中一般用的是外置tomcat來部署(便于后續(xù)發(fā)布更新),需要在tomcat的配置文件server.xml中設置超時時間(默認20秒以下設置為120秒)。
<Connector port="8811" protocol="HTTP/1.1" connectionTimeout="120000" redirectPort="8443" />
Nginx的設置
如果服務端使用到Nginx做了反向代理轉發(fā)請求,就需要在Nginx的配置文件nginx.conf中設置超時時間,否則會返回“java.io.IOException: 你的主機中的軟件中止了一個已建立的連接”這樣的異常提示。
未設置時Nginx響應時間默認60秒,這里我將http頭部的keepalive_timeout
、client_header_timeout
、client_body_timeout
、send_timeout
、以及server代碼塊中的proxy_read_timeout
均配置為120秒。
http { include mime.types; default_type application/octet-stream; client_max_body_size 100m; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 120; #連接超時時間,服務器將會在這個時間后關閉連接 send_timeout 120; #發(fā)送超時時間 client_header_timeout 120; #請求頭的超時時間 client_body_timeout 120; #請求體的讀超時時間 #gzip on; ..... #業(yè)務系統(tǒng)的配置 server { listen 9092; server_name localhost; location / { proxy_pass http://127.0.0.1:8811/mywebsev/; proxy_read_timeout 120; # 等候后端服務器響應時間 秒 } } }
參考文章
springboot:實現(xiàn)異步響應請求(解決前端請求超時的問題)
總結
到此這篇關于Springboot調整接口響應返回時長(解決響應超時問題)的文章就介紹到這了,更多相關Springboot調整接口響應返回時長內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Security整合KeyCloak保護Rest API實現(xiàn)詳解
這篇文章主要為大家介紹了Spring Security整合KeyCloak保護Rest API實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11解決后端傳long類型數(shù)據(jù)到前端精度丟失問題
這篇文章主要介紹了解決后端傳long類型數(shù)據(jù)到前端精度丟失問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01解決RestTemplate 的getForEntity調用接口亂碼的問題
這篇文章主要介紹了解決RestTemplate 的getForEntity調用接口亂碼的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java微信開發(fā)API第三步 微信獲取以及保存接口調用憑證
這篇文章主要為大家詳細介紹了java微信開發(fā)API第二步,微信獲取以及保存接口調用憑證,感興趣的小伙伴們可以參考一下2016-06-06