Spring Cloud Gateway接入WebSocket:实现实时通信

在现代的微服务架构中,实时通信变得越来越重要。Spring Cloud Gateway作为Spring Cloud生态中的API网关,提供了动态路由、监控、弹性、安全等功能。本文将介绍如何通过Spring Cloud Gateway接入WebSocket,实现服务之间的实时通信。

为什么需要WebSocket

WebSocket提供了全双工通信机制,允许服务器主动向客户端发送消息,这在需要实时数据推送的场景(如聊天应用、实时通知等)中非常有用。

Spring Cloud Gateway配置

首先,我们需要在Spring Cloud Gateway中配置WebSocket路由。以下是配置示例:

yaml 复制代码
spring:
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        - id: ruoyi-system2
          uri: lb:ws://ruoyi-system
          predicates:
            - Path=/admin/websocket/**
          filters:
            - StripPrefix=1

这里配置了一个WebSocket路由,将/admin/websocket/**路径的请求转发到ruoyi-system服务。

安全配置

为了确保WebSocket通信的安全,我们还需要进行一些安全配置:

yaml 复制代码
security:
  xss:
    enabled: true
    excludeUrls:
      - /system/notice
  # 不校验白名单
  ignore:
    whites:
      - /auth/logout
      - /auth/login
      - /auth/register
      - /*/v2/api-docs
      - /csrf
      - /admin/websocket/**

依赖配置

admin模块中添加WebSocket依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

WebSocket配置类

创建一个配置类,启用WebSocket支持:

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

WebSocket实现

实现WebSocket服务端:

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/websocket/{sid}")
@Component
@Slf4j
public class WebSocketServer {
    private static int onlineCount = 0;
    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>();

    private Session session;
    private String sid = "";

    @OnOpen
    public void onOpen(Session session, @PathParam("sid") String sid) {
        this.session = session;
        webSocketSet.add(this);
        addOnlineCount();
        log.info("有新窗口开始监听:" + sid + ", 当前在线人数为" + getOnlineCount());
        this.sid = sid;
        try {
            sendMessage("连接成功");
        } catch (IOException e) {
            log.error("websocket IO异常");
        }
    }

    @OnClose
    public void onClose() {
        webSocketSet.remove(this);
        subOnlineCount();
        log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到来自窗口" + sid + "的信息:" + message);
        for (WebSocketServer item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }

    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    public static void sendInfo(String message, @PathParam("sid") String sid) throws IOException {
        log.info("推送消息到窗口" + sid + ",推送内容:" + message);
        for (WebSocketServer item : webSocketSet) {
            try {
                if (sid == null) {
                    item.sendMessage(message);
                } else if (item.sid.equals(sid)) {
                    item.sendMessage(message);
                }
            } catch (IOException e) {
                continue;
            }
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }

    public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() {
        return webSocketSet;
    }
}

测试工具

可以使用WebSocket在线测试工具进行测试。

测试步骤

  1. 通过网关连接:

    复制代码
    ws://127.0.0.1:8080/admin/websocket/123
  2. 直接连接服务:

    复制代码
    ws://127.0.0.1:9201/websocket/123

通过以上步骤,可以实现Spring Cloud Gateway与WebSocket的集成,实现实时通信功能。

相关推荐
无奈笑天下21 小时前
银河麒麟高级服务器操作系统【双网卡绑定之bond0】操作方法
linux·运维·服务器·网络·经验分享
飞Link21 小时前
【网络与 AI 工程的交叉】多模态模型的数据传输特点:视频、音频、文本混合通道
网络·人工智能·音视频
一执念21 小时前
网络和互联网通信的本质
网络
老蒋新思维21 小时前
创客匠人峰会实录:知识变现的场景化革命 —— 创始人 IP 如何在垂直领域建立变现壁垒
网络·人工智能·tcp/ip·重构·知识付费·创始人ip·创客匠人
老蒋新思维21 小时前
创客匠人峰会深度解析:智能体驱动知识变现的数字资产化路径 —— 创始人 IP 的长期增长密码
人工智能·网络协议·tcp/ip·重构·知识付费·创始人ip·创客匠人
M158227690551 天前
六通道 CAN 集线器在消防报警主机系统中的应用方案
网络
盈创力和20071 天前
当抱杆箱也上云:如何用 LoRa/NB-IoT 打造一个会“告警”的智能户外电气箱?
网络·物联网
MonkeyKing_sunyuhua1 天前
国内Dockerfile的配置,提高打包速度
linux·运维·网络
盛世宏博智慧档案1 天前
数据追溯 + 异常预警:机柜温湿度以太网变送器应用实践方案
linux·服务器·网络
老蒋新思维1 天前
创客匠人峰会实录:创始人 IP 变现的 “人 + 智能体” 协同范式 —— 打破知识变现的能力边界
大数据·网络·人工智能·网络协议·tcp/ip·创始人ip·创客匠人