Kafka分区分配策略其中之一:RoundRobinAssignor

时间:2024-02-25 17:01:08

之所以写这个文章的原因,因为在看kafka的视频时对RoundRobinAssignor策略产生了疑惑,之后上网看博客发现了两种不同的描述。

这两种说法不知道谁对谁错,所以,我决定去看官方文档(划重点:有问题,看官方文档)

官方文档翻了个遍,也没有找到很详细的说法,所以使出了第二招,那就是看API。就这个地址了:org.apache.kafka.clients.consumer.RoundRobinAssignor。里面是kafka最新的2.3.x版本的描述,我也看了0.10.2版本的,都是一样的,只是2.3.x文档排版更好。

下面关于RoundRobinAssignor策略的描述摘自上面的链接:

When subscriptions differ across consumer instances, the assignment process still considers each consumer instance in round robin fashion but skips over an instance if it is not subscribed to the topic. Unlike the case when subscriptions are identical, this can result in imbalanced assignments. For example, we have three consumers C0, C1, C2, and three topics t0, t1, t2, with 1, 2, and 3 partitions, respectively. Therefore, the partitions are t0p0, t1p0, t1p1, t2p0, t2p1, t2p2. C0 is subscribed to t0; C1 is subscribed to t0, t1; and C2 is subscribed to t0, t1, t2.
 
That assignment will be:

  • C0: [t0p0]
  • C1: [t1p0]
  • C2: [t1p1, t2p0, t2p1, t2p2]

结论
RoundRobinAssignor分区分配策略不会将Topic中的message发给consumer group的中没有订阅这个Topic的消费者。