RocketMQ幾個核心概念

Producer

A producer sends messages generated by the business application systems to brokers. RocketMQ provides multiple paradigms of sending: synchronous, asynchronous and one-way.

官網翻譯過來就是消息發送者發送由業務系統生成的消息到brokers。會有多種發送消息的方式:同步、異步和單向。

synchronous
消息發送出去後,producer會等到broker迴應後才能繼續發送下一個消息。

SendResult sendResult = producer.send(msg);

asynchronous
指發送方發出數據後,不等接收方發回響應,接着發送下個數據包的通訊方式。 MQ 的異步發送,需要用戶實現異步發送回調接口(SendCallback)。消息發送方在發送了一條消息後,不需要等待服務器響應即可返回,進行第二條消息發送。發送方通過回調接口接收服務器響應,並對響應結果進行處理。

producer.send(msg, new SendCallback() {
    @Override
    public void onSuccess(SendResult sendResult) {
        System.out.printf("%s%n",sendResult);
    }
    @Override
    public void onException(Throwable throwable) {
        throwable.printStackTrace();
    }
});

one-way
發送方只負責發送消息,不等待服務器迴應且沒有回調函數觸發,即只發送請求不等待應答.效率最高。

producer.sendOneway(msg);

Producer Group

Producers of the same role are grouped together. A different producer instance of the same producer group may be contacted by a broker to commit or roll back a transaction in case the original producer crashed after the transaction.

相同角色的producers會被放到一個producer group。以防最開始的發送者再發起事務後崩潰,一個發送組的其他實例會進行提交或者回滾事務。

Warning: Considering the provided producer is sufficiently powerful at sending messages, only one instance is allowed per producer group to avoid unnecessary initialization of producer instances.

警告:考慮到發送者發送消息足夠強大,所以只有一個producer group裏只允許存在一個實例,以避免不必要的多實例初始化。

Consumer

A Consumer pulls messages from brokers and feeds them into application. In perspective of user application, two types of consumers are provided:

消費者從brokers上拉取消息。有兩種類型的消費者。

PullConsumer: pull consumer actively pulls messages from brokers. Once batches of messages are pulled, user application initiates consuming process.

拉取消息:消費者從brokers上拉取一批消息進行消費。

PushConsumer: push consumer, on the other hand, encapsulates message pulling, consuming progress and maintaining other work inside, leaving a callback interface to end user to implement which will be executed on message arrival.

推送消息:封裝消息拉取,消費進度和維持內部其他工作,將回調接口留給最終用戶來實現,在消息到達執行。

Consumer Group

Similar to previously mentioned producer group, consumers of the exactly same role are grouped together and named Consumer Group.

相同角色的消費者會被放到一個consumer group。

Consumer Group is a great concept with which achieving goals of load-balance and fault-tolerance, in terms of message consuming, is super easy.

消費組是一個很大的概念,用來達到負載均衡和容錯的目標。在消息消費方面非常簡單。

Warning: consumer instances of a consumer group must have exactly the same topic subscription(s).

警告:一個消費組的消費者實例必須訂閱相同的topic。

Topic

Topic is a category in which producers deliver messages and consumers pull messages. Topics have very loose relationship with producers and consumers. Specifically, a topic may have zero, one or multiple producers that sends messages to it; conversely, a producer can send messages of different topics. In consumer’s perspective, a topic may be subscribed by zero, one or multiple consumer groups. And a consumer group, similarly, may subscribe to one or more topics as long as instances of this group keep their subscription consistent.

topic是一個目錄,消息投遞的地方和消費消息獲取的地方。topics和生產者和消費者關聯不大。一個topic可以有0個,或者多個生產者向他投遞消息,一個消費者也可以發送消息到不同的topics。在消費者方面,topic可以有有0或者多個消費組訂閱。一個消費組也同樣可以訂閱一個或者多個topics,只要消費組內部實例保持訂閱一致性即可。

Message

Message is the information to be delivered. A message must have a topic, which can be interpreted as address of your letter to mail to. A message may also have an optional tag and extra key-value pairs. For example, you may set a business key to your message and look up the message on a broker server to diagnose issues during development.

消息必須有topic,topic就像是郵件的地址。消息也可以有tag和額外的KV對。例如,你可以給消息設置業務key,這樣在生產出問題時候可以查找問題。

Message msg = new Message("
TopicTest" /* Topic */,
"TagA" /* Tag */,
("Hello RocketMQ " ).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */);

Tag

Tag, in other words sub-topic, provides extra flexibility to users. With tag, messages with different purposes from the same business module may have the same topic and different tag. Tags would be helpful to keep your code clean and coherent, and tags also can facilitate the query system RocketMQ provides.

標籤,換句話說就是子話題,給用戶提供額外的靈活性。通過標籤,相同業務模塊的的消息出於不同的目的,可以定義相同的topic,不同的tag。標籤可以使代碼更潔淨,連貫,標籤也促進了RocketMQ提供的查詢系統。

Name Server

Name server serves as the routing information provider. Producer/Consumer clients look up topics to find the corresponding broker list.

作爲路由信息提供者,類似於kafka裏的zk,是輕量級的服務發現和路由。消費組和發送者客戶端從這裏查找topics對應的broker列表。

Name Server啓動和停止
進入到bin目錄下,運行namesrv,啓動NameServer。nohup sh mqnamesrv & , $ 表示後臺運行,默認監聽9876端口。
必須先啓動Name Server,再啓動Broker。

bin/mqshutdown namesrv 停止服務,必須先停止Broker,再停止Name Server。

Broker

Broker is a major component of the RocketMQ system. It receives messages sent from producers, store them and prepare to handle pull requests from consumers. It also stores message related meta data, including consumer groups, consuming progress offsets and topic / queue info.

broker是重要組件,接收消息並存儲消息,消費者從這裏拉取消息。並且存儲消息相關的元數據,包括消費組,消費進度偏移量和topic和queue信息。

Broker啓動和停止
nohup sh bin/mqbroker -n ${namesrvIp}:9876 -c /conf/broker.conf &

[-c可以指定broker.conf配置文件]。默認情況下會加載conf/broker.conf。

nohup sh mqbroker -n localhost:9876 &
啓動broker,其中-n表示指定當前broker對應的命名服務地址: 默認情況下,broker監聽的是10911端口。必須先啓動Name Server,再啓動Broker。

sh bin/mqshutdown broker
停止broker,必須先停止broker,再停止Name Server。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章