springboot+websocket+redis搭建的实现

时间:2022-09-23 11:24:14

在多负载环境下使用websocket

一、原因

在某些业务场景,我们需要页面对于后台的操作进行实时的刷新,这时候就需要使用websocket。

通常在后台单机的情况下没有任何的问题,如果后台经过nginx等进行负载的话,则会导致前台不能准备的接收到后台给与的响应。socket属于长连接,其session只会保存在一台服务器上,其他负载及其不会持有这个session,此时,我们需要使用redis的发布订阅来实现,session的共享。

二、环境准备

https://mvnrepository.com/里,查找websocket的依赖。使用springboot的starter依赖,注意对应自己springboot的版本。

springboot+websocket+redis搭建的实现

  1. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-websocket</artifactId>
  5. <version>2.2.10.RELEASE</version>
  6. </dependency>

除此之外添加redis的依赖,也使用starter版本:

  1. <!-- redis -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

三、代码

redis监听配置:

  1. /**
  2. * @description: redis监听配置类
  3. * @author:weirx
  4. * @date:2021/3/22 14:08
  5. * @version:3.0
  6. */
  7. @Configuration
  8. public class RedisConfig {
  9.  
  10. /**
  11. * description: 手动注册Redis监听到IOC
  12. *
  13. * @param redisConnectionFactory
  14. * @return: org.springframework.data.redis.listener.RedisMessageListenerContainer
  15. * @author: weirx
  16. * @time: 2021/3/22 14:11
  17. */
  18. @Bean
  19. public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) {
  20. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  21. container.setConnectionFactory(redisConnectionFactory);
  22. return container;
  23. }
  24. }

webSocket配置:

  1. /**
  2. * @description: websocket配置类
  3. * @author:weirx
  4. * @date:2021/3/22 14:11
  5. * @version:3.0
  6. */
  7. @Configuration
  8. public class WebSocketConfig {
  9.  
  10. /**
  11. * description: 这个配置类的作用是要注入ServerEndpointExporter,
  12. * 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint。
  13. * 如果是使用独立的servlet容器,而不是直接使用springboot的内置容器,
  14. * 就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。
  15. *
  16. * @return: org.springframework.web.socket.server.standard.ServerEndpointExporter
  17. * @author: weirx
  18. * @time: 2021/3/22 14:12
  19. */
  20. @Bean
  21. public ServerEndpointExporter serverEndpointExporter(){
  22. return new ServerEndpointExporter();
  23. }
  24. }

redis工具类:

  1. @Component
  2. public class RedisUtil {
  3.  
  4. @Autowired
  5. private StringRedisTemplate stringRedisTemplate;
  6.  
  7. /**
  8. * 发布
  9. *
  10. * @param key
  11. */
  12. public void publish(String key, String value) {
  13. stringRedisTemplate.convertAndSend(key, value);
  14. }
  15. }

WebSocket服务提供类:

  1. /**
  2. * description: @ServerEndpoint 注解是一个类层次的注解,
  3. * 它的功能主要是将目前的类定义成一个websocket服务器端,注解的值将被用于监听用户连接的终端访问URL地址,
  4. * 客户端可以通过这个URL来连接到WebSocket服务器端使用springboot的唯一区别是要@Component声明下,
  5. * 而使用独立容器是由容器自己管理websocket的,但在springboot中连容器都是spring管理的。
  6. *
  7. * @author: weirx
  8. * @time: 2021/3/22 14:31
  9. */
  10. @Slf4j
  11. @Component
  12. @ServerEndpoint("/websocket/server/{loginName}")
  13. public class WebSocketServer {
  14.  
  15. /**
  16. * 因为@ServerEndpoint不支持注入,所以使用SpringUtils获取IOC实例
  17. */
  18. private RedisMessageListenerContainer redisMessageListenerContainer =
  19. ApplicationContextProvider.getBean(RedisMessageListenerContainer.class);
  20.  
  21. /**
  22. * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
  23. */
  24. private static AtomicInteger onlineCount = new AtomicInteger(0);
  25.  
  26. /**
  27. * concurrent包的线程安全Set,用来存放每个客户端对应的webSocket对象。
  28. * 若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
  29. */
  30. private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
  31.  
  32. /**
  33. * 与某个客户端的连接会话,需要通过它来给客户端发送数据
  34. */
  35. private Session session;
  36.  
  37. /**
  38. * redis监听
  39. */
  40. private SubscribeListener subscribeListener;
  41.  
  42. /**
  43. * 连接建立成功调用的方法
  44. *
  45. * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
  46. */
  47. @OnOpen
  48. public void onOpen(@PathParam("loginName") String loginName, Session session) {
  49. this.session = session;
  50. //加入set中
  51. webSocketSet.add(this);
  52. //在线数加1
  53. addOnlineCount();
  54. log.info("有新连接[" + loginName + "]加入!当前在线人数为{}", getOnlineCount());
  55. subscribeListener = new SubscribeListener();
  56. subscribeListener.setSession(session);
  57. //设置订阅topic
  58. redisMessageListenerContainer.addMessageListener(
  59. subscribeListener, new ChannelTopic(Constants.TOPIC_PREFIX + loginName));
  60.  
  61. }
  62.  
  63. /**
  64. * 连接关闭调用的方法
  65. */
  66. @OnClose
  67. public void onClose() throws IOException {
  68. //从set中删除
  69. webSocketSet.remove(this);
  70. //在线数减1
  71. subOnlineCount();
  72. redisMessageListenerContainer.removeMessageListener(subscribeListener);
  73. log.info("有一连接关闭!当前在线人数为{}", getOnlineCount());
  74. }
  75.  
  76. /**
  77. * 收到客户端消息后调用的方法
  78. *
  79. * @param message 客户端发送过来的消息
  80. * @param session 可选的参数
  81. */
  82. @OnMessage
  83. public void onMessage(String message, Session session) {
  84. log.info("来自客户端的消息:{}", message);
  85. //群发消息
  86. for (WebSocketServer item : webSocketSet) {
  87. try {
  88. item.sendMessage(message);
  89. } catch (IOException e) {
  90. log.info("发送消息异常:msg = {}", e);
  91. continue;
  92. }
  93. }
  94. }
  95.  
  96. /**
  97. * 发生错误时调用
  98. *
  99. * @param session
  100. * @param error
  101. */
  102. @OnError
  103. public void onError(Session session, Throwable error) {
  104. log.info("发生错误,{}", error);
  105. }
  106.  
  107. /**
  108. * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
  109. *
  110. * @param message
  111. * @throws IOException
  112. */
  113. public void sendMessage(String message) throws IOException {
  114. this.session.getBasicRemote().sendText(message);
  115. }
  116.  
  117. public int getOnlineCount() {
  118. return onlineCount.get();
  119. }
  120.  
  121. public void addOnlineCount() {
  122. WebSocketServer.onlineCount.getAndIncrement();
  123. }
  124.  
  125. public void subOnlineCount() {
  126. WebSocketServer.onlineCount.getAndDecrement();
  127. }
  128.  
  129. }

redis消息发布:

  1. @Autowired
  2. private RedisUtil redisUtil;
  3.  
  4. @Override
  5. public Result send(String loginName, String msg) {
  6. //推送站内信webSocket
  7. redisUtil.publish("TOPIC" + loginName, msg);
  8. return Result.success();
  9. }

前端vue代码:

  1. <template>
  2. <div class="dashboard-container">
  3. <div class="dashboard-text">消息内容: {{ responseData }}</div>
  4. </div>
  5. </template>
  6.  
  7. <script>
  8. import {mapGetters} from 'vuex'
  9.  
  10. export default {
  11. data() {
  12. return {
  13. websocket: null,
  14. responseData: null
  15. }
  16. },
  17. created() {
  18. this.initWebSocket();
  19. },
  20. destroyed() {
  21. this.websock.close() //离开路由之后断开websocket连接
  22. },
  23. methods: {
  24. //初始化websocket
  25. initWebSocket() {
  26. const wsUri = "ws://127.0.0.1:21116/websocket/server/" + "admin";
  27. this.websock = new WebSocket(wsUri);
  28. this.websock.onmessage = this.websocketonmessage;
  29. this.websock.onopen = this.websocketonopen;
  30. this.websock.onerror = this.websocketonerror;
  31. this.websock.onclose = this.websocketclose;
  32. },
  33. websocketonopen() { //连接建立之后执行send方法发送数据
  34. let actions = {"用户账号": "admin"};
  35. this.websocketsend(JSON.stringify(actions));
  36. },
  37. websocketonerror() {//连接建立失败重连
  38. this.initWebSocket();
  39. },
  40. websocketonmessage(e) { //数据接收
  41. const redata = JSON.parse(e.data);
  42. this.responseData = redata;
  43. },
  44. websocketsend(Data) {//数据发送
  45. this.websock.send(Data);
  46. },
  47. websocketclose(e) { //关闭
  48. console.log('断开连接', e);
  49. },
  50.  
  51. },
  52. name: 'Dashboard',
  53. computed: {
  54. ...mapGetters([
  55. 'name',
  56. 'roles'
  57. ])
  58. }
  59. }
  60. </script>

四、测试

springboot+websocket+redis搭建的实现

发送前

springboot+websocket+redis搭建的实现

发送后

到此这篇关于springboot+websocket+redis搭建的实现的文章就介绍到这了,更多相关springboot websocket redis搭建内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.jianshu.com/p/ac197bca9aed