当前位置: 首页 > news >正文

wordpress 在线知识库seo标题关键词优化

wordpress 在线知识库,seo标题关键词优化,网页制作基础教程第2版电子教材,wordpress手机登录代码在现代的微服务架构中,实时通信变得越来越重要。Spring Cloud Gateway作为Spring Cloud生态中的API网关,提供了动态路由、监控、弹性、安全等功能。本文将介绍如何通过Spring Cloud Gateway接入WebSocket,实现服务之间的实时通信。 为什么需…

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

为什么需要WebSocket

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

Spring Cloud Gateway配置

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

spring:cloud:gateway:discovery:locator:lowerCaseServiceId: trueenabled: trueroutes:- id: ruoyi-system2uri: lb:ws://ruoyi-systempredicates:- Path=/admin/websocket/**filters:- StripPrefix=1

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

安全配置

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

security:xss:enabled: trueexcludeUrls:- /system/notice# 不校验白名单ignore:whites:- /auth/logout- /auth/login- /auth/register- /*/v2/api-docs- /csrf- /admin/websocket/**

依赖配置

admin模块中添加WebSocket依赖:

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

WebSocket配置类

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configuration
public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter();}
}

WebSocket实现

实现WebSocket服务端:

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 = "";@OnOpenpublic 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异常");}}@OnClosepublic void onClose() {webSocketSet.remove(this);subOnlineCount();log.info("有一连接关闭!当前在线人数为" + getOnlineCount());}@OnMessagepublic void onMessage(String message, Session session) {log.info("收到来自窗口" + sid + "的信息:" + message);for (WebSocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}@OnErrorpublic 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的集成,实现实时通信功能。
在这里插入图片描述

http://www.fp688.cn/news/160820.html

相关文章:

  • 建设独立网站的公司吗写文案接单平台
  • 网站外包优化优化网站最好的刷排名软件
  • 专业建站制作有品质的网站推广公司
  • 中英网站怎么做有哪些免费推广软件
  • 哪个网站可以做日语题自媒体135免费版下载
  • 服装网站建设图如何做平台推广赚钱
  • 网站上的ar是什么软件做的香港百度广告
  • 做外贸的免费网站搜索大全浏览器
  • 个人如何做一个网站谷歌浏览器 免费下载
  • 做网站百度一下国家职业技能培训平台
  • 渭南网站制作学校steam交易链接在哪
  • 网站空间500mb关键词工具
  • 自助贸易免费建站网站搭建外贸
  • 昆明房地产网站建设百度竞价培训班
  • 网站的交流的功能怎么做广州推广优化
  • 省内注销二建 建设部网站更新慢淘宝关键词排名查询
  • 建设银行网站安全性分析网站免费网站免费
  • 在域名做网站官网建站多少钱
  • 网站关键词格式服务营销案例100例
  • 山海关网站制作网站建设 网站制作
  • 网站建设百家号搜索引擎营销策略有哪些
  • 仓库管理系统软件网站关键词优化排名
  • 汕头老城区是什么区林云seo博客
  • 上海网站建设公司哪家比较靠谱深圳小程序建设公司
  • 佛山网站建设品牌app排名优化公司
  • 海拉尔网站建设平台网络营销顾问工作内容
  • 网站建设的原则想做seo哪里有培训的
  • 东莞公司注册代办网站seo具体怎么做?
  • 做网站选哪家优化大师百科
  • 做网站卖仿品cps推广联盟