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

浙公网安备 33010602011771号