关于SseEmitter(SSE)在本地可以响应,部署到服务器后无法响应的问题
@GetMapping(value = "/v3/detail", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public SseEmitter v3Detail(String id) {
SseEmitter emitter = new SseEmitter((long) (60 * 1000 * 10));
V2Params params = v3ParamMap.get(id);
if (params == null) {
getSend(emitter, "result", "id is not found: " + id);
return emitter;
}
// CODE...
return emitter;
}
本地刷刷就响应了,服务器上无法响应:
经过排查发现是 nginx 的问题
因为是域名通过nginx代理转发到java应用的
解决:
http {
upstream java_app {
server 127.0.0.1:8056;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://java_app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# SSE support
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
}
}