RocketMQ—Producer(四)消息發(fā)送流程

前 言

在深入講解消息發(fā)送之前疗涉,我們可先簡(jiǎn)單概括消息的發(fā)送的主要步驟可分為:消息驗(yàn)證、路由查詢吟秩、選擇消息隊(duì)列咱扣、消息組裝、消息發(fā)送涵防、消息結(jié)果處理闹伪、異常處理;(單向發(fā)送并不處理消息發(fā)送結(jié)果);同步偏瓤、異步杀怠、單向發(fā)送消息的入口API有一些區(qū)別,本文將以下面接口實(shí)現(xiàn)類為入口分析消息發(fā)送的流程:

DefaultMQProducerImpl#sendDefaultImpl

(由于消息發(fā)送細(xì)節(jié)非常多厅克,本文將分析核心步驟赔退,如漏掉還請(qǐng)各位查漏補(bǔ)缺,自行分析哈)

同步發(fā)送總結(jié)流程圖如下:

image.png

一证舟、源碼分析

DefaultMQProducerImpl#sendDefaultImpl

/**
 * 發(fā)送信息
 * @param msg 消息內(nèi)容
 * @param communicationMode 發(fā)送模式
 * @param sendCallback 回掉
 * @param timeout 超時(shí)時(shí)間
 */
private SendResult sendDefaultImpl(
    Message msg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback,
    final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();         //驗(yàn)證 serviceState == Running 運(yùn)行中
    Validators.checkMessage(msg, this.defaultMQProducer);   //1> 驗(yàn)證消息

    final long invokeID = random.nextLong();//隨機(jī)的-invokeId
    long beginTimestampFirst = System.currentTimeMillis();//開始時(shí)間
    long beginTimestampPrev = beginTimestampFirst;
    long endTimestamp = beginTimestampFirst;
    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic()); // 2> 獲取路由信息
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        boolean callTimeout = false;
        MessageQueue mq = null;
        Exception exception = null;
        SendResult sendResult = null;
        int timesTotal = communicationMode == CommunicationMode.SYNC ? 1 + this.defaultMQProducer.getRetryTimesWhenSendFailed() : 1;//重試次數(shù)硕旗,同步默認(rèn)3,其他1次
        int times = 0;
        String[] brokersSent = new String[timesTotal];//發(fā)送的brokerName集合
        for (; times < timesTotal; times++) {
            String lastBrokerName = null == mq ? null : mq.getBrokerName();
            MessageQueue mqSelected = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName); // 3>選擇消息隊(duì)列
            if (mqSelected != null) {
                mq = mqSelected;
                brokersSent[times] = mq.getBrokerName();
                try {
                    beginTimestampPrev = System.currentTimeMillis();//本次開始時(shí)間
                    long costTime = beginTimestampPrev - beginTimestampFirst;//計(jì)算發(fā)送消耗時(shí)間
                    if (timeout < costTime) {//如果消耗時(shí)間 大于 超時(shí)時(shí)間褪储,直接break
                        callTimeout = true;
                        break;
                    }
                    //發(fā)送消息
                    sendResult = this.sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout - costTime); // 4>消息發(fā)送
                    //發(fā)送完成時(shí)間
                    endTimestamp = System.currentTimeMillis();
                    //更新失敗條目信息
                    this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, false);
                    switch (communicationMode) {
                        case ASYNC:
                            return null;
                        case ONEWAY:
                            return null;
                        case SYNC:
                            if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
                                if (this.defaultMQProducer.isRetryAnotherBrokerWhenNotStoreOK()) {
                                    continue;
                                }
                            }

                            return sendResult;
                        default:
                            break;
                    }
                } catch (RemotingException e) {
                    endTimestamp = System.currentTimeMillis();
                    this.updateFaultItem(mq.getBrokerName(), endTimestamp - beginTimestampPrev, true);  //5>更新失敗條目
                    log.warn(String.format("sendKernelImpl exception, resend at once, InvokeID: %s, RT: %sms, Broker: %s", invokeID, endTimestamp - beginTimestampPrev, mq), e);
                    log.warn(msg.toString());
                    exception = e;
                    continue;
                    ...省略...
            } else {    //沒有找到消息隊(duì)列卵渴,直接break
                break;
            }
        }
        if (sendResult != null) {
            return sendResult;
        }
        String info = String.format("Send [%d] times, still failed, cost [%d]ms, Topic: %s, BrokersSent: %s",
            times,
            System.currentTimeMillis() - beginTimestampFirst,
            msg.getTopic(),
            Arrays.toString(brokersSent));

        info += FAQUrl.suggestTodo(FAQUrl.SEND_MSG_FAILED);

        MQClientException mqClientException = new MQClientException(info, exception);
            ...省略...
        throw mqClientException;
    }
   ...省略...
}

1.1 驗(yàn)證消息

Validators.checkMessage

//Validate message 驗(yàn)證消息
public static void checkMessage(Message msg, DefaultMQProducer defaultMQProducer)
    throws MQClientException {
    if (null == msg) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message is null");
    }
    Validators.checkTopic(msg.getTopic()); // 驗(yàn)證topic, 此處代碼大家可自行查看,灰常簡(jiǎn)單
    if (null == msg.getBody()) {           // body 消息體不能為空
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body is null");
    }
    if (0 == msg.getBody().length) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL, "the message body length is zero");
    }
    //消息最大長(zhǎng)度 不能大于 4M
    if (msg.getBody().length > defaultMQProducer.getMaxMessageSize()) {
        throw new MQClientException(ResponseCode.MESSAGE_ILLEGAL,
            "the message body size over max value, MAX: " + defaultMQProducer.getMaxMessageSize());
    }
}

備注:

主要驗(yàn)證消息分為兩部分

  1. topic驗(yàn)證:匹配正則表達(dá)式(^[%|a-zA-Z0-9_-]+$),長(zhǎng)度小于255,不等于默認(rèn)主題:TBW102
  2. body驗(yàn)證:body內(nèi)容是否為空鲤竹,消息內(nèi)容最大長(zhǎng)度默認(rèn)不能超過4M

1.2 獲取路由信息

tryToFindTopicPublishInfo

在'路由動(dòng)態(tài)更新'我們以及分析過了,代碼大家可以再回顧下昔榴,簡(jiǎn)單邏輯總結(jié)如下:

  1. 如果生產(chǎn)者中緩存了 topic 的路由信息辛藻,如果該路由信息中包含了消息隊(duì)列,則直接返回該路由信息;
  2. 如果沒有緩存或沒有包含消息隊(duì)列互订, 則向 NameServer查詢?cè)?topic 的路由信息;
  3. 如果最終未找到路由信息吱肌,則拋出異常 : 無法找到主題相關(guān)路由信息異常.

1.3 選擇消息隊(duì)列

將在'系列5'著重分析此段代碼功能消息

1.4 消息發(fā)送

sendKernelImpl(msg, mq, communicationMode, sendCallback, topicPublishInfo, timeout - costTime);

由于代碼篇幅太長(zhǎng),下面講解只摘取sendKernelImpl方法的核心代碼解析,但強(qiáng)烈建議仔細(xì)去擼一遍代碼消息。

1.4.1 查詢-brokerAddr

String brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
if (null == brokerAddr) {
    tryToFindTopicPublishInfo(mq.getTopic());
    brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
}
if(brokerAddr != null) {
  ... 省略 ...
} else{
  拋異常
}

邏輯:

從brokerAddrTable獲取主MasterId,獲取不到則查詢路由仰禽,如果繼續(xù)獲取不到則跑異常消息

 //MQClientInstance#findBrokerAddressInPublish(獲取broker的網(wǎng)絡(luò)地址(主-master的地址)
public String findBrokerAddressInPublish(final String brokerName) {
    HashMap<Long/* brokerId */, String/* address */> map = this.brokerAddrTable.get(brokerName);
    if (map != null && !map.isEmpty()) {
        return map.get(MixAll.MASTER_ID);
    }
    return null;
}

備注:

brokerAddrTable 是路由更新維護(hù)的broker地址信息氮墨。

1.1.2 消息壓縮消息

int sysFlag = 0;
boolean msgBodyCompressed = false;//壓縮標(biāo)記
if (this.tryToCompressMessage(msg)) {//嘗試壓縮
    sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
    msgBodyCompressed = true;
}
// 壓縮
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }
    return false;
}

備注:

  1. 批量消息不支持壓縮
  2. 消息大于4k,zip壓縮吐葵,壓縮級(jí)別:默認(rèn):5

1.1.3 發(fā)送消息請(qǐng)求參數(shù)構(gòu)建消息

  • SendMessageRequestHeader
/** 構(gòu)建消息發(fā)送 請(qǐng)求包 规揪。主要包含如下重要信息:生產(chǎn)者組、主題名稱温峭、默認(rèn)創(chuàng)建主題Key猛铅、該主題在單個(gè)Broker默認(rèn)隊(duì)列數(shù) 、隊(duì)列ID (隊(duì)列序號(hào))凤藏、消息系統(tǒng)標(biāo)記 ( MessageSysFlag)奸忽、
   消息發(fā)送時(shí)間、消息標(biāo)記(RocketMQ對(duì)消息中的 flag不做任何處理揖庄, 供應(yīng)用程序使用)栗菜、 消息擴(kuò)展屬性、消息重試次數(shù)蹄梢、是否是批量消息等疙筹。
*/
SendMessageRequestHeader requestHeader = newSendMessageRequestHeader();
requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());//生產(chǎn)者組
requestHeader.setTopic(msg.getTopic());//主題名稱
requestHeader.setDefaultTopic(this.defaultMQProducer.getCreateTopicKey());//默認(rèn)創(chuàng)建主題Key
requestHeader.setDefaultTopicQueueNums(this.defaultMQProducer.getDefaultTopicQueueNums());//該主題在單個(gè)Broker默認(rèn)隊(duì)列數(shù)
requestHeader.setQueueId(mq.getQueueId());//隊(duì)列ID (隊(duì)列序號(hào))
requestHeader.setSysFlag(sysFlag);//消息系統(tǒng)標(biāo)記 ( MessageSysFlag)
requestHeader.setBornTimestamp(System.currentTimeMillis());//消息發(fā)送時(shí)間
requestHeader.setFlag(msg.getFlag());//消息標(biāo)記(RocketMQ對(duì)消息中的 flag不做任何處理, 供應(yīng)用程序使用)
requestHeader.setProperties(MessageDecoder.messageProperties2String(msg.getProperties()));//【重要】消息擴(kuò)展屬性
requestHeader.setReconsumeTimes(0);//消息重試次數(shù) 
requestHeader.setUnitMode(this.isUnitMode());
requestHeader.setBatch(msg instanceofMessageBatch);//是否是批量消息等
if(requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {//主題 topic 包含:RETRY
   String reconsumeTimes = MessageAccessor.getReconsumeTime(msg);
   if (reconsumeTimes != null) {
       requestHeader.setReconsumeTimes(Integer.valueOf(reconsumeTimes));
       MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_RECONSUME_TIME);
   }
   String maxReconsumeTimes = MessageAccessor.getMaxReconsumeTimes(msg);
   if (maxReconsumeTimes != null) {
       requestHeader.setMaxReconsumeTimes(Integer.valueOf(maxReconsumeTimes));
       MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_MAX_RECONSUME_TIMES);
   }
}

1.1.4 消息發(fā)送

  • MQClientAPIImpl#sendMessage

public SendResult sendMessage(
        final String addr,
        final String brokerName,
        final Message msg,
        final SendMessageRequestHeader requestHeader,
        final long timeoutMillis,
        final CommunicationMode communicationMode,
        final SendCallback sendCallback,
        final TopicPublishInfo topicPublishInfo,
        final MQClientInstance instance,
        final int retryTimesWhenSendFailed,
        final SendMessageContext context,
        final DefaultMQProducerImpl producer
    ) throws RemotingException, MQBrokerException, InterruptedException {
    long beginStartTime = System.currentTimeMillis();
    RemotingCommand request = null;
    if (sendSmartMsg || msg instanceof MessageBatch) {
        //默認(rèn)smartMsg(智能) 或者 批量消息
        SendMessageRequestHeaderV2 requestHeaderV2 = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(requestHeader);
        request = RemotingCommand.createRequestCommand(msg instanceof MessageBatch ? RequestCode.SEND_BATCH_MESSAGE : RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
    } else {
        request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
    }
    request.setBody(msg.getBody());//設(shè)置消息內(nèi)容

    switch (communicationMode) {
        case ONEWAY://單向
            this.remotingClient.invokeOneway(addr, request, timeoutMillis);
            return null;
        case ASYNC://異步
            final AtomicInteger times = new AtomicInteger();
            long costTimeAsync = System.currentTimeMillis() - beginStartTime;
            if (timeoutMillis < costTimeAsync) {
                throw new RemotingTooMuchRequestException("sendMessage call timeout");
            }
            this.sendMessageAsync(addr, brokerName, msg, timeoutMillis - costTimeAsync, request, sendCallback, topicPublishInfo, instance,
                retryTimesWhenSendFailed, times, context, producer);
            return null;
        case SYNC://同步
            long costTimeSync = System.currentTimeMillis() - beginStartTime;
            if (timeoutMillis < costTimeSync) { //超時(shí)判斷
                throw new RemotingTooMuchRequestException("sendMessage call timeout");
            }
            return this.sendMessageSync(addr, brokerName, msg, timeoutMillis - costTimeSync, request);
        default:
            assert false;
            break;
    }
    return null;
}

分析:

從此處可知道,單向/異步/同步發(fā)送的實(shí)際差別了腌歉。單向發(fā)送直接返回null,同步需要等待返回結(jié)果蛙酪,異步返回null但sendCallback會(huì)異步處理發(fā)送結(jié)果。
牛逼的你一定會(huì)去研究 invokeOneway翘盖、sendMessageAsync桂塞、sendMessageSync 三個(gè)方法的的源碼,其實(shí)很簡(jiǎn)單馍驯。

二阁危、結(jié)論

其實(shí)發(fā)送流程涉及代碼很多,這邊沒有一一分析汰瘫,比如落下的一些可擴(kuò)展的鉤子函數(shù)狂打,netty網(wǎng)絡(luò)處理,最關(guān)鍵的是異常處理等混弥,建議仔細(xì)研究哈趴乡。


程序員的核心競(jìng)爭(zhēng)力其實(shí)還是技術(shù),因此對(duì)技術(shù)還是要不斷的學(xué)習(xí)蝗拿,關(guān)注 “IT 巔峰技術(shù)” 公眾號(hào) 晾捏,該公眾號(hào)內(nèi)容定位:中高級(jí)開發(fā)、架構(gòu)師哀托、中層管理人員等中高端崗位服務(wù)的惦辛,除了技術(shù)交流外還有很多架構(gòu)思想和實(shí)戰(zhàn)案例。

作者是 《 消息中間件 RocketMQ 技術(shù)內(nèi)幕》 一書作者仓手,同時(shí)也是 “RocketMQ 上海社區(qū)”聯(lián)合創(chuàng)始人胖齐,曾就職于拼多多、德邦等公司嗽冒,現(xiàn)任上市快遞公司架構(gòu)負(fù)責(zé)人呀伙,主要負(fù)責(zé)開發(fā)框架的搭建、中間件相關(guān)技術(shù)的二次開發(fā)和運(yùn)維管理辛慰、混合云及基礎(chǔ)服務(wù)平臺(tái)的建設(shè)区匠。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市帅腌,隨后出現(xiàn)的幾起案子驰弄,更是在濱河造成了極大的恐慌,老刑警劉巖速客,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戚篙,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡溺职,警方通過查閱死者的電腦和手機(jī)岔擂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門位喂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乱灵,你說我怎么就攤上這事塑崖。” “怎么了痛倚?”我有些...
    開封第一講書人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵规婆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我蝉稳,道長(zhǎng)抒蚜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任耘戚,我火速辦了婚禮嗡髓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘收津。我一直安慰自己饿这,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開白布撞秋。 她就那樣靜靜地躺著蛹稍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪部服。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評(píng)論 1 289
  • 那天拗慨,我揣著相機(jī)與錄音廓八,去河邊找鬼。 笑死赵抢,一個(gè)胖子當(dāng)著我的面吹牛剧蹂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播烦却,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼宠叼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了其爵?” 一聲冷哼從身側(cè)響起冒冬,我...
    開封第一講書人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎摩渺,沒想到半個(gè)月后简烤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡摇幻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年横侦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了挥萌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡枉侧,死狀恐怖引瀑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情榨馁,我是刑警寧澤憨栽,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站辆影,受9級(jí)特大地震影響徒像,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛙讥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一锯蛀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧次慢,春花似錦旁涤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至闻妓,卻和暖如春菌羽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背由缆。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來泰國打工注祖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人均唉。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓是晨,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親舔箭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子罩缴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容