使用 spring4 stomp 和 socksjs 应用程序配置 rabbitmq

Posted

技术标签:

【中文标题】使用 spring4 stomp 和 socksjs 应用程序配置 rabbitmq【英文标题】:Configuring rabbitmq with spring4 stomp and socksjs application 【发布时间】:2014-10-19 14:12:23 【问题描述】:

我遇到了 spring 4+Stomp+socks js 应用程序的问题。使用简单的消息代理可以正常工作,但是当切换到 rabbitmq 时,它无法正常工作,并且无法用提到的答案解决它 Configure External Broker(RabbitMQ) In Spring4+STOMP+SockJS Application

我的代码是:

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/hello">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:stomp-broker-relay prefix="/topic" system-login="guest" system-passcode="guest" client-login="guest"
                                  client-passcode="guest" relay-host="localhost" relay-port="15672"/>
</websocket:message-broker>

控制器:

    @Controller
public class StompController 

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greetUser(User user) throws Exception 
        Thread.sleep(3000); // simulated delay
        return new Greeting("Hello, " + user.getName() + "!");
    




Js:

function connect() 
            var socket = new SockJS('/hello');
            stompClient = Stomp.over(socket);
            stompClient.connect('guest','guest', function(frame) 
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/greetings', function(greeting)
                    showGreeting(JSON.parse(greeting.body).content);
                );
            );
        

连接时说:

SEVERE [clientInboundChannel-1] org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler.handleMessageInternal 消息代理未激活。忽略:[Payload byte[0]][Headers=stompCommand= CONNECT, stompCredentials=[PROTECTED], nativeHeaders=heart-beat=[10000,10000], passcode=[PROTECTED], login=[guest], accept-version=[1.1,1.0], simpMessageType=CONNECT, simpSessionAttributes= ,simpSessionId=ih_04mxa,id=d36fc1c1-e00c-fb48-51af-c526e3018e20,时间戳=1413617272621]

我什至在 rabbit mq 中启用了 stomp 插件。

【问题讨论】:

【参考方案1】:

STOMP 端口

我认为 Spring 无法连接到您的 RabbitMQ 实例,因为您将它指向端口 15672,我认为它必须是 Web UI 或其他东西的默认端口。 The default port for the STOMP connector with rabbitMQ is 61613(确实是the default value chosen by Spring)。你可以试试那个吗?

访问控制

此外,您绝对应该考虑您的访问控制配置,as the guest user won't work remotely。

【讨论】:

以上是关于使用 spring4 stomp 和 socksjs 应用程序配置 rabbitmq的主要内容,如果未能解决你的问题,请参考以下文章

Spring 4 websocket + stomp + rabbitmq 和集群

Spring 4 Web 套接字 - 我必须有一个 stomp 代理吗?

Spring 4 websocket和stomp - 未到达控制器

STOMP over websockets 与普通 STOMP。哪一个更好?

Spring 4 - websocket 消息传递 stomp 处理程序

第18章-使用WebSocket和STOMP实现消息功能