我们先从client端看一个消息是如何发送到服务端,服务端又是如何解析消息的。
client端:
构造请求体:
构造请求体:
发送消息体:
下面看服务端:
rocketmq的协议服务端解析救灾这里了RemotingCommand.decode(byteBuffer),我们来看下这个方法,具体是如何解析协议的:
public static RemotingCommand decode(final ByteBuffer byteBuffer) {
int length = byteBuffer.limit(); //获取字节缓冲区的整个长度,这个长度等于通信协议格式的2、3、4段的总长度
int oriHeaderLen = byteBuffer.getInt(); //从缓冲区中读取4个字节的int类型的数据值 ,这个值就是报文头部的长度
int headerLength = getHeaderLength(oriHeaderLen); byte[] headerData = new byte[headerLength];
byteBuffer.get(headerData); //接下来从缓冲区中读取headerLength个字节的数据,这个数据就是报文头部的数据 RemotingCommand cmd = headerDecode(headerData, getProtocolType(oriHeaderLen)); int bodyLength = length - 4 - headerLength;
byte[] bodyData = null;
if (bodyLength > 0) {
bodyData = new byte[bodyLength];
byteBuffer.get(bodyData); //接下来读取length-4-headerLength 个字节的数据,这个数据就是报文体的数据
}
cmd.body = bodyData; return cmd;
}