Skip to content
目录概览

RocketMQ如何做负载均衡?

通过Topic在多Broker中分布式存储实现。

producer端

发送端指定 message queue发送消息到相应的 broker,来达到写入时的负载均衡

  • 提升写入吞吐量,当多个producer同时向一个 broker 写入数据的时候,性能会下降
  • 消息分布在多 broker 中,为负载消费做准备

默认策略是随机选择:

  • producer 维护一个 index
  • 每次取节点会自增
  • index 向所有 broker 个数取余
  • 自带容错策略

其他实现:

  • SelectMessageQueueByHash

  • hash的是传入的args

  • SelectMessageQueueByRandom

  • SelectMessageQueueByMachineRoom 没有实现

也可以自定义实现MessageQueueSelector接口中的select方法

java
MessageQueue select(final List<MessageQueue> mqs, final Message msg, final Object arg);
1

consumer端

采用的是平均分配算法来进行负载均衡。

其他负载均衡算法

  • 平均分配策略(默认)(AllocateMessageQueueAveragely)
  • 环形分配策略(AllocateMessageQueueAveragelyByCircle)
  • 手动配置分配策略(AllocateMessageQueueByConfig)
  • 机房分配策略(AllocateMessageQueueByMachineRoom)
  • 一致性哈希分配策略(AllocateMessageQueueConsistentHash)
  • 靠近机房策略(AllocateMachineRoomNearby)