gateway 整合 websocket demo
背景: 这个websocket 因为使用的地方不多,并没有独立出一个项目,是集成在已有的服务中。
1: gateway 配置
- id: service-test
uri: lb:ws://service-test
predicates:
- Path=http://www.mamicode.com/ws/**
filters:
- StripPrefix=1
注释:
lb:ws: 表示要转发websocket协议
Path=http://www.mamicode.com/ws/** 表示请求path 是 ip:port/ws/** 请求,,会转发到 id 为 service-test 的服务
StripPrefix=1 过滤掉/ws的路径
ok 启动 gateway , 我的gateway 端口是 7002
2: service-test 服务配置
pom.xml 添加 websocket的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
配置代码(这里只展示连接的代码,其他的可以看下文档)
@ServerEndpoint("/webSocket")@Component
public class WebSocketStart {
private static Map<String, List<SessionInfo>> sessionMap = Maps.newConcurrentMap();
/**
* 连接
*/
@OnOpen
public void connected(Session session) {
session.getAsyncRemote().sendText("小伙子登陆成功!!!");
// cacheSession(session);
}
}
ok 启动 service-test , 我的 service-test 端口是 8091
先测试下 service-test 是否能连接成功。
再来测试下 gateway
gateway 整合 websocket demo
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/41892.html
- 上一篇:express学习之路(一)
- 下一篇:js 中文转拼音