最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)詳解(解決響應(yīng)超時(shí)問(wèn)題)

 更新時(shí)間:2023年01月30日 10:11:21   作者:JermeryBesian  
當(dāng)后端對(duì)于數(shù)據(jù)量較大的處理或是某些耗時(shí)的操作時(shí),需要先對(duì)請(qǐng)求接口的請(qǐng)求進(jìn)行響應(yīng),下面這篇文章主要給大家介紹了關(guān)于Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)(解決響應(yīng)超時(shí)問(wèn)題)的相關(guān)資料,需要的朋友可以參考下

配置Http會(huì)話超時(shí)

可以通過(guò)兩種方式為Spring Boot應(yīng)用程序配置HTTP會(huì)話超時(shí)。

application.properties中配置會(huì)話超時(shí)

最簡(jiǎn)單的方法是在你的application.properties中加入?yún)?shù)server.servlet.session.timeout。比如說(shuō)

server.servlet.session.timeout=60s

還要注意的是,Tomcat不允許你將超時(shí)時(shí)間設(shè)置得少于60秒。

以程序方式配置會(huì)話超時(shí)

假設(shè)我們想讓我們的HttpSession只持續(xù)兩分鐘。為了實(shí)現(xiàn)這一點(diǎn),我們可以在我們的WebConfiguration類中添加一個(gè)EmbeddedServletContainerCustomizer Bean,內(nèi)容如下。

@Configuration
public class WebConfiguration {
  @Bean
  public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
      @Override
      public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setSessionTimeout(2, TimeUnit.MINUTES);
      }
    };
  }
}

這里再給出一個(gè)使用Java 8和lambda表達(dá)式的捷徑寫(xiě)法。

public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
    return (ConfigurableEmbeddedServletContainer container) -> {
      container.setSessionTimeout(2, TimeUnit.MINUTES);
    };
  }

在應(yīng)用程序啟動(dòng)期間,Spring Boot自動(dòng)配置檢測(cè)到EmbeddedServletContainerCustomizer,并調(diào)用customize(…)方法,傳遞對(duì)Servlet容器的引用。

配置接口訪問(wèn)超時(shí)

SpringBoot設(shè)置接口訪問(wèn)超時(shí)時(shí)間有兩種方式

一、配置文件方式

在配置文件application.properties中加了spring.mvc.async.request-timeout=120000,意思是設(shè)置超時(shí)時(shí)間為120000ms即120s

# [設(shè)置接口的超時(shí)時(shí)間]
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();
	}
}

采用上面的兩種配置之一后,重新運(yùn)行服務(wù),調(diào)用接口最大等待的響應(yīng)時(shí)間為上面設(shè)置的120s。

需要避免踩到的坑

如果按上述配置后,還是會(huì)出現(xiàn)超時(shí)情況,有可能是以下幾種技術(shù)的問(wèn)題,需要對(duì)應(yīng)設(shè)置一下。

tomcat的設(shè)置

上文中是springboot開(kāi)發(fā)環(huán)境,使用了內(nèi)置的tomcat。而在實(shí)際生產(chǎn)環(huán)境中一般用的是外置tomcat來(lái)部署(便于后續(xù)發(fā)布更新),需要在tomcat的配置文件server.xml中設(shè)置超時(shí)時(shí)間(默認(rèn)20秒以下設(shè)置為120秒)。

    <Connector port="8811" protocol="HTTP/1.1"
               connectionTimeout="120000"
               redirectPort="8443" />

Nginx的設(shè)置

如果服務(wù)端使用到Nginx做了反向代理轉(zhuǎn)發(fā)請(qǐng)求,就需要在Nginx的配置文件nginx.conf中設(shè)置超時(shí)時(shí)間,否則會(huì)返回“java.io.IOException: 你的主機(jī)中的軟件中止了一個(gè)已建立的連接”這樣的異常提示。

未設(shè)置時(shí)Nginx響應(yīng)時(shí)間默認(rèn)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; #連接超時(shí)時(shí)間,服務(wù)器將會(huì)在這個(gè)時(shí)間后關(guān)閉連接
    send_timeout 120;    #發(fā)送超時(shí)時(shí)間
    client_header_timeout 120;    #請(qǐng)求頭的超時(shí)時(shí)間
    client_body_timeout 120;    #請(qǐng)求體的讀超時(shí)時(shí)間
    #gzip  on;
 
.....
 
    #業(yè)務(wù)系統(tǒng)的配置
    server {
        listen       9092;
        server_name  localhost;
	
    	location / {
             proxy_pass http://127.0.0.1:8811/mywebsev/;
	         proxy_read_timeout 120;  # 等候后端服務(wù)器響應(yīng)時(shí)間 秒
            }
    }
}

參考文章

springboot:實(shí)現(xiàn)異步響應(yīng)請(qǐng)求(解決前端請(qǐng)求超時(shí)的問(wèn)題)

總結(jié)

到此這篇關(guān)于Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)(解決響應(yīng)超時(shí)問(wèn)題)的文章就介紹到這了,更多相關(guān)Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

洛隆县| 小金县| 曲松县| 沁水县| 绥芬河市| 三河市| 阿勒泰市| 钟山县| 邵东县| 额济纳旗| 沂南县| 格尔木市| 鄯善县| 勃利县| 休宁县| 涡阳县| 昆山市| 麻城市| 广河县| 汉中市| 银川市| 温州市| 泾源县| 榆中县| 万源市| 江津市| 沈丘县| 高州市| 鄢陵县| 同江市| 顺义区| 泽普县| 剑阁县| 清水河县| 原阳县| 敖汉旗| 锡林浩特市| 安泽县| 白银市| 永德县| 轮台县|