Spring Web-Flux 服务器发送事件在使用 IP 进行本地和远程调用时不起作用。

huangapple go评论66阅读模式
英文:

Spring Web-Flux Server Sent Events is not working when using IP for local and remote calls

问题

我有两个简单的控制器:

@GetMapping(value = "/simple-get")
public String simpleGet() {
    return "simple Get";
}

@GetMapping(path = "/stream-flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamFlux() {
    return Flux.interval(Duration.ofSeconds(2))
            .map(sequence -> "Flux - " + LocalTime.now().toString());
}

当调用第一个控制器时,它按预期工作(在本地和远程使用不同机器的 IP)。但是当调用第二个控制器时,在本地使用 localhost(例如:http://localhost:65465/stream-flux)按预期工作,但在部署到不同机器的 IP 后在远程不起作用。我甚至检查过,当使用 127.0.0.1 时,在本地也不起作用,这非常奇怪...所有在 WebFlux 中不起作用的东西,在 simple-get API 中按预期工作,因此我排除了连接问题...在任何地方都找不到有关此的任何信息...非常感谢对此的任何想法。

英文:

I have two simple controllers:

@GetMapping(value = &quot;/simple-get&quot;)
public String simpleGet() {
    return &quot;simple Get&quot;;

}

@GetMapping(path = &quot;/stream-flux&quot;, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux&lt;String&gt; streamFlux() {
    return Flux.interval(Duration.ofSeconds(2))
            .map(sequence -&gt; &quot;Flux - &quot; + LocalTime.now().toString());
}

When calling the first one, it works as expected (locally and remotely using an ip of a different machine). But when calling the second one, it works as expected using localhost e.g.:
http://localhost:65465/stream-flux but it does not work remotely when deployed to a different machine using its ip. I even checked and it does not work locally when using 127.0.0.1 which is very strange..
all things that does not work with web-flux, works as expected with the simple-get API so I rule out connection problems..Couldn't find anything about it anywhere.. would appreciate any thoughts on this.

答案1

得分: 3

感谢所有对提供帮助感兴趣的人。经过进一步调查,我们已经找到了导致 RC(这里可能是某个问题或现象的缩写)的原因,实际上与代码/Spring-web-flux无关。
当使用 IP 地址或远程调用时,请求会在挂起状态下保持,直到达到服务器上定义的超时时间。
在 Windows 计算机上使用命令提示符中的 netstat -ano | findstr 8080,我们能够找到正在使用的端口及其状态。
当使用 localhost 时,首次调用 API 会分配一个额外的端口来建立连接,该连接将保持打开状态,以用于稍后的服务器发送事件(Server Sent Events),我们可以看到监听该新端口的进程的 PID 是浏览器。
当使用 IP 地址时,发生了完全相同的情况,但现在监听新端口的进程实际上是防病毒软件。
Sophos 防病毒软件具有 Web 保护功能,它将 SSE 视为恶意下载线程,因此它等待 HTTP 响应到达以进行扫描,在 SSE 中,只要连接保持打开状态,响应就不会到达。

更多详细信息:

链接:https://community.sophos.com/free-antivirus-tools-for-desktops/f/discussions/5750/sophos-av-blocks-server-sent-events-sse-on-mac-os-x-yosemite

链接:https://stackoverflow.com/questions/12978466/javascript-eventsource-sse-not-firing-in-browser

使用 HTTPS 应该可以解决这个问题。

谢谢

英文:

Thanks for everyone who were interested in helping. After additional investigation we have found the RC and it actually has nothing to do with the code/Spring-web-flux.
When using ip address or calling it remotely, the request was stuck on pending until it reached the timeout defined on the server.
On a windows machine using netstat -ano | findstr 8080 on the cmd
We were able to find the ports being used and their status.
When using local host, the first call to the api assigns an additional port to establish a connection that would remain open for later Server Sent Events and we can see the PID of the process that is listening to that new port is the browser.
When using ip address. same exact thing is happening but the process that is now listening to the new port is in fact the anti-virus.
Sophos antivirus has a feature of web-protection and it treats SSE as a malicious download thread so it waits for the http response to arrive in order to scan it, in SSE it never arrives as long as the connection is open.

more details:

https://community.sophos.com/free-antivirus-tools-for-desktops/f/discussions/5750/sophos-av-blocks-server-sent-events-sse-on-mac-os-x-yosemite

https://stackoverflow.com/questions/12978466/javascript-eventsource-sse-not-firing-in-browser

Using https should solved the problem.

Thanks

huangapple
  • 本文由 发表于 2020年10月18日 20:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64413123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定