2017-08-24 85 views
3

让我先描述给定的情况。如何处理春季启动应用程序中的长轮询,由nginx负载均衡?

我有一个有角度的JavaScript前端。我需要使用websocket,因此我使用“sockjs”和“stomp-websocket”。

var socket, 
    client; 
socket = new SockJS('http://localhost:8080/stomp'); 
client = Stomp.over(socket); 
client.connect({}, function() { 
    client.subscribe('/dummy/message', function (message) { 
    console.log('subscribed'); 
    } 
}); 

我的后台是一个春天启动应用程序:

@Configuration 
@EnableWebSocketMessageBroker 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { 

    @Override 
    public void configureMessageBroker(MessageBrokerRegistry registry) { 
     registry.enableSimpleBroker("/dummy"); 
     registry.setApplicationDestinationPrefixes("/app"); 
    } 

    @Override 
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { 
     stompEndpointRegistry 
      .addEndpoint("/stomp") 
      .setAllowedOrigins("*") 
      .withSockJS() 
      .setSessionCookieNeeded(false); 
    } 
} 

这种设置工作没有任何问题。即使当我在Firefox中禁用websockets时,它仍然可以正常工作(在这种情况下可以回退)。

当我启动后端的更多实例并使用nginx时,我的问题就开始了。

我的nginx的配置是:

upstream ws_be { 
    server localhost:8081; 
    server localhost:8082; 
} 

server { 
    listen 8080; 

    location/{ 
     proxy_pass http://ws_be; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
    } 
} 

当我使用这个设置了多个后端,的WebSockets如预期如果我在我的浏览器禁用的WebSockets回退不工作了仍然有效。它只是保持连接和即时断开连接。

错误在浏览器控制台:

Opening Web Socket... stomp.min.js:8:1893 
Web Socket Opened... stomp.min.js:8:1893 
>>> CONNECT 
accept-version:1.1,1.0 
heart-beat:10000,10000 

<<< CONNECTED 
version:1.1 
heart-beat:0,0 

connected to server undefined stomp.min.js:8:1893 
>>> SUBSCRIBE 
id:sub-0 
destination:/dummy/message 

Whoops! Lost connection to undefined 

在后端我得到一个IO破管道错误:

org.apache.catalina.connector.ClientAbortException: java.io.IOException: Datenübergabe unterbrochen (broken pipe) 

在这一点上我不知道我怎么也得配置nginx的还是我的后端可以使用给定的设置正常工作。 现在我希望有人对此问题有一个想法或暗示。

+0

你能否检查你的浏览器devtools哪个传输是由浏览器选择的,你在网络标签中得到了什么错误信息? –

+0

它不断重复这四个请求: - GET http:// localhost:8080/stomp/info - >状态200 - POST http:// localhost:8080/stomp/101/x_bobkwk/xhr_streaming - >状态200中止 - POST http:// localhost:8080/stomp/101/x_bobkwk/xhr_send - >状态204 - POST http:// localhost:8080/stomp/101/x_bobkwk/xhr_send - >状态404 – Vadim

+0

在这种情况下,与此有关? https://serverfault.com/questions/789417/should-proxy-buffering-be-disabled-in-nginx-to-support-sockjs-xhr-streaming –

回答