Spring 4+Stomp WebSocket+RabbitMQ 3.6.5+Tomcat 8搭建WebSocket服务

来源:互联网 发布:慢镜头软件下载 编辑:程序博客网 时间:2024/05/01 13:00

本次用的版本为Spring 4.3.0.Release、Tomcat 8、RabbitMQ 3.6.5。

首先应该先安装RabbitMQ服务器,最好是最新版的,并且启动Rabbit stomp插件:

rabbitmq-plugins enable rabbitmq_stomp

可以用rabbitmq-plugins list命令来查看插件是否已经启动。需要注意的是该插件默认监听的端口是61613,而不是rabbit server的默认端口(5672)。所以在Spring中不需要配置消息服务器的连接端口(默认就是61613):

@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{    @Override    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {        //加入setAllowedOrigins("*")防止浏览器出现403 Forbidden的问题        stompEndpointRegistry.addEndpoint("/pay-result").setAllowedOrigins("*").withSockJS();    }    @Override    public void configureMessageBroker(MessageBrokerRegistry registry) {        registry.enableStompBrokerRelay("/topic", "/queue");        registry.setApplicationDestinationPrefixes("/app");    }}

如果端口配置错误,在web程序启动和运行期间会一直出现ERROR StompBrokerRelayMessageHandler - TCP connection failure in session _system_: Transport failure: Connection reset by peer
这种错误:

2016-10-19 18:47:20 ERROR StompBrokerRelayMessageHandler - TCP connection failure in session _system_: Transport failure: Connection reset by peerjava.io.IOException: Connection reset by peer    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)    at sun.nio.ch.IOUtil.read(IOUtil.java:192)    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:366)    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:651)    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:574)    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:488)    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:450)    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)    at java.lang.Thread.run(Thread.java:745)2016-10-19 18:47:25 ERROR StompBrokerRelayMessageHandler - TCP connection failure in session _system_: Transport failure: Connection reset by peerjava.io.IOException: Connection reset by peer    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)    at sun.nio.ch.IOUtil.read(IOUtil.java:192)    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:366)    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:651)    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:574)    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:488)    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:450)    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)    at java.lang.Thread.run(Thread.java:745)2016-10-19 18:47:30 ERROR StompBrokerRelayMessageHandler - TCP connection failure in session _system_: Transport failure: Connection reset by peerjava.io.IOException: Connection reset by peer    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)    at sun.nio.ch.IOUtil.read(IOUtil.java:192)    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:366)    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:651)    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:574)    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:488)    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:450)    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)    at java.lang.Thread.run(Thread.java:745)2016-10-19 18:47:33 INFO  WebSocketMessageBrokerStats - WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[1 sessions, 127.0.0.1:5672 (not available), processed CONNECT(1)-CONNECTED(0)-DISCONNECT(0)], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]2016-10-19 18:47:35 ERROR StompBrokerRelayMessageHandler - TCP connection failure in session _system_: Transport failure: Connection reset by peerjava.io.IOException: Connection reset by peer    at sun.nio.ch.FileDispatcherImpl.read0(Native Method)    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)    at sun.nio.ch.IOUtil.read(IOUtil.java:192)    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:366)    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:118)    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:651)    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:574)    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:488)    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:450)    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)    at java.lang.Thread.run(Thread.java:745)

另外,因为需要在Stomp协议中使用RabbitMQ作为消息代理,所以还需要加入以下三个依赖:

<dependency>            <groupId>io.projectreactor</groupId>            <artifactId>reactor-core</artifactId>            <version>2.0.8.RELEASE</version>        </dependency>        <dependency>            <groupId>io.projectreactor</groupId>            <artifactId>reactor-net</artifactId>            <version>2.0.8.RELEASE</version>        </dependency>        <dependency>            <groupId>io.netty</groupId>            <artifactId>netty-all</artifactId>            <version>4.1.6.Final</version>        </dependency>

如果浏览器出现403 Forbidden问题,而且返回的响应头如图所示:

这里写图片描述

说明是浏览器跨域的问题,需要在registerStompEndpoints中加上setAllowedOrigins("*")

0 1