欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

RocketMQ?producer發(fā)送者淺析

 更新時(shí)間:2023年04月24日 11:33:09   作者:Acqierement  
RocketMQ生產(chǎn)者是一種高性能、可靠的消息發(fā)送者,能夠?qū)⑾⒖焖?、可靠地發(fā)送到RocketMQ消息隊(duì)列中。它具有多種消息發(fā)送模式和消息發(fā)送方式,可以根據(jù)不同的業(yè)務(wù)需求進(jìn)行靈活配置

發(fā)送者其實(shí)比較簡(jiǎn)單,需要做的就是首先確定往哪里發(fā)送,其次怎么讓消息發(fā)送順暢。我們就看一下具體的代碼吧。

首先調(diào)用start方法。完成各個(gè)類的初始化,啟動(dòng)多個(gè)定時(shí)任務(wù),其中一個(gè)定時(shí)任務(wù)是updateTopicRouteInfoFromNameServer,這個(gè)方法里面和nameService建立長(zhǎng)連接,同時(shí)維護(hù)了topicRouteTable和brokerAddrTable等緩存。topicRouteTable里面維護(hù)了這個(gè)topic包括有哪些queue和broker。這樣producer才可以知道要發(fā)往哪里。

啟動(dòng)的流程主要在這個(gè)方法中:

MQClientInstance#start

public void start() throws MQClientException {
    synchronized (this) {
        switch (this.serviceState) {
            case CREATE_JUST:
                this.serviceState = ServiceState.START_FAILED;
                // If not specified,looking address from name server
                if (null == this.clientConfig.getNamesrvAddr()) {
                    this.mQClientAPIImpl.fetchNameServerAddr();
                }
                // Start request-response channel
                this.mQClientAPIImpl.start();
                // Start various schedule tasks
                this.startScheduledTask();
                // Start pull service
                this.pullMessageService.start();
                // Start rebalance service
                this.rebalanceService.start();
                // Start push service
                this.defaultMQProducer.getDefaultMQProducerImpl().start(false);
                log.info("the client factory [{}] start OK", this.clientId);
                this.serviceState = ServiceState.RUNNING;
                break;
            case START_FAILED:
                throw new MQClientException("The Factory object[" + this.getClientId() + "] has been created before, and failed.", null);
            default:
                break;
        }
    }
}

其中啟動(dòng)了一系列定時(shí)任務(wù),包括org.apache.rocketmq.client.impl.factory.MQClientInstance#updateTopicRouteInfoFromNameServer這個(gè)方法

    public boolean updateTopicRouteInfoFromNameServer(final String topic, boolean isDefault,
        DefaultMQProducer defaultMQProducer) {
        try {
            if (this.lockNamesrv.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
                try {
                    TopicRouteData topicRouteData;
                    if (isDefault && defaultMQProducer != null) {
                        // 從nameServer獲取topciRouteData
                        topicRouteData = this.mQClientAPIImpl.getDefaultTopicRouteInfoFromNameServer(defaultMQProducer.getCreateTopicKey(),
                            clientConfig.getMqClientApiTimeout());
                        if (topicRouteData != null) {
                            for (QueueData data : topicRouteData.getQueueDatas()) {
                                int queueNums = Math.min(defaultMQProducer.getDefaultTopicQueueNums(), data.getReadQueueNums());
                                data.setReadQueueNums(queueNums);
                                data.setWriteQueueNums(queueNums);
                            }
                        }
                    } else {
                        topicRouteData = this.mQClientAPIImpl.getTopicRouteInfoFromNameServer(topic, clientConfig.getMqClientApiTimeout());
                    }
                    if (topicRouteData != null) {
                        TopicRouteData old = this.topicRouteTable.get(topic);
                        boolean changed = topicRouteData.topicRouteDataChanged(old);
                        if (!changed) {
                            changed = this.isNeedUpdateTopicRouteInfo(topic);
                        } else {
                            log.info("the topic[{}] route info changed, old[{}] ,new[{}]", topic, old, topicRouteData);
                        }
                        if (changed) {
                            for (BrokerData bd : topicRouteData.getBrokerDatas()) {
                                this.brokerAddrTable.put(bd.getBrokerName(), bd.getBrokerAddrs());
                            }
                            // Update endpoint map
                            {
                                ConcurrentMap<MessageQueue, String> mqEndPoints = topicRouteData2EndpointsForStaticTopic(topic, topicRouteData);
                                if (!mqEndPoints.isEmpty()) {
                                    topicEndPointsTable.put(topic, mqEndPoints);
                                }
                            }
                            // Update Pub info
                            {
                                // 生成topicPublishInfo
                                TopicPublishInfo publishInfo = topicRouteData2TopicPublishInfo(topic, topicRouteData);
                                publishInfo.setHaveTopicRouterInfo(true);
                                for (Entry<String, MQProducerInner> entry : this.producerTable.entrySet()) {
                                    MQProducerInner impl = entry.getValue();
                                    if (impl != null) {
                                        // 更新 topicPublishInfo
                                        impl.updateTopicPublishInfo(topic, publishInfo);
                                    }
                                }
                            }
                            // Update sub info
                            if (!consumerTable.isEmpty()) {
                                Set<MessageQueue> subscribeInfo = topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
                                for (Entry<String, MQConsumerInner> entry : this.consumerTable.entrySet()) {
                                    MQConsumerInner impl = entry.getValue();
                                    if (impl != null) {
                                        impl.updateTopicSubscribeInfo(topic, subscribeInfo);
                                    }
                                }
                            }
                            TopicRouteData cloneTopicRouteData = new TopicRouteData(topicRouteData);
                            log.info("topicRouteTable.put. Topic = {}, TopicRouteData[{}]", topic, cloneTopicRouteData);
                            this.topicRouteTable.put(topic, cloneTopicRouteData);
                            return true;
                        }
                    } else {
                        log.warn("updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}. [{}]", topic, this.clientId);
                    }
                } catch (MQClientException e) {
                    if (!topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) && !topic.equals(TopicValidator.AUTO_CREATE_TOPIC_KEY_TOPIC)) {
                        log.warn("updateTopicRouteInfoFromNameServer Exception", e);
                    }
                } catch (RemotingException e) {
                    log.error("updateTopicRouteInfoFromNameServer Exception", e);
                    throw new IllegalStateException(e);
                } finally {
                    this.lockNamesrv.unlock();
                }
            } else {
                log.warn("updateTopicRouteInfoFromNameServer tryLock timeout {}ms. [{}]", LOCK_TIMEOUT_MILLIS, this.clientId);
            }
        } catch (InterruptedException e) {
            log.warn("updateTopicRouteInfoFromNameServer Exception", e);
        }
        return false;
    }

通過方法名也知道是從nameServer獲取這個(gè)topic相關(guān)的broke數(shù)據(jù),拿到TopicRouteData數(shù)據(jù)。先更新brokerAddrTable,存儲(chǔ)borker具體的地址。然后在org.apache.rocketmq.client.impl.factory.MQClientInstance#topicRouteData2TopicPublishInfo里面再進(jìn)一步生成TopicPublishInfo數(shù)據(jù)。TopicPublishInfo是對(duì)TopicRouteData的一個(gè)封裝,除了TopicRouteData,還有messageQueue數(shù)據(jù),messageQueue是Queue和Borker的交集,會(huì)根據(jù)配置的queue數(shù)量,生成具體的messageQueue,queueId就是0,1,2,3,4他們自己的順序。

所以有了TopicPublishInfo數(shù)據(jù),就知道往哪里發(fā)了。

發(fā)送消息的過程。

  • 先找到TopicPublishInfo。TopicPublishInfo里面有一個(gè)MessageQueue的list。
  • 從MessageQueueList里面拿到一個(gè)messageQueue。 如果沒有開啟sendLatencyFaultEnable,默認(rèn)就是采用輪詢方法。具體的輪詢方式就是,TopicPublishInfo里面維護(hù)了一個(gè)序號(hào)index,每次index自增1,然后通過index去MessageQueueList里面拿一個(gè)。
  • 拿到了MessageQueue之后,里面有broker的name,根據(jù)name去找broker的ip地址,發(fā)送數(shù)據(jù)。這個(gè)ip地址就是前面提到的brokerAddrTable變量,在updateTopicRouteInfoFromNameServer方法里面維護(hù)的。

到此這篇關(guān)于RocketMQ producer發(fā)送者淺析的文章就介紹到這了,更多相關(guān)RocketMQ producer內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    這篇文章主要介紹了詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • Java多線程案例之定時(shí)器詳解

    Java多線程案例之定時(shí)器詳解

    定時(shí)器是一種實(shí)際開發(fā)中非常常用的組件,?類似于一個(gè)?“鬧鐘”,?達(dá)到一個(gè)設(shè)定的時(shí)間之后,?就執(zhí)行某個(gè)指定好的代碼。本文主要來和大家聊聊定時(shí)器的原理與使用,需要的可以參考一下
    2023-01-01
  • Spring容器的創(chuàng)建過程之如何注冊(cè)BeanPostProcessor詳解

    Spring容器的創(chuàng)建過程之如何注冊(cè)BeanPostProcessor詳解

    關(guān)于BeanPostProcessor 各位一定不陌生,今天整理的這篇文章總結(jié)了如何注冊(cè)BeanPostProcessor,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下
    2021-06-06
  • Java連接postgresql數(shù)據(jù)庫(kù)的示例代碼

    Java連接postgresql數(shù)據(jù)庫(kù)的示例代碼

    本篇文章主要介紹了Java連接postgresql數(shù)據(jù)庫(kù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 解決Spring Cloud中Feign/Ribbon第一次請(qǐng)求失敗的方法

    解決Spring Cloud中Feign/Ribbon第一次請(qǐng)求失敗的方法

    這篇文章主要給大家介紹了關(guān)于解決Spring Cloud中Feign/Ribbon第一次請(qǐng)求失敗的方法,文中給出了三種解決的方法,大家可以根據(jù)需要選擇對(duì)應(yīng)的方法,需要的朋友們下面來一起看看吧。
    2017-02-02
  • 深入IDEA Debug問題透析詳解

    深入IDEA Debug問題透析詳解

    這篇文章主要為大家介紹了深入IDEA Debug問題透析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Java中對(duì)于并發(fā)問題的處理思路分享

    Java中對(duì)于并發(fā)問題的處理思路分享

    并發(fā)粗暴的解釋就是一段代碼,在同一時(shí)間段內(nèi),被多個(gè)線程同時(shí)處理的情況就是并發(fā)現(xiàn)象。這篇文章和大家分享了一些對(duì)于并發(fā)問題的處理思路,需要的可以參考一下
    2023-02-02
  • spring boot 統(tǒng)一JSON格式的接口返回結(jié)果的實(shí)現(xiàn)

    spring boot 統(tǒng)一JSON格式的接口返回結(jié)果的實(shí)現(xiàn)

    這篇文章主要介紹了spring boot 統(tǒng)一JSON格式的接口返回結(jié)果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口

    Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口

    這篇文章主要給大家介紹了關(guān)于Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • Netty內(nèi)存池泄漏問題以解決方案

    Netty內(nèi)存池泄漏問題以解決方案

    這篇文章主要介紹了Netty內(nèi)存池泄漏問題以解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評(píng)論