網站首頁 編程語言 正文
背景:
Nginx做反向代理,springboot為后端服務。
問題:
通過瀏覽器向后臺發起請求夠,由于后臺處理時間長,出現504 Gateway Time-out,實際后臺程序依然在執行。如何解決?
504從哪來:本文的場景下504是nginx返回的。
nginx配置中控制該超時時間的屬性:
Syntax: | proxy_read_timeout?time; |
---|---|
Default: | proxy_read_timeout 60s; |
Context: | http,?server,?location |
官方地址:Module ngx_http_proxy_module (nginx.org)
官方描述如下:Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
一個請求有三方參與:瀏覽器,nginx,后臺服務器。
504的錯誤碼是有nginx返回的。結合官網的解釋,我們可以得出結論:
當nginx與后臺的鏈接兩次讀取有效數據之間超過配置的時間時,就會產生504超時。nginx會主動關閉與后臺服務器的鏈接。注意是兩次成功讀取的間隔,不是整個reponse的時間。
默認情況下proxy_read_timeout時60s。
如果你百度或google,通常解決方式有兩種:提高后臺處理效率 或 增大proxy_read_timeout。
增大方法很簡單,proxy_read_timeout? [你期望的時間]。
But,后臺效率提升總是有極限的。而proxy_read_timeout是固定值。總會有些正常業務場景,超過了設置的timeout值。
兩種解決方案
本人解決的問題:上傳excel文件后,由于文件大小無法預計,所以后臺處理時間也無法預計。同時還要支持大文件的上傳。上傳后由后臺解析處理。post請求,返回的是json。
一,關閉read-timout,可以實現,但是生產環境下你敢不設置超時時間么?所以不建議。
二,既然nginx只要從reponse成功讀取數據兩次的間隔在proxy_read_timeout設置的時間內,就不會超時。那么我們是不是可以通過持續的向response中寫入數據來保證不超時呢。
答案是肯定的。
想通了這一點,實現就十分簡單。
1,正常上傳文件。
2,新建一個線程。持有response的引用,含有標志位,滿足條件時循環執行,程序開始處理數據前,啟動線程。
3,線程的功能只有一個,以固定間隔向response中寫入數據。使nginx與后臺鏈接不超時。
4,這里就需要注意,我的方法是返回json,同時要持續向response寫入數據,所以我手動拼裝json字符串。相當于在之前返回的json中增加一個屬性,名稱隨意,我的叫pending,值隨意,非空即可。我是用英文半角的句號" . "。
5,數據處理完后,回調線程的stop方法,終止線程中的循環。
注意:如有雷同純屬巧合。如果已經有大佬講過這種解決方式,請艾特我,我立即刪除本文。
保持線程代碼如下:
#上下文代碼
//獲取鮮橙池executor,具體方式看個人。不會的直接百度,有很多
response.setContentType(ContentType.APPLICATION_JSON.getMimeType());
ResponseKeeper responseKeeper = new ResponseKeeper(response);
executorService.execute(responseKeeper);
#上下文代碼
public class ResponseKeeper implements Runnable {
/**
* 循環標志:true時停止循環,終止線程
*/
private boolean done = false;
private HttpServletResponse response;
public void stop(){
done = true;
}
public ResponseKeeper(HttpServletResponse response) {
this.response = response;
}
@Override
public void run() {
try {
response.getWriter().write("{\"pending\":\"");
while(!done){
response.getWriter().write(".");
response.getWriter().flush();
LOGGER.error("flush-{}", System.currentTimeMillis());
Thread.sleep(1000);
}
response.getWriter().write("\", \"status\": \"0\", \"msg\":\"success\"}");
} catch (Exception e) {
e.printStackTrace();
}
}
}
其他問題:
如果你遇到異常
IllegalStateException – if the getOutputStream method has already been called for this response object
那就說明你的程序中有地方調用過了,response.getOutputStream();
只需要與已有程序保持一致使用outputStream即可。
即將response.getWriter() 提換成?response.getOutputStream();
原因簡單來講就是這兩個方法互斥。調用了一個就不能調用另一個。
總結
原文鏈接:https://blog.csdn.net/fox_mt/article/details/125249100
相關推薦
- 2022-12-11 Rust中Cargo的使用詳解_Rust語言
- 2022-09-01 Android使用Intent傳遞組件大數據_Android
- 2022-09-14 python?特殊屬性及方法詳細解析_python
- 2024-03-03 ElementUi中el-cascader表單驗證問題
- 2022-06-18 android實現在圖標上顯示數字_Android
- 2022-05-12 Android 記錄build打包的時間并顯示到手機上面
- 2022-04-11 python實現簡易計算器功能_python
- 2022-09-22 stack和queue的模擬實現
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支