【问题标题】:Consumer group member has no partition消费者组成员没有分区
【发布时间】:2019-01-13 04:04:33
【问题描述】:

我在同一个消费者组上启动了两个消费者,我订阅了 20 个主题(每个主题只有一个分区)

仅在消费者上使用:

kafka-consumer-groups --bootstrap-server XXXXX:9092 --group foo --describe --members --verbose

Note: This will not show information about old Zookeeper-based consumers.

CONSUMER-ID                                  HOST            CLIENT-ID       #PARTITIONS     ASSIGNMENT
rdkafka-07cbd673-6a16-4d55-9625-7f0925866540 /xxxxx rdkafka         20              arretsBus(0), capteurMeteo(0), capteurPointMesure(0), chantier(0), coworking(0), horodateur(
0), incident(0), livraison(0), meteo(0), metro(0), parkrelais(0), qair(0), rhdata(0), sensUnique(0), trafic(0), tramway(0), tweets(0), voieRapide(0), zone30(0), zoneRencontre(0)
rdkafka-9a543197-6c97-4213-bd59-cb5a48e4ec15 /xxxx    rdkafka         0 

我做错了什么?

【问题讨论】:

  • 您能否检查两者是否订阅了同一组主题?此外,如果可能,通过在命令末尾添加 --verbose 来更新结果。
  • 我更新了详细信息,是的,他们订阅了两个消费者的 20 个主题,事实上,相同的代码在 kubernetes 上部署了两次。也许是因为他们只有一个分区?

标签: apache-kafka kafka-consumer-api


【解决方案1】:

好的,我对这种行为进行了一些阅读,知道它为什么会发生很有趣。 Kafka有两种分区分配策略。

  • 范围: Assigns to each consumer a consecutive subset of partitions from each topic it subscribes to. So if consumers C1 and C2 are subscribed to two topics, T1 and T2, and each of the topics has three partitions, then C1 will be assigned partitions 0 and 1 from topics T1 and T2, while C2 will be assigned partition 2 from those topics. Because each topic has an uneven number of partitions and the assignment is done for each topic independently, the first consumer ends up with more partitions than the second. This happens whenever Range assignment is used and the number of consumers does not divide the number of partitions in each topic neatly.

  • 循环: Takes all the partitions from all subscribed topics and assigns them to consumers sequentially, one by one. If C1 and C2 described previously used RoundRobin assignment, C1 would have partitions 0 and 2 from topic T1 and partition 1 from topic T2. C2 would have partition 1 from topic T1 and partitions 0 and 2 from topic T2. In general, if all consumers are subscribed to the same topics (a very common scenario), RoundRobin assignment will end up with all consumers having the same number of partitions (or at most 1 partition difference).

默认策略是 Range,这解释了为什么您会看到这样的分区分布。

所以,我做了一个小实验。我创建了两个控制台消费者,每个消费者都在听主题test1, test2, test3, test4,每个主题只有一个分区。正如预期的那样,消费者 1 被分配了所有分区。

然后我将分区策略更改为 org.apache.kafka.clients.consumer.RoundRobinAssignor 并将其传递给控制台消费者,瞧,现在两个消费者都得到了 2 个分区。

更新: 哎呀没看到它已经在几分钟前得到了回答。

【讨论】:

  • 谢谢更好的答案 ;)
【解决方案2】:

好的,我找到了问题,它可以使用:

'partition.assignment.strategy': 'roundrobin'

CONSUMER-ID                                  HOST            CLIENT-ID       #PARTITIONS     ASSIGNMENT
rdkafka-fa7ec1ca-1c34-498b-bd22-24ad6ca99645 /XXXX  rdkafka         10              capteurPointMesure(0), meteo(0), metro(0), parkrelais(0), qair(0), sensUnique(0), tweets(0),
 voieRapide(0), zone30(0), zoneRencontre(0)
rdkafka-89f765b6-2014-4b8c-bef2-c6406763118b /XXXX    rdkafka         10              arretsBus(0), capteurMeteo(0), chantier(0), coworking(0), horodateur(0), incident(0), livrai
son(0), rhdata(0), trafic(0), tramway(0)

每个主题的范围策略工作,循环我有预期的结果。

【讨论】:

  • 是的,循环分配器将跨不同主题的所有分区视为一个完整的分区组。
【解决方案3】:

在 Apache Kafka 中,分区数定义了您希望在同一消费者组中的消费者的并行级别;这意味着作为同一个消费者组的一部分的两个消费者不能从同一个分区中读取。 在您的情况下,您的主题只有一个分区,该分区将仅分配给一个消费者,而另一个将只是空闲等待重新平衡:这意味着如果第一个消费者断开连接,第二个消费者将从空闲转移到消费划分。 如果您的期望是为每个消费者获得 10 个主题,那么 Apache Kafka 就不是这样工作的。正如我所说,并行单元是主题中的分区,而不是主题本身。

【讨论】:

    【解决方案4】:

    在 Kafka 中,一个主题/分区最多只能被一个消费者组中的一个消费者消费,以避免消费者之间的竞争。

    【讨论】:

    • 当然,但我希望每个消费者获得 10 个主题
    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 2017-01-04
    • 2017-05-11
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多