什么是协议
从生活角度去理解:协议大部分情况下是指两个或两个以上实体为了开展某项活动,经过协商后双方达成的一致意见。例如租房合同协议、劳动合同协议等。
从互联网角度去理解:协议是指网络通信的参与方必须遵循相同的规则,这套规则称为协议,它最终体现为在网络上传输的数据包的格式。例如TCP、FTP、AMQP、HTTP协议等。
目前互联网上通信协议基本底层都是基于TCP协议作为网络通信的基础进行再次封装的。
什么是TCP私有协议
TCP是目前整个互联网的传输层通信协议,它的中文全称是:传输控制协议(TCP,Transmission Control Protocol),它是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793 [1] 定义。
目前主流的公有协议或私有协议基本上都是基于TCP/IP协议基础上进行实现的,例如大名鼎鼎的万维网HTTP协议、消息队列AMQP协议、SMTP协议等都是工作在 TCP/IP协议族上的公有协议。
而TCP私有协议通常是指公司内部基于TCP协议基础上定制的一套内部协议,私有协议通常具有灵活性高,按实际场景的需要进行定制,同时由于私有协议的封闭性,从而使私有协议天生具备安全性。
私有协议使用场景
定制一套成熟的、完善的私有协议通常需要具备较高软件开发水平的人员参与实现,所以是需要投入一定成本的,不到必要场景时,并不会优先去考虑自研内部私有协议。下面说一下什么场景下可以考虑定制私有协议:
1、公司高层战略上有计划走自研路线,这种情况下一般会投入一定的成本来支持自研需要的成本。
2、在分布式环境下,对网络通信数据传输过程有较高的安全性时,就有必要考虑制定私有协议,通过制定私有协议可以对数据按照一定的算法进行加解密。
3、在分布式环境下,对网络通信性能方面有极致的要求时,可以考虑定制TPC私有协议来替代常用HTTP协议,由于TCP工作在OSI第四层传输层,相比HTTP工作在OSI第七层应用层来说,性能消耗要低。
4、其他涉及全双工(客户端可以发送数据给服务端,服务端也可以主动发数据给客户端)网络编程的需求,例如在线聊天等场景。
小T协议总体设计
在开始设计协议前,这里先给本次协议取个名称:小T协议,方面后面称呼。
设计目标
Ø 支持高并发场景。
Ø 支持高性能要求。
Ø 支持自定义Java-Bean序列化和反序列。
Ø 支持自定义安全准入机制。
Ø 支持连接心跳检测机制。
Ø 支持断连重试重发机制。
设计原则
Ø 可靠性原则
Ø 安全性原则
Ø 可扩展性原则
技术选项
Ø 开发语言:JAVA
Ø 网络I/O模型:NIO
Ø 网络编程框架:Netty
Ø 项目构建:Maven
Ø 编码工具:社区版IDEA
Ø 日志框架:log4j
通信模型
通信流程如下:
1、底层TCP协议三次握手成功之后,客户端开始发起私有协议的握手请求到服务端,消息头command指令为1。
2、服务端接收到客户端握手请求时,进行安全认证,认证通过后返回握手应答,消息头command指令为2。
3、客户端接收到服务端握手应答后,创建定时发送心跳任务,并间隔5s发送一次心跳请求给服务端,消息头command指令为5。
4、服务端接收到心跳请求后,立即应答心跳,消息头command指令为6。
5、客户端可以发送业务消息给服务端,服务端接收到客户端的业务消息后进行处理,处理结束后可以根据实际情况是否需要应答消息给客户端。
协议定义
协议的定义主要是定义消息格式,小T协议整体上分为两部分:
Ø 消息头(header)
Ø 消息体(body)
每个部分具体定义如下表格所示:
表 1-1 小T协议数据格式定义表(Message):
字段 |
类型 |
长度(bit) |
说明 |
header |
Header |
不定长 |
消息头定义 |
body |
Object |
不定长 |
消息体定义 |
表 1-2 小T协议数据格式消息头定义表(Header):
字段 |
类型 |
长度(bit) |
说明 |
magic |
int |
32 |
魔数标识。 固定值:0xACAFDCBA,4个字节 |
mainVersion |
char |
16 |
主版本。 1-9,2个字节 |
minorVersion |
char |
16 |
次版本。 1-9,2个字节 |
length |
long |
64 |
消息长度,包括header+body |
sid |
long |
64 |
集群节点全局唯一 |
command |
char |
16 |
1、握手请求消息 2、握手应答消息 3、业务请求消息 4、业务应答消息 5、心跳请求消息 6、心跳应答消息 |
attribute |
Map |
不定长 |
消息头扩展属性 |
协议编码
小T协议编码通过继承Netty的消息转字节数组类,该类在消息出站时(write)会判断消息泛型类型是否匹配,如果匹配则调用子类实现的encode方法将对象编码为字节数组进行网络传输,这里先通过JSON将对象解析为字符串,然后得到字符串的字节数组。
注意,编码时我们将整个对象的字节数组长度通过Netty缓冲对象ByteBuf的writeInt方法写在最开始位置,这样做方便后面解码时先读出这个长度,通过这个长度从ByteBuf种读取对象字节内容进行解密。
协议解码
通过继承Netty的LengthFieldBasedFrameDecoder解码类在消息入站时进行解码。解码方式先通过ByteBuf的readInt方法读出小子字节的长度,然后在通过这个长度值从ByteBuf读取消息对象字节内容进行解码,这里均为通过JSON反解析为消息对象类。
TCP粘包和拆包
粘包和拆包是面向应用层而言的,我们通常把应用层如HTTP每次请求或响应/或SMTP每次发送的邮件看作是一个消息包,而TCP是基于字节流进行传输的,它把应用层发送的所有消息包看作是一连串没有边界的字节流进行传输,TCP为了方便网络优化,在发送端和接收端都设有缓冲区,以及TCP更底层的数据链路层的MSS(MTU-40通常为1460)大小限制,导致TCP在发送端和接收端都可能造成粘包和拆包现象。下面简单分析产生粘包和拆包的情况:
1、当发送/接收的消息包字节流大小超过TCP发送/接收端缓冲区剩余空间时,将会发生拆。
2、当发送/接收的消息包字节流大小小于TCP发送/接收端缓冲区剩余空间时,将会发生粘包。
3、当发送的消息包字节流大小超过MSS限制时,将会发生拆包。
4、当发送端启用Nagle算法时,会加大粘包和拆包的概率。
5、当TCP时间滑动窗口的变化也会加大粘包和拆包的发生概率。
由于TCP不关心业务,所以粘包和拆包需要在应用层去处理,大致有3种解决方式:
1、固定消息包长度:限制发送端发送的单条消息固定长度,不足的空位补齐,接收端每次读取固定长度的字节作为一条完整消息。
2、协定消息结束符:发送端在每个消息包末尾增加一个结束符(例如换行符)来表示一条完整消息的结束位置,这样接收端可以根据这个结束符来判断消息包是否接收完成。如果没有接收完成需要接收端本地缓存当前的部分内容,等待剩余部分的字节流到来。
3、消息头长度标识:通过把每个消息包的长度放到消息头上,接收端获取消息头的时候解析出消息长度,然后向后读取该长度的内容,不够的仍然需要本地缓存当前的部分内容,等待剩余部分的字节流到来。
Netty 框架中对 TCP 粘包拆包有现成的解决方案,只要在其 pipline 中加入对应的解码器就可实现(通常需要自己实现编解码器,解码器通过继承的方式即可具备解决能力)。
Ø LineBasedFrameDecoder:协定消息结束符方式(默认固定换行符)。
Ø DelimiterBasedFrameDecoder:协定消息结束符方式(可自定义结束符)。
Ø FixedLengthFrameDecoder:固定消息包长度方式。
Ø LengthFieldBasedFrameDecoder:消息头长度标识方式(小T协议采用这种方式)。
注意,Netty自带的TCP粘包和拆包解决方案是通过解码阶段去处理的,所以为了解码阶段处理正确,实现自定义编码时必须严格按照解码阶段使用和配置的粘包和拆包处理方案来编码。
建立连接
正常情况下先启动小T协议服务端,然后客户端通过异步线程方式启动小T协议客户端进行连接请求,如果连接建立成功将由客户端发起小T协议设计的握手请求到服务端,如果连接建立失败将由客户端重试机制进行定时重连。
断连重试
断开重试流程如下:
1、客户端启动时设置20s读超时事件监听器。
2、当服务端进程异常退出时,由于Netty的write或writeAndFlush都是往本地内存缓冲写,缓冲满了批量发送出去,所以客户端启动时设置了20s读超时事件监听器,所以客户端超过20s没有接收到读请求时,会触发超时监听事件而端口连接。
3、当客户端连接断开时,每间隔5s尝试重连一次,直到连接成功为止。
关闭连接
当发生网络异常、握手失败、长时间没接收到服务端心跳应答时(客户端读超时),客户端将关闭连接资源,并定义重连。
握手机制
协议连接建立成功后由客户端发起握手请求,服务器接收握手请求后首先检查客户端消息头的主次版本是否和服务端一致,魔数值是否正常,以及进行安全检查,如果检查不通过则返回握手失败标记位:0返回客户端,客户端接收到握手失败标记位时会打印日志并断开连接进行重试任务。如果检查通过服务端会返回握手应答指令command=2,和成功标记位:1给客户端,客户端接收握手成功后会进行下一步心跳任务的建立。
心跳检查
当客户端和服务端握手成功后,由客户端启动定时发送心跳检查任务,每间隔5s发送一次心跳包给服务端,服务端接收到心跳时将回给客户端应答,如果服务端发送异常或网络问题没有应答心跳包给客户端,将触发客户端读超时而异常方式断开连接进行重连任务。
安全机制
基于JDK自带的SPI技术支持自定义安全机制扩展。在客户端发起握手请求时由服务端执行安全检查,检查通过应答握手成功标记位:1,否则应答握手失败标记位:0。客户端接收到握手失败标记位时将打印日志并断开连接进行重连任务。
业务处理
基于JDK自带的SPI技术实现协议业务处理的自定义扩展开发,默认将接收到的业务请求回调XiaotBizRespCallbackProvide接口execute方法。协议使用者可以基于JDK自带的SPI机制在项目resources目录下创建META-INF/services目录,创建对应接口的实现类方式进行扩展,示例截图如下:
小T协议源码开发
下面给出小T协议完整源码内容。
整体项目结构
pom.xml配置
- project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- modelVersion>4.0.0modelVersion>
- parent>
- groupId>org.springframework.bootgroupId>
- artifactId>spring-boot-starter-parentartifactId>
- version>2.5.0version>
- relativePath/>
- parent>
- groupId>com.xiaot.protocolgroupId>
- artifactId>xiaot-protocolartifactId>
- version>0.0.1-SNAPSHOTversion>
- name>xiaot-protocolname>
- description>XiaoT-Protocol project for Spring Bootdescription>
- properties>
- java.version>1.8java.version>
- properties>
-
-
- dependencies>
- dependency>
- groupId>org.projectlombokgroupId>
- artifactId>lombokartifactId>
- version>1.18.4version>
- dependency>
- dependency>
- groupId>org.slf4jgroupId>
- artifactId>slf4j-apiartifactId>
- version>1.7.25version>
- dependency>
- dependency>
- groupId>org.slf4jgroupId>
- artifactId>slf4j-log4j12artifactId>
- version>1.7.25version>
- dependency>
- dependency>
- groupId>io.nettygroupId>
- artifactId>netty-allartifactId>
- version>4.1.45.Finalversion>
- dependency>
- dependency>
- groupId>com.alibabagroupId>
- artifactId>fastjsonartifactId>
- version>1.2.76version>
- dependency>
- dependencies>
-
-
- build>
- finalName>xiaot-protocolfinalName>
- plugins>
- plugin>
- groupId>org.apache.maven.pluginsgroupId>
- artifactId>maven-compiler-pluginartifactId>
- configuration>
- source>1.8source>
- target>1.8target>
- encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
- plugin>
- groupId>org.apache.maven.pluginsgroupId>
- artifactId>maven-surefire-pluginartifactId>
- configuration>
- skipTests>trueskipTests>
- configuration>
- plugin>
- plugins>
- build>
-
-
- project>
-
-
log4j.properties配置文件
- log4j.rootLogger = debug,stdout,debug,info,error
-
-
- log4j.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %p %l -%m%n
-
-
- log4j.appender.stdout = org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target = System.out
- log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern = ${log4j.pattern}
-
-
-
-
- log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.debug.File = ./logs/xiaot-info.log
- log4j.appender.debug.Append = true
- log4j.appender.debug.Threshold = DEBUG
- log4j.appender.debug.layout = org.apache.log4j.PatternLayout
- log4j.appender.debug.layout.ConversionPattern =${log4j.pattern}
-
-
-
-
- log4j.appender.info = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.info.File = ./logs/xiaot-info.log
- log4j.appender.info.Append = true
- log4j.appender.info.Threshold = INFO
- log4j.appender.info.layout = org.apache.log4j.PatternLayout
- log4j.appender.info.layout.ConversionPattern = ${log4j.pattern}
-
-
- log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.error.File = ./logs/xiaot-error.log
- log4j.appender.error.Append = true
- log4j.appender.error.Threshold = ERROR
- log4j.appender.error.layout = org.apache.log4j.PatternLayout
- log4j.appender.error.layout.ConversionPattern = ${log4j.pattern}
协议定义源码开发
消息头XiaotHeader.java源码:
- @Data
- public class XiaotHeader {
-
-
- /**
- * 协议魔数,固定值 0xACAFDCBA
- */
- private int major = Const.MAJOR;
- /**
- * 协议主版本:
- */
- private char mainVersion = Const.MAIN_VERSION;
- /**
- * 协议次版本:
- */
- private char minorVersion = Const.MINOR_VERSION;
- /**
- * 消息总长度
- */
- private int length;
- /**
- * 会话id
- */
- private long sid;
- /**
- * 指令
- */
- private char command;
- /**
- * 是否成功 1:成功,0:失败
- */
- private int success;
- /**
- * 消息头扩展属性
- */
- private Map
attribute = new HashMap(); -
-
- }
消息体XiaotMessage.java源码:
- @Data
- public class XiaotMessage {
-
-
- private XiaotHeader header;
- private Object body;
-
-
- }
协议指令枚举Command.java源码:
- public enum Command {
-
-
- HANDSHAKE_REQ('1', "握手请求消息"),
- HANDSHAKE_RESP('2', "握手应答消息"),
- BIZ_REQ('3', "业务请求消息"),
- BIZ_RESP('4', "业务应答消息"),
- HEARTBEAT_REQ('5', "心跳请求消息"),
- HEARTBEAT_RESP('6', "心跳应答消息"),
-
-
-
-
- ;
-
-
-
-
- private char val;
- private String desc;
-
-
- Command(char val, String desc) {
- this.val = val;
- this.desc = desc;
- }
-
-
- public static Command of(char val){
- for (Command value : Command.values()) {
- if (value.getVal() == val){
- return value;
- }
- }
- return null;
- }
-
-
- public char getVal() {
- return val;
- }
-
-
- public void setVal(char val) {
- this.val = val;
- }
-
-
- public String getDesc() {
- return desc;
- }
-
-
- public void setDesc(String desc) {
- this.desc = desc;
- }
- }
-
-
协议编解码源码开发
消息编码器XiaotMessageEncoder.java源码:
- @Slf4j
- public class XiaotMessageEncoder extends MessageToByteEncoder
{ -
-
- @Override
- protected void encode(ChannelHandlerContext channelHandlerContext, XiaotMessage message, ByteBuf byteBuf) throws Exception {
- if (message == null || message.getHeader() == null) {
- throw new Exception("encode message be do not null");
- }
- //Serialize
- byte[] raw = JSONObject.toJSONString(message).getBytes(StandardCharsets.UTF_8);
- message.getHeader().setLength(raw.length);
- // 这里严格按照解码器TCP粘包和拆包解决方案要求格式进行编码
- // writeInt表示往消息头写内容长度,占用空间是4个字节
- byteBuf.writeInt(message.getHeader().getLength());
- byteBuf.writeBytes(raw);
- }
-
-
- }
消息解码器XiaotMessageDecoder.java源码:
- public class XiaotMessageDecoder extends LengthFieldBasedFrameDecoder {
-
-
-
-
- public XiaotMessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
- super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
- }
-
-
-
-
- @Override
- protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
- //Netty处理粘包和拆包得到完整的消息包之后,会调用这里逻辑
- ByteBuf byteBuf = (ByteBuf) super.decode(ctx, in);
- if (byteBuf == null) {
- return null;
- }
- try {
- int length = byteBuf.readInt();
- //初始化byte数组
- byte[] array = new byte[length];
- //从byteBuf上次读位置开始读取字节数据到byte数组0,length位置
- byteBuf.getBytes(byteBuf.readerIndex(), array, 0, length);
- //实例化解码工具类
- //
- String json = new String(array, StandardCharsets.UTF_8);
- XiaotMessage message = JSONObject.parseObject(json, XiaotMessage.class);
- return message;
- } finally {
- byteBuf.release();
- }
- }
- }
握手源码开发
客户端发起握手请求处理器HandshakeReqHandler.java源码:
- @Slf4j
- public class HandshakeReqHandler extends ChannelInboundHandlerAdapter {
-
-
-
-
- /**
- * 在连接通道channel激活时立即发送握手请求
- *
- * @param ctx
- * @throws Exception
- */
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- XiaotHeader header = new XiaotHeader();
- header.setCommand(Command.HANDSHAKE_REQ.getVal());
- header.setSid(SidUtil.INSTANCE.nextId());
- XiaotMessage sendMsg = new XiaotMessage();
- sendMsg.setHeader(header);
- ChannelWriteUtil.write(ctx.channel(), sendMsg, future -> {
- if (future.cause() != null) {
- throw new IOException(future.cause());
- } else if (!future.isSuccess()) {
- throw new IOException("client send handshake request fail...");
- } else {
- log.debug("client send handshake request");
- }
- });
- }
-
-
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
-
-
- //类型转换
- XiaotMessage receiveMsg = (XiaotMessage) msg;
-
-
- //处理握手应答指令
- if (receiveMsg != null && receiveMsg.getHeader() != null && Command.HANDSHAKE_RESP.getVal() == receiveMsg.getHeader().getCommand()) {
- if (Const.SUCCESS == receiveMsg.getHeader().getSuccess()) {
- log.debug("client receive handshake response");
- } else {
- log.error("client receive handshake response fail " + (receiveMsg.getBody() == null ? "" : (String) receiveMsg.getBody()));
- ctx.close();
- }
- }
- //只有成功才继续往下流转
- ctx.fireChannelRead(msg);
- }
-
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- ctx.fireExceptionCaught(cause);
- }
- }
服务端应答握手处理器HandshakeRespHandler.java源码:
- @Slf4j
- public class HandshakeRespHandler extends ChannelInboundHandlerAdapter {
-
-
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
-
-
- //类型转换
- XiaotMessage receiveMsg = (XiaotMessage) msg;
-
-
- //参数检查
- if (receiveMsg != null && receiveMsg.getHeader() != null && Command.HANDSHAKE_REQ.getVal() == receiveMsg.getHeader().getCommand()) {
- //检查协议魔数
- if (Const.MAJOR != receiveMsg.getHeader().getMajor()) {
- ChannelWriteUtil.write(ctx.channel(), buildFailMessage("xiaot protocol major check fail," + receiveMsg.getHeader().getMajor()));
- return;
- }
-
-
- //检查主版本
- else if (Const.MAIN_VERSION != receiveMsg.getHeader().getMainVersion()) {
- ChannelWriteUtil.write(ctx.channel(), buildFailMessage("xiaot protocol main version check fail," + receiveMsg.getHeader().getMainVersion()));
- }
-
-
- //检查次版本
- else if (Const.MINOR_VERSION != receiveMsg.getHeader().getMinorVersion()) {
- ChannelWriteUtil.write(ctx.channel(), buildFailMessage("xiaot protocol minor version check fail," + receiveMsg.getHeader().getMinorVersion()));
- }
-
-
- //安全认证 || 握手应答
- else {
-
-
- XiaotSecurity security = new XiaotSecurity();
- security.setHeader(receiveMsg.getHeader());
- security.setRemoteAddress((InetSocketAddress) ctx.channel().remoteAddress());
- //基于JDK自带SPI技术实现安全认证
- ServiceLoader
loader = ServiceLoader.load(XiaotSecurityAuthProvide.class); - boolean isOk = true;
- for (XiaotSecurityAuthProvide service : loader) {
- String errorMsg = service.isAllow(security);
- if (errorMsg != null) {
- isOk = false;
- ChannelWriteUtil.write(ctx.channel(), buildFailMessage("xiaot protocol security check fail, " + errorMsg));
- break;
- }
- }
-
-
- if (isOk) {
- log.debug("server receive handshake request...");
- //应答握手
- XiaotMessage respMsg = new XiaotMessage();
- XiaotHeader respHeader = new XiaotHeader();
- respHeader.setSuccess(Const.SUCCESS);
- respHeader.setCommand(Command.HANDSHAKE_RESP.getVal());
- respMsg.setHeader(respHeader);
- ChannelWriteUtil.write(ctx.channel(), respMsg, new GenericFutureListener
super Void>>() { - @Override
- public void operationComplete(Future super Void> future) throws Exception {
- if (future.isSuccess()) {
- log.debug("server send handshake response");
- } else {
- log.error("server send handshake response fail...", future.cause());
- }
- }
- });
- }
- }
- }
-
-
- ctx.fireChannelRead(msg);
- }
-
-
- private XiaotMessage buildFailMessage(String body) {
- XiaotMessage respMsg = new XiaotMessage();
- XiaotHeader respHeader = new XiaotHeader();
- respHeader.setCommand(Command.HANDSHAKE_RESP.getVal());
- respHeader.setSuccess(Const.FAIL);
- respMsg.setHeader(respHeader);
- respMsg.setBody(body);
- log.error(body);
- return respMsg;
- }
-
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- ctx.fireExceptionCaught(cause);
- }
- }
心跳检查源码开发
客户端发起心跳请求HeartBeatReqHandler.java源码:
- @Slf4j
- public class HeartBeatReqHandler extends ChannelInboundHandlerAdapter {
-
-
- private volatile ScheduledFuture> scheduledFuture;
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- XiaotMessage receiveMsg = (XiaotMessage) msg;
- //参数检查
- if (receiveMsg != null && receiveMsg.getHeader() != null) {
-
-
- if (Command.HANDSHAKE_RESP.getVal() == receiveMsg.getHeader().getCommand()) {
- //接收到握手成功应答,初始化心跳检查后台任务
- if (Const.SUCCESS == receiveMsg.getHeader().getSuccess()) {
- log.debug("client init heartbeat task");
- scheduledFuture = ctx.executor().scheduleAtFixedRate(
- new HeartBeatTask(ctx), 0, 5, TimeUnit.SECONDS
- );
- }
- }
-
-
- //接收到心跳应答,
- else if (Command.HEARTBEAT_RESP.getVal() == receiveMsg.getHeader().getCommand()) {
- log.debug("client receive heartbeat response");
- }
- }
- //放过
- ctx.fireChannelRead(msg);
- }
-
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- if (scheduledFuture != null) {
- scheduledFuture.cancel(false);
- ctx.executor().terminationFuture();
- }
- ctx.fireExceptionCaught(cause);
- }
-
-
- private static class HeartBeatTask implements Runnable {
- private final ChannelHandlerContext ctx;
-
-
- public HeartBeatTask(ChannelHandlerContext ctx) {
- this.ctx = ctx;
- }
-
-
- @Override
- public void run() {
- XiaotMessage sendMsg = new XiaotMessage();
- XiaotHeader header = new XiaotHeader();
- header.setCommand(Command.HEARTBEAT_REQ.getVal());
- sendMsg.setHeader(header);
- ChannelWriteUtil.write(ctx.channel(), sendMsg, future -> {
- if (future.isSuccess()) {
- log.debug("client send heartbeat request");
- } else {
- log.error("client send heartbeat request fail...", future.cause());
- }
- });
- }
- }
-
-
- }
服务端心跳应答HeartBeatRespHandler.java源码:
- @Slf4j
- public class HeartBeatRespHandler extends ChannelInboundHandlerAdapter {
-
-
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- XiaotMessage receiveMsg = (XiaotMessage) msg;
- //参数检查
- if (receiveMsg != null && receiveMsg.getHeader() != null) {
- //接收到心跳请求,
- if (Command.HEARTBEAT_REQ.getVal() == receiveMsg.getHeader().getCommand()) {
- log.debug("server receive heartbeat request");
- //应答心跳请求
- XiaotMessage sendMsg = new XiaotMessage();
- XiaotHeader header = new XiaotHeader();
- header.setCommand(Command.HEARTBEAT_RESP.getVal());
- header.setSuccess(Const.SUCCESS);
- sendMsg.setHeader(header);
- ChannelWriteUtil.write(ctx.channel(), sendMsg, future -> {
- if (future.isSuccess()) {
- log.debug("server send heartbeat response");
- } else {
- log.error("server send heartbeat response fail..", future.cause());
- }
- });
- }
- }
-
-
- ctx.fireChannelRead(msg);
- }
-
-
- }
业务应答源码开发
- @Slf4j
- public class BizRespHandler extends ChannelInboundHandlerAdapter {
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- //类型转换
- XiaotMessage receiveMsg = (XiaotMessage) msg;
-
-
- //业务应答指令
- if (receiveMsg != null && receiveMsg.getHeader() != null && Command.BIZ_REQ.getVal() == receiveMsg.getHeader().getCommand()) {
- log.debug("server receive biz data : {}", JSONObject.toJSONString(receiveMsg));
- //基于JDK自带的SPI技术实现协议业务处理的自定义扩展开发
- //默认将接收到的业务请求回调XiaotBizRespCallbackProvide接口execute方法
- ServiceLoader
loader = ServiceLoader.load(XiaotBizRespCallbackProvide.class); - for (XiaotBizRespCallbackProvide service : loader) {
- service.execute(receiveMsg.getBody(), receiveMsg.getHeader().getAttribute(), ctx);
- }
- }
- ctx.fireChannelRead(msg);
- }
- }
-
-
末尾处理器源码开发
- @Slf4j
- public class TailHandler extends ChannelInboundHandlerAdapter {
-
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- //
- try {
- if (msg != null) {
- XiaotMessage message = (XiaotMessage) msg;
- if (message.getHeader() != null) {
- if (Command.of(message.getHeader().getCommand()) == null) {
- log.error("receive be not illegal command message: " + JSONObject.toJSONString(message));
- }
- }
- }
- } finally {
- ReferenceCountUtil.release(msg);
- }
-
-
- }
-
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- log.error(cause.getMessage(), cause);
- }
- }
-
-
建立连接源码开发
小T协议服务端源码:
- @Slf4j
- public class XiaotServer {
-
-
- /**
- * 绑定端口
- *
- * @throws Exception
- */
- public void bind(int port) throws Exception {
-
-
- //ReactSelect主线程组
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- //工作线程组
- EventLoopGroup workGroup = new NioEventLoopGroup();
-
-
- ServerBootstrap bootstrap = new ServerBootstrap();
- bootstrap.group(bossGroup, workGroup)
- .channel(NioServerSocketChannel.class)
- // 当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
- // 如果未设置或所设置的值小于1,Java将使用默认值50。
- // 当长度大于Backlog时,新的连接就会被TCP内核拒绝掉
- .option(ChannelOption.SO_BACKLOG, 100)
- .childHandler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline()
- //消息解码器,继承LengthFieldBasedFrameDecoder可以处理TCP粘包/拆包问题
- .addLast("XiaotMessageDecoder", new XiaotMessageDecoder(1024 * 1024, 0, 4))
- //消息编码器
- .addLast("XiaotMessageEncoder", new XiaotMessageEncoder())
- //握手应答处理器
- .addLast("HandshakeRespHandler", new HandshakeRespHandler())
- //心跳应答处理器
- .addLast("HeartBeatRespHandler", new HeartBeatRespHandler())
- //业务应答处理器
- .addLast("BizRespHandler", new BizRespHandler())
- //末尾处理器
- .addLast("TailHandler", new TailHandler())
- ;
- }
- });
- ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
- bootstrap.bind(port).sync();
- log.info("Xiaot Server Start on port: " + port);
- }
-
-
-
-
- public static void main(String[] args) throws Exception {
- new XiaotServer().bind(9000);
- }
- }
-
-
小T协议客户端源码:
- @Slf4j
- public class XiaotClient {
-
-
-
-
- /**
- * channel
- */
- private volatile Channel channel;
- private volatile boolean init = false;
-
-
- public XiaotClient(String host, int port) {
- //实例化客户端时启动异步线程向服务端请求连接
- new Thread(() -> connection(host, port)).start();
- }
-
-
- /**
- * 建立连接
- *
- * @param host 远程主机
- * @param port 远程端口
- */
- public synchronized void connection(String host, int port) {
-
-
- if (init) {
- return;
- }
- init = true;
- //
- EventLoopGroup group = new NioEventLoopGroup();
- Bootstrap bootstrap = new Bootstrap();
-
-
- try {
- bootstrap.group(group).channel(NioSocketChannel.class)
- //禁用nagle算法。
- .option(ChannelOption.TCP_NODELAY, Boolean.TRUE)
- //配置buffer水位线 1m
- // .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024))
- .handler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline()
- //读超时,当超过30s未接收到服务端任何应答(包括心跳应答)时,将断开连接进行重试任务。
- .addLast(new ReadTimeoutHandler(30, TimeUnit.SECONDS))
- //消息解码器,继承自LengthFieldBasedFrameDecoder解决了TCP粘包和拆包问题
- //第一个参数表示消息最大大小,这里设置较大一个值。
- //第二个参数表示消息头的偏移位置,这里设置为0表示不偏移。
- //第三个参数表示消息长度占用的字节大小,这边设置为4,对应编码器的writeInt(xxx)
- .addLast("XiaotMessageDecoder", new XiaotMessageDecoder(1024 * 1024, 0, 4))
- //消息编码器
- .addLast("XiaotMessageEncoder", new XiaotMessageEncoder())
- //握手请求处理器
- .addLast("HandshakeReqHandler", new HandshakeReqHandler())
- //心跳请求处理器
- .addLast("HeartBeatReqHandler", new HeartBeatReqHandler())
- //业务应答处理器
- .addLast("BizRespHandler", new BizRespHandler())
- //末尾处理器
- .addLast("TailHandler", new TailHandler())
- ;
- }
- });
- //打印资源泄露日志
- ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
- ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)).sync();
- log.info("Xiaot Protocol Client Connection Server Success.");
- channel = future.channel();
- channel.closeFuture().sync();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
-
-
- // 下面是客户端断开连接后的重连任务
- try {
- if (channel != null) {
- channel.close();
- channel = null;
- }
- TimeUnit.SECONDS.sleep(5);
- log.error("client connection time out, retry connection...");
- init = false;
- connection(host, port);
- } catch (InterruptedException interruptedException) {
- log.error("client connection time out, retry fail...", interruptedException);
- }
- }
- }
-
-
- /**
- * 发送消息
- *
- * @param body 消息体
- */
- public void sendMessage(Object body) throws Exception {
- this.sendMessage(body, null, null);
- }
-
-
- /**
- * 发送消息
- *
- * @param body 消息体
- */
- public void sendMessage(Object body, Map
headerMap) throws Exception { - this.sendMessage(body, headerMap, null);
- }
-
-
- /**
- * 发送消息
- *
- * @param body 消息体
- */
- public void sendMessage(Object body,
- Map
headerMap, - GenericFutureListener extends Future super Void>> futureListener) throws Exception {
-
-
-
-
- XiaotMessage message = new XiaotMessage();
- XiaotHeader header = new XiaotHeader();
- header.setCommand(Command.BIZ_REQ.getVal());
- if (headerMap != null) {
- header.setAttribute(headerMap);
- }
- message.setBody(body);
- message.setHeader(header);
-
-
- ChannelWriteUtil.write(channel, message, futureListener);
- }
-
-
- }
其他源码开发
ChannelWriteUtil.java源码:
- public class ChannelWriteUtil {
-
-
-
-
- public static ChannelFuture write(Channel channel,
- XiaotMessage message) {
-
-
- if (channel == null) {
- throw new NullPointerException("channel is null");
- }
- if (!channel.isWritable()) {
- throw new ChannelException("channel not writable");
- }
-
-
- ChannelFuture future = channel.writeAndFlush(message);
- if (!future.isSuccess()) {
- throw new ChannelException("channel write fail", future.cause());
- }
- return future;
- }
-
-
- public static ChannelFuture write(Channel channel,
- XiaotMessage message,
- GenericFutureListener extends Future super Void>> futureListener) {
-
-
- if (channel == null) {
- throw new NullPointerException("channel is null");
- }
- if (!channel.isWritable()) {
- throw new ChannelException("channel not writable");
- }
-
-
- ChannelFuture future = channel.writeAndFlush(message);
- if (future.cause() != null) {
- throw new ChannelException("channel write fail", future.cause());
- }
- if (futureListener != null) {
- future.addListener(futureListener);
- }
- return future;
- }
- }
-
-
Const.java源码:
- public class ChannelWriteUtil {
-
-
-
-
- public static ChannelFuture write(Channel channel,
- XiaotMessage message) {
-
-
- if (channel == null) {
- throw new NullPointerException("channel is null");
- }
- if (!channel.isWritable()) {
- throw new ChannelException("channel not writable");
- }
-
-
- ChannelFuture future = channel.writeAndFlush(message);
- if (!future.isSuccess()) {
- throw new ChannelException("channel write fail", future.cause());
- }
- return future;
- }
-
-
- public static ChannelFuture write(Channel channel,
- XiaotMessage message,
- GenericFutureListener extends Future super Void>> futureListener) {
-
-
- if (channel == null) {
- throw new NullPointerException("channel is null");
- }
- if (!channel.isWritable()) {
- throw new ChannelException("channel not writable");
- }
-
-
- ChannelFuture future = channel.writeAndFlush(message);
- if (future.cause() != null) {
- throw new ChannelException("channel write fail", future.cause());
- }
- if (futureListener != null) {
- future.addListener(futureListener);
- }
- return future;
- }
- }
-
-
小T协议试运行
启动客户端
模拟客户端启动源码如下:
- @Slf4j
- public class XiaotClientExample {
-
-
- public static void main(String[] args) throws InterruptedException {
-
-
- //实例化小T协议客户端
- XiaotClient client = new XiaotClient("localhost", 9000);
-
-
- //客户端启动三条线程同时通过小T协议客户端向服务端发送数据
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- try {
- TimeUnit.MILLISECONDS.sleep(1000);
- client.sendMessage("zhangsan sayHello33333 .................................", null, new GenericFutureListener
super Void>>() { - @Override
- public void operationComplete(Future super Void> future) throws Exception {
- if (future.isSuccess()) {
- // log.info("send success...");
- } else {
- log.error("send fail...", future.cause());
- }
- }
- });
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }
- }
- }
- }).start();
-
-
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true){
- try {
- TimeUnit.MILLISECONDS.sleep(7);
- client.sendMessage("wangwu sayHello11111 .................................", null);
- }catch (Exception e){
- log.error(e.getMessage(), e);
- }
- }
- }
- }).start();
-
-
-
-
- new Thread(new Runnable() {
- @Override
- public void run() {
- while (true){
- try {
- TimeUnit.MILLISECONDS.sleep(2);
- client.sendMessage("zhaoliu sayHello222222 .................................", null);
- }catch (Exception e){
- log.error(e.getMessage(), e);
- }
- }
- }
- }).start();
-
-
- //阻塞主线程
- TimeUnit.HOURS.sleep(10000);
- }
-
-
- }
-
-
客户端控制台日志输出如下:
由于服务端没启动,客户端请求连接失败,可以看到客户端channel为null导致不可写以及客户端定时发起重试的日志。
启动服务端
模拟启动服务端源码:
- public class XiaotServerExample {
-
-
- public static void main(String[] args) throws Exception {
- new XiaotServer().bind(9000);
- }
- }
服务端控制台日志输入如下:
可以看到服务端不断接收到客户端三条线程同时发送的业务请求,以及握手和心跳的请求。现在我们看到客户端控制台输出如下:
可以看到客户端定时发送和接收的心跳请求。此时如果关闭服务端,客户端将会断开连接,执行重连任务,截图如下: