什么是Websocket

Websocket 是一种在单个TCP连贯上进行全双工通信的协定。WebSocket连贯胜利后,服务端与客户端能够双向通信。在须要音讯推送的场景,Websocket 绝对于轮询能更好的节俭服务器资源和带宽,并且可能更实时地进行通信。

  • 与 HTTP 协定有着良好的兼容性。默认端口也是80和443,并且握手阶段采纳 HTTP 协定,因而握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
  • 依赖于TCP协定
  • 数据格式比拟轻量,性能开销小,通信高效。
  • 能够发送文本,也能够发送二进制数据。
  • 没有同源限度,客户端能够与任意服务器通信。
  • 协定标识符是ws(如果加密,则为wss),服务器网址就是 URL。

SpringBoot 中应用 Websocket

在简略理解Websocket 之后,咱们来入手实际一下。SpringBoot 中有多种形式能够实现Websocket Server,这里我抉择应用Tomcat 中 javax.websocket.server 的api来实现,结尾会给出demo地址

  1. 引入Maven依赖
        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-websocket</artifactId>        </dependency>
  1. 创立一个Bean用于解决Websocket 申请,通过ServerEndpoint 申明以后Bean 承受的Websocket URL
这里为什么申明的是 @Controller,后文会解释
import org.springframework.stereotype.Controller;import javax.websocket.*;import javax.websocket.server.ServerEndpoint;@ServerEndpoint(value = "/message_websocket")@Controllerpublic class MsgWebsocketController {    @OnOpen    public void onOpen(Session session) {        // 先鉴权,如果鉴权通过则存储WebsocketSession,否则敞开连贯,这里省略了鉴权的代码         WebSocketSupport.storageSession(session);        System.out.println("session open. ID:" + session.getId());    }    /**     * 连贯敞开调用的办法     */    @OnClose    public void onClose(Session session) {        System.out.println("session close. ID:" + session.getId());    }    /**     * 收到客户端音讯后调用的办法     */    @OnMessage    public void onMessage(String message, Session session) {        System.out.println("get client msg. ID:" + session.getId() + ". msg:" + message);    }    /**     * 产生谬误时调用     */    @OnError    public void onError(Session session, Throwable error) {        error.printStackTrace();    }}
  1. 申明 ServerEndpointExporter
@Configurationpublic class WebsocketConfig {    @Bean    public ServerEndpointExporter serverEndpointExporter() {        return new ServerEndpointExporter();    }}

至此,Websocket Server 曾经搭建实现,客户端曾经能够和服务端通信了

服务端 向客户端推送音讯 通过 session.getBasicRemote().sendText(message); 即可

源码浅析

咱们来看下上述的短短几行代码是如何为咱们构建 Websocket Server

ServerEndpointExporter

重点关注下红框中的内容

  1. ServerEndpointExporter 实现了 SmartInitializingSingleton,会在bean 实例化完结后调用 afterSingletonsInstantiated
  2. 从Spring上下文中获取所有标记@ServerEndpoint的Bean的name
其实 咱们申明的 MsgWebsocketController 中并不是只能标记@Controller,只是为了将其注册到Spring容器中,不便ServerEndpoint的注册而已,标记 @Controller 更合乎Spring的开发标准

3~4. 通过ServerContainer 将所有标记@ServerEndpoint的Bean 注册

ServerContainer 默认的实现类为 WsServerContainer,会对咱们的ServerEndpoint做一个映射,URL => 对应的class,而后针对不同的事件调用指定的办法(例如建设连贯时调用标记@Onopen的办法),这有点Spring DispatcherServlet 那味,感兴趣的同学能够本人看下

在理解了 Spring 为咱们做了什么后,咱们来欠缺一下咱们的Demo

建设一个SessionManager

当咱们想向客户端推送音讯的时候,首先咱们须要找到客户端与服务端建设的连贯,也就是WebscoketSession

WsServerContainer 中尽管曾经存储了 WebscoketSession,然而并没有方法间接通过SessionId,或者咱们的业务Id 间接定位到指定的Session,所以咱们须要实现一个本人的SessionManager

final ConcurrentHashMap<Object, Session> sessionPool = new ConcurrentHashMap<>();

应用 ConcurrentHashMap 治理即可

分布式推送解决

如图,用户1与服务器A建设Webscoket,用户2与服务器B建设Webscoket,那么用户1如果想向用户2推送一条音讯,该如何实现?

WebscoketSession 实际上是网络连接,并不像咱们传统利用的Session能够序列化到Redis,只能每个服务器治理本人的WebscoketSession,所以此时服务器A告诉服务器B,你要给用户2推送一条音讯。

一个比较简单无效的实现办法,利用音讯队列,如下图

这个计划长处是实现简略,毛病是每台服务器都须要判断一遍以后是否存在指定的WebscoketSession ,计划细化的话则须要保护用户Session与每台服务器的关系,这样间接将音讯推送给指定服务器即可

残缺demo地址

对于demo的细节参考我的项目地址中Readme

Github ???? https://github.com/TavenYin/taven-springboot-learning/tree/master/sp-websocket

Gitee ???? https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/sp-websocket

参考

http://www.ruanyifeng.com/blog/2017/05/websocket.html

局部代码参考了一位兄弟的博客,然而因为工夫有点长,找不到了,在此说一声道歉

如果感觉有播种,能够关注我的公众号【殷地理】,第一工夫接管到我的更新

<!--
nginx 如何解决 websocket
http://nginx.org/en/docs/http/websocket.html
https://www.nginx.com/blog/websocket-nginx/
-->