我們先從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; }