RocketMQ通信協議


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

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM