Spring Boot Web Socket RabbitMQ 不可用

Posted

技术标签:

【中文标题】Spring Boot Web Socket RabbitMQ 不可用【英文标题】:Spring Boot Web Socket RabbitMQ not available 【发布时间】:2018-02-15 21:15:56 【问题描述】:

我有一个简单的带有 web 套接字配置的 spring boot 应用程序。

当我使用 SimpleBroker(由 spring 提供)运行我的应用程序时,一切正常,但是当我想使用 rabbitmq 而不是 SimpleBroker 时,我遇到了一些问题,我的代理“不可用”

@Configuration
@EnableWebSocketMessageBroker
open class WebSocketConfig : ILogging by LoggingImp<WebSocketConfig>(), 
WebSocketMessageBrokerConfigurer 

@Autowired
private lateinit var env: Environment

override fun registerStompEndpoints(registry: StompEndpointRegistry) 
    registry.addEndpoint("/hig").setAllowedOrigins("*").withSockJS()


override fun configureMessageBroker(registry: MessageBrokerRegistry) 
    registry.setApplicationDestinationPrefixes("/app")
    val host = env.getProperty("spring.rabbitmq.host")!!
    val port = env.getProperty("spring.rabbitmq.port")!!.toInt()
    val login = env.getProperty("spring.rabbitmq.username")!!
    val pass = env.getProperty("spring.rabbitmq.password")!!
    log.debug("webSocket=$host, $port, $login, $pass")
//        registry.enableSimpleBroker("/chat")
    registry.enableStompBrokerRelay("/chat")
            .setRelayHost(host)
            .setRelayPort(port)
            .setClientLogin(login)
            .setClientPasscode(pass)
            .setAutoStartup(true)
            .setSystemHeartbeatReceiveInterval(10000)
            .setSystemHeartbeatSendInterval(10000)
    


来自 WebSocketMessageBrokerStats 的响应:


"loggingPeriod": 1800000,
"webSocketSessionStatsInfo": "0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)",
"stompSubProtocolStatsInfo": "processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)",
"stompBrokerRelayStatsInfo": "1 sessions, localhost:5672 (not available), processed CONNECT(1)-CONNECTED(0)-DISCONNECT(0)",
"clientInboundExecutorStatsInfo": "pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0",
"clientOutboundExecutorStatsInfo": "pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0",
"sockJsTaskSchedulerStatsInfo": "pool size = 1, active threads = 0, queued tasks = 1, completed tasks = 0"

经纪人详情:

2018-02-15 23:03:56.367 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hig/websocket]
2018-02-15 23:03:56.367 [XNIO-2 task-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /hig/websocket
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/hig/websocket]
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - Matching patterns for request [/hig/websocket] are [/hig/**]
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - URI Template variables for request [/hig/websocket] are 
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.w.s.s.s.WebSocketHandlerMapping - Mapping [/hig/websocket] to HandlerExecutionChain with handler [org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler@352c1b98] and 1 interceptor
2018-02-15 23:03:56.368 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/hig/websocket] is: -1
2018-02-15 23:03:56.369 [XNIO-2 task-16] DEBUG o.s.w.s.s.t.h.DefaultSockJsService - Processing transport request: GET http://localhost:8080/hig/websocket
2018-02-15 23:03:56.370 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-02-15 23:03:56.370 [XNIO-2 task-16] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
2018-02-15 23:03:56.371 [XNIO-2 task-16] DEBUG o.s.w.s.h.LoggingWebSocketHandlerDecorator - New StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.a.NativeWebSocketSession - Closing StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.h.LoggingWebSocketHandlerDecorator - StandardWebSocketSession[id=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4, uri=/hig/websocket] closed with CloseStatus[code=1002, reason=]
2018-02-15 23:03:56.374 [clientOutboundChannel-7] DEBUG o.s.w.s.m.SubProtocolWebSocketHandler - Clearing session Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4
2018-02-15 23:03:56.375 [clientOutboundChannel-8] DEBUG o.s.w.s.m.SubProtocolWebSocketHandler - No session for GenericMessage [payload=byte[0], headers=simpMessageType=OTHER, stompCommand=ERROR, nativeHeaders=message=[Broker not available.], simpSessionId=Enl3glxP29UhO95gUafWViRXrXaZn3Kv556EVxW4]

对于客户,我使用的是 angular 5

当我使用registry.enableSimpleBroker("/chat") 时,一切正常

当我启用registry.enableStompBrokerRelay("/chat")... 时,我得到:

消息:代理不可用。 内容长度:0

pom.xml:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>2.0.0.M7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
        <version>2.0.0.M7</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-net</artifactId>
        <version>2.0.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.ipc</groupId>
        <artifactId>reactor-netty</artifactId>
        <version>0.7.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.21.Final</version>
    </dependency>

【问题讨论】:

【参考方案1】:

您正在尝试在不运行 stomp 适配器的 rabbitmq 上使用 stomp 协议进行连接。 运行:

rabbitmq-plugins enable rabbitmq_stomp

启用踩踏。

另外,您正在连接到端口 5672,它是 amqp 协议/适配器。这就是为什么你有这个stompCommand=ERROR, nativeHeaders=message=[Broker not available.]。 启用后,stomp 适配器将默认侦听 tcp/61613。

【讨论】:

我的 dockerfile 显示:运行 rabbitmq-plugins enable --offline rabbitmq_web_stomp EXPOSE 15674 是否正确?

以上是关于Spring Boot Web Socket RabbitMQ 不可用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot + Web Socket 实现扫码登录,这种方式太香了!!

Spring Boot + Web Socket 打造实时监控异常,写得太好了!

Spring Boot + Web Socket 打造实时监控异常,写得太好了!

Spring Boot + Web Socket 打造实时监控异常,写得太好了!

Spring Boot + Web Socket 实现扫码登录,这种方式太香了!!

Spring Boot + Web Socket 实现扫码登录,这种方式太香了!!