模拟一个广播弹幕的websocket。gateway通过eureka注册中心拉取服务进行转发websocket
1.搭建 Spring WebSocket
1.1 pom.xml websocket maven依赖
1.2 application.yml 配置文件
spring:application:name: bulletserver:port: 5678eureka:client:serviceUrl:defaultZone: http://localhost:1025/eureka/
1.3 BulletApplication websocket启动程序
@SpringBootApplication public class BulletApplication { public static void main(String[] args) { SpringApplication.run(BulletApplication.class, args); } }
1.4 WebSocketAutoConfig websocket配置类
@Configuration @EnableWebSocketMessageBroker public class WebSocketAutoConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/bullet") //开启/bullet端点 .setAllowedOrigins("*") //允许跨域访问 .withSockJS(); //使用sockJS } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/toAll"); //订阅Broker名称 } }
1.5 BulletMessageVO类
使用lombok的@Getter和@Setter注解来自动生成get、set方法
@Getter@Setterpublic class BulletMessageVO {String username;String message;}
1.6 BulletController websocket控制器
@Controller public class BulletController { @MessageMapping("/chat") @SendTo("/toAll/bulletScreen") //SendTo 发送至 Broker 下的指定订阅路径 public String say(BulletMessageVO clientMessage) { String result=null; if (clientMessage!=null){ result=clientMessage.getUsername()+":"+clientMessage.getMessage(); } return result; } }
2 搭建 Spring Cloud Gateway
2.1 pom.xml gateway网关maven依赖
2.2 application.yml gateway网关配置文件
spring:application:name: gatewaygateway:routes:- id: bulletscreen# 重点!/info必须使用http进行转发,lb代表从注册中心获取服务uri: lb://bulletpredicates:# 重点!转发该路径!- Path=/bullet/info/**- id: bulletscreen# 重点!lb:ws://代表从注册中心获取服务,并且转发协议为websocket,这种格式怀疑人生!uri: lb:ws://bulletpredicates:# 转发/bullet端点下的所有路径- Path=/bullet/**server:port: 8888eureka:client:serviceUrl:defaultZone: http://localhost:1025/eureka/
2.3 GatewayApplication gateway启动类
@SpringCloudApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
3 编写html
这个是从网上找来的html代码,可以保存到本地文件,不需要放入服务里面
4 启动程序
启动Eureka 服务端,开启注册中心启动Bullet WebSocket程序启动GateWay网关
5 测试程序
开启多个html页面,并打开控制台 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CIoFSk8j-1661298547168)(http://springcloud-new.oss-cn-shenzhen.aliyuncs.com/fa80300e03825afecdedee1a8cb7fab7.png?Expires=1849511713&OSSAccessKeyId=LTAI57F52hRuWq3h&Signature=2P5g2foC%2F6rkuEq%2FCnKKJd2H0Qo%3D)] 在多个页面中点击连接按钮,观察控制台是否连接成功 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n7MIflec-1661298547169)(http://springcloud-new.oss-cn-shenzhen.aliyuncs.com/11b4351711bbbae6ebcb58abb65e2054.png?Expires=1849511792&OSSAccessKeyId=LTAI57F52hRuWq3h&Signature=8n7lwgy6HZHyyLSrS603A2i0q7I%3D)] 输入名字和消息,观察是否成功进行广播
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1tF7q1ci-1661298547170)(http://springcloud-new.oss-cn-shenzhen.aliyuncs.com/393a3cc1c89db0c1038202204553910c.png?Expires=1849511842&OSSAccessKeyId=LTAI57F52hRuWq3h&Signature=fPfLl%2FPgl30rdPOLM6dPNIaKRh0%3D)]
6 源码
https://github.com/naah69/SpringCloud-Gateway-WebSocket-Demo
)
相关链接
发表评论