一、消费端请求消费消息
1、每次请求之前都会计算请求broker组中的哪个broker(0为master、非0为slave)
/*
* PullAPIWrapper#pullKernelImpl
*/
FindBrokerResult findBrokerResult =
this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),
this.recalculatePullFromWhichNode(mq), false);
if (null == findBrokerResult) {
this.mQClientFactory.updateTopicRouteInfoFromNameServer(mq.getTopic());
findBrokerResult = this.mQClientFactory.findBrokerAddressInSubscribe(mq.getBrokerName(),
this.recalculatePullFromWhichNode(mq), false);
}
二、broker端获取消息
1、broker在获取消息的时候会计算消息队列消费偏移量相对与commitLog存储偏移量滞后了多少,如果滞后超过一定阈值,则告知消费者下次从slave节点获取消息
// DefaultMessageStore#getMessage
// 消费队列相对于commitLog滞后的量
long diff = maxOffsetPy - maxPhyOffsetPulling;
// broker物理内存的阈值
long memory = (long) (StoreUtil.TOTAL_PHYSICAL_MEMORY_SIZE *
(this.messageStoreConfig.getAccessMessageInMemoryMaxRatio() / 100.0));
getResult.setSuggestPullingFromSlave(diff > memory);