自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試官:RocketMQ 長輪詢是怎么實現(xiàn)的?

數(shù)據(jù)庫 其他數(shù)據(jù)庫
長輪詢可以降低無效的輪詢請求,提升請求效率。RocketMQ 消費者長輪詢支持配置,當消息量不太大,消費者沒有必要頻繁地請求,這時可以設(shè)置成長輪詢機制。需要注意的是,消費端設(shè)置的請求超時時間必須大于 Broker 輪詢時間。

大家好,我是君哥。

我們知道,消息隊列消費端獲取消息的方式包括推模式和拉模式,RocketMQ 并沒有實現(xiàn)推模式,RocketMQ 的推模式本質(zhì)上也是拉模式。他們在實現(xiàn)上有下面的不同:

  • 拉模式需要開發(fā)在代碼里調(diào)用拉取消息的方法,拉取到消息后直接進行消息處理;
  • 推模式是消費者客戶端初始化時利用重平衡線程去拉取消息,拉取消息的方法會注冊回調(diào)函數(shù),拉取到消息后,由回調(diào)函數(shù)觸發(fā)監(jiān)聽器(定義處理邏輯)進行消息處理。

RocketMQ 為了提供拉取消息的效率,采用了長輪詢機制,避免消費端無效的輪詢請求。當消費者發(fā)送長輪詢請求后,如果 Broker 上沒有新消息,則不會立刻返回,而是掛起請求,等待新消息到來或者請求超時。

今天來聊一聊 RocketMQ 的長輪詢是怎么實現(xiàn)的。

1 長輪詢

長輪詢的流程如下圖:

圖片圖片

客戶端建立連接后,發(fā)送消息拉取請求,如果服務(wù)端有新消息,則返回消息。如果服務(wù)端沒有新消息,則掛起連接,等待新消息到來后給客戶端返回??蛻舳巳绻B接超時,則斷開連接。

2 RocketMQ 實現(xiàn)

2.1 消費端

RocketMQ 消費端長輪詢有 2 個超時設(shè)置:

  • brokerSuspendMaxTimeMillis:長輪詢,Consumer 拉消息請求在 Broker 掛起超過這個時間,就會給消費端返回響應(yīng),無論有沒有新消息,單位毫秒。這個參數(shù)消費端發(fā)送拉取請求時會發(fā)給 Broker,Broker 用來判斷這個長連接是否超時。
  • consumerTimeoutMillisWhenSuspend:消費端發(fā)送拉取請求的超時時間,這個時間要大于 brokerSuspendMaxTimeMillis,客戶端初始化時會有校驗。

注意,這 2 個超時時間官方都不推薦修改。

if (this.defaultLitePullConsumer.getConsumerTimeoutMillisWhenSuspend() < this.defaultLitePullConsumer.getBrokerSuspendMaxTimeMillis()) {
 throw new MQClientException(
  "Long polling mode, the consumer consumerTimeoutMillisWhenSuspend must greater than brokerSuspendMaxTimeMillis"
   + FAQUrl.suggestTodo(FAQUrl.CLIENT_PARAMETER_CHECK_URL),
  null);
}

2.2 Broker

RocketMQ 在 Broker 端通過設(shè)置 longPollingEnable 來開啟長輪詢,默認是開啟。

Broker 長輪詢掛起時間使用 suspendTimeoutMillis 來進行控制,前面提到過,這個時間由消費者發(fā)送的 brokerSuspendMaxTimeMillis 參數(shù)來賦值。

2.2.1 掛起消息

Broker 收到客戶端拉取消息請求后,如果沒有新消息,則將請求掛起,也就是將請求放到 pullRequestTable。

//PullMessageProcessor#processRequest
case ResponseCode.PULL_NOT_FOUND:

if (brokerAllowSuspend && hasSuspendFlag) {
//suspendTimeoutMillisLong 這個參數(shù)就是消費端發(fā)來的 consumerTimeoutMillisWhenSuspend
long pollingTimeMills = suspendTimeoutMillisLong;
if (!this.brokerController.getBrokerConfig().isLongPollingEnable()) {
   pollingTimeMills = this.brokerController.getBrokerConfig().getShortPollingTimeMills();
  }

  String topic = requestHeader.getTopic();
long offset = requestHeader.getQueueOffset();
int queueId = requestHeader.getQueueId();
  PullRequest pullRequest = new PullRequest(request, channel, pollingTimeMills,
   this.brokerController.getMessageStore().now(), offset, subscriptionData, messageFilter);
//這里掛起消息
this.brokerController.getPullRequestHoldService().suspendPullRequest(topic, queueId, pullRequest);
  response = null;
break;
 }

上面的 suspendPullRequest 調(diào)用了 PullRequestHoldService#suspendPullRequest,將請求保存在 pullRequestTable。

2.2.2 處理掛起

消息掛起后,后面怎么恢復(fù)呢?這里總需要一個線程去循環(huán)處理掛起的消息,這個處理邏輯也在 PullRequestHoldService,看下面代碼:

public void run() {
 log.info("{} service started", this.getServiceName());
while (!this.isStopped()) {
try {
   //長輪詢模式,等待 5s 后處理
   if (this.brokerController.getBrokerConfig().isLongPollingEnable()) {
    this.waitForRunning(5 * 1000);
   } //...
   //這里處理被掛起的請求
   this.checkHoldRequest();
  } catch (Throwable e) {
   log.warn(this.getServiceName() + " service has exception. ", e);
  }
 }//...
}

處理請求的邏輯參考下面代碼:

protected void checkHoldRequest() {
for (String key : this.pullRequestTable.keySet()) {
  String[] kArray = key.split(TOPIC_QUEUEID_SEPARATOR);
if (2 == kArray.length) {
   String topic = kArray[0];
   int queueId = Integer.parseInt(kArray[1]);
   finallong offset = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, queueId);
   try {
    this.notifyMessageArriving(topic, queueId, offset);
   } catch (Throwable e) {
    log.error("check hold request failed. topic={}, queueId={}", topic, queueId, e);
   }
  }
 }
}

notifyMessageArriving 方法邏輯如下:

  1. 如果當前請求有新消息到來,則給消費者返回響應(yīng);
  2. 如果當前請求沒有新消息,但是掛起請求已經(jīng)超時,則給消費者返回響應(yīng);
  3. 否則, 繼續(xù)掛起,等待 5s 后重復(fù)執(zhí)行上面邏輯。

3 總結(jié)

長輪詢可以降低無效的輪詢請求,提升請求效率。RocketMQ 消費者長輪詢支持配置,當消息量不太大,消費者沒有必要頻繁地請求,這時可以設(shè)置成長輪詢機制。需要注意的是,消費端設(shè)置的請求超時時間必須大于 Broker 輪詢時間。


責任編輯:武曉燕 來源: 君哥聊技術(shù)
相關(guān)推薦

2022-12-05 10:47:08

RocketMQ灰度消息

2024-02-04 10:08:34

2024-12-25 15:44:15

2023-02-08 07:04:20

死鎖面試官單元

2024-10-15 10:00:06

2021-09-27 07:11:18

MySQLACID特性

2025-02-26 12:19:52

2021-09-07 10:44:33

Java 注解開發(fā)

2025-04-08 00:00:00

@AsyncSpring異步

2021-02-19 10:02:57

HTTPSJava安全

2023-02-08 08:32:41

輪詢鎖

2021-11-02 09:05:25

Redis

2024-08-22 10:39:50

@Async注解代理

2024-03-05 10:33:39

AOPSpring編程

2025-03-07 00:00:10

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2020-12-09 10:29:53

SSH加密數(shù)據(jù)安全

2024-09-11 22:51:19

線程通訊Object

2023-11-20 10:09:59

2017-03-16 15:27:10

面試官測試技術(shù)
點贊
收藏

51CTO技術(shù)棧公眾號