Solon rpc 之 SocketD 协议系列
Solon rpc 之 SocketD 协议 - 概述
Solon rpc 之 SocketD 协议 - 消息上报模式
Solon rpc 之 SocketD 协议 - 消息应答模式
Solon rpc 之 SocketD 协议 - 消息订阅模式
Solon rpc 之 SocketD 协议 - RPC调用模式
Solon rpc 之 SocketD 协议 - 单连接双向RPC模式
Solon rpc 之 SocketD 协议 - 消息加密模式html
SocketD 是一种二进制的点对点通讯协议,是一种新的网络通讯第七层协议。旨在用于分布式应用程序中。从这个意义上讲,SocketD能够是RSocket等其余相似协议的替代方案。它的消息协议规范具备异步,背压的双向,多路复用,断线重连,基于消息等特性。暂时只提供Java实现,目前作为Solon rpc的sockte通道协议。java
本案以简单的消息加密模式为例演示:(在消息上报模式的基础上,增长加密演示)git
<dependency> <groupId>org.noear</groupId> <artifactId>solon.boot.socketd.smartsocket</artifactId> <version>1.2.18</version> </dependency>
//启动服务端 public class ServerApp { public static void main(String[] args) { //启动Solon容器(SocketD bean&plugin 由solon容器管理) Solon.start(ServerApp.class, args, app -> { app.enableSocketD(true); //设置增强协议,使用加密协议(客户端也要一同设置) SocketD.setProtocol(new MessageProtocolSecret() { @Override public byte[] encrypt(byte[] bytes) throws Exception { return EncryptUtils.aesEncrypt(bytes,"pLft36Ok5zfmP6zI"); } @Override public byte[] decrypt(byte[] bytes) throws Exception { return EncryptUtils.aesDecrypt(bytes,"pLft36Ok5zfmP6zI"); } }); }); } } //定义服务端监听 @ServerEndpoint public class ServerListener implements Listener { @Override public void onOpen(Session session) { System.out.println("有客户端链上来喽..."); } @Override public void onMessage(Session session, Message message) { //收到消息,作业务处理 if(message.flag() != MessageFlag.heartbeat){ System.out.println("服务端:我收到心跳"); }else { System.out.println("服务端:我收到:" + message.bodyAsString()); } } }
//启动客户端 public class ClientApp { public static void main(String[] args) throws Throwable { //启动Solon容器(SocketD bean&plugin 由solon容器管理) Solon.start(ClientApp.class, args, app->{ //设置增强协议,使用加密协议(服务端也要一同设置) SocketD.setProtocol(new MessageProtocolSecret() { @Override public byte[] encrypt(byte[] bytes) throws Exception { return EncryptUtils.aesEncrypt(bytes,"pLft36Ok5zfmP6zI"); } @Override public byte[] decrypt(byte[] bytes) throws Exception { return EncryptUtils.aesDecrypt(bytes,"pLft36Ok5zfmP6zI"); } }); }); //建立会话(若是后端是WebSocekt,协议头为:ws) Session session = SocketD.createSession("tcp://localhost:28080"); //设定30秒自动上发心跳(若是断开了,也尝试自动重链) session.sendHeartbeatAuto(30); //上报消息 session.send("Helloworld server!"); } }
https://gitee.com/noear/solon_demo/tree/master/demo28.solon_socketd_message后端