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
- Spring 4+Stomp WebSocket+RabbitMQ 3.6.5+Tomcat 8搭建WebSocket服务
- Spring websocket stomp
- 利用spring-websocket包搭建websocket服务
- 使用spring-websocket包搭建websocket服务
- 利用spring-websocket包搭建websocket服务
- Spring websocket over STOMP使用指南
- 【JAVA-Spring】利用spring-websocket包来搭建websocket服务
- Stomp Over Websocket的Spring实现
- Spring boot WebSocket stomp简单例子
- 使用maven搭建XML风格的Spring MVC+WebSocket+sockjs+STOMP+ActiveMQ应用
- 使用maven搭建XML风格的Spring MVC+WebSocket+sockjs+STOMP+ActiveMQ应用
- WebSocket与STOMP介绍
- STOMP Over WebSocket原文
- STOMP-WebSocket中文文档
- WebSocket+STOMP+SockJS
- websocket 、stomp关系
- spring websocket简单搭建
- spring mvc 4整合spring websocket,点对点式通讯,stomp监听连接
- 工作流适用场景
- 手摸手教你用 Scene 完成炫酷的注册登录动画
- 看到比较有用的博文汇总
- 字符串反转
- 认真学spring官网,很容易找到spring4的jar包下载位置
- Spring 4+Stomp WebSocket+RabbitMQ 3.6.5+Tomcat 8搭建WebSocket服务
- 图片流量节省大杀器:基于CDN的sharpP自适应图片技术实践
- 216. Combination Sum III
- ZeusPlugin 插件框架热修复 Gradle 插件源码
- 超图三维管线常见问题
- std::bind和std::function详解
- 一、Android 解决在ListView Item 中Edittext数据显示混乱
- Android Studio中Dex分包方案
- Eclipse安装Activiti-Designer插件