websocket 多個項目之間通信


簡介

  使用spring boot 實現的多個微服務,其中一個項目中用websocket 與 前端實現實時通信,非該項目的服務如何實現接入websocket ?

websocket 在服務中是單例的,不同的服務之間的websocket 是不能通信,為了實現跨服務的通信,需要使用 kafka 。在其他服務中通過

kafka 生產者將消息發送給消息隊列,websocket 所在的服務中通過 kafka 消費者監聽隊列,將監聽到的消息發送給websocket ,實現跨服

務實時消息傳輸

項目配置

<dependency>
       <groupId>org.springframework.kafka</groupId>
       <artifactId>spring-kafka</artifactId>
</dependency>

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

主要的 kafka 配置在 application.yml 文件中

Github地址:https://github.com/bytecodebuffer/spring/tree/main/springboot-websocket-kafka


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM