【问题标题】:"message:Broker not available." error when implementing stomp over spring websocket“消息:经纪人不可用。”在spring websocket上实现stomp时出错
【发布时间】:2020-02-03 17:15:03
【问题描述】:

我正在使用 spring WebSocket、SockJs 和 Amazon MQ 构建一个示例聊天应用程序。当客户端订阅该主题时,它会引发“代理不可用”异常。所有入站流量规则都在 AWS 安全组中正确设置,并且代理也有 stomp 支持。我正在关注这个Spring Guide

如果我使用内存中的代理,它可以正常工作。非常感谢您对此提供的帮助,以下是示例代码。

代理:Amazon MQ(内部使用 Active MQ)

版本:5.15.0

WebSocketConfig.java

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {

    registry.enableStompBrokerRelay("/topic")
            .setRelayHost("***********.mq.us-east-2.amazonaws.com").setRelayPort(61614)
            .setClientLogin("******").setClientPasscode("*****");

    registry.setApplicationDestinationPrefixes("/app");

}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

    registry.addEndpoint("/chat-endpoint").withSockJS();
}

应用程序启动日志

.......
INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Starting...

INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Starting "system" session, StompBrokerRelay[ReactorNettyTcpClient[reactor.netty.tcp.TcpClientDoOn@7acb7b3e]]

INFO 14280 --- [alina-utility-1] o.s.m.s.s.StompBrokerRelayMessageHandler : Started.
......

客户

var socket = new SockJS('/chat-endpoint');
    stompClient = Stomp.over(socket);

    stompClient.connect({}, function(frame) {

        setConnected(true);
        stompClient.subscribe('/topic/message', function(message) {
                                   displayMessage(message); });

});

浏览器控制台日志

正在打开 Web Socket... Web Socket 已打开... CONNECT accept-version:1.1,1.0 heart-beat:10000,10000

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

stomp.min.js:8 哎呀!失去连接到 http://localhost:8080/testApp/chat-endpoint

【问题讨论】:

    标签: spring spring-websocket stomp sockjs amazon-mq


    【解决方案1】:

    我遇到了同样的问题。为了修复它,我稍微更改了 configureMessageBroker 方法:

        @Override
        public void configureMessageBroker(MessageBrokerRegistry registry) {
            ReactorNettyTcpClient<byte[]> client = new ReactorNettyTcpClient<>(tcpClient -> tcpClient
                    .host("your-amazon-mq-host.amazonaws.com")
                    .port(61614)
                    .secure(SslProvider.defaultClientProvider()), new StompReactorNettyCodec());
    
            registry.setApplicationDestinationPrefixes("/app");
            registry.enableStompBrokerRelay("/queue", "/topic")
                    .setAutoStartup(true)
                    .setSystemLogin("amazonmq-login")
                    .setSystemPasscode("amazonmq-pass")
                    .setClientLogin("amazonmq-login")
                    .setClientPasscode("amazonmq-pass")
                    .setTcpClient(client);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多