简体   繁体   English

消费者的Kafka主题和分区分配

[英]Kafka Topic and Partition allocation for consumer

I just started working on kafka, I need to develop a consumer client using sarama go pkg, the client is supposed to be a part of consumer group, and is needed to read from two topic A and B, client needs to read from some partitions of topic A allocated to it by any balance strategy and for B it needs to read from all partition (B is kinda like brodcast topic).我刚开始研究kafka,我需要使用sarama go pkg开发一个消费者客户端,客户端应该是消费者组的一部分,需要从两个主题A和B中读取,客户端需要从一些分区中读取任何平衡策略分配给它的主题 A 和 B 它需要从所有分区中读取(B 有点像广播主题)。

Workflow:工作流程:

  • consumer group xx.消费群xx。
  • I have two topic A and B with 6 partition [0,1,2...5] each.我有两个主题 A 和 B,每个有 6 个分区 [0,1,2...5]。
  • I have two consumer C1 and C2 in xx, data should be read in such a way:我在xx中有两个消费者C1和C2,数据应该这样读取:
    C1 reads from A:[0,1,2] and from B:[0,1,2,3,4,5,6] C1 从 A:[0,1,2] 和 B:[0,1,2,3,4,5,6] 读取
    C2 reads from A:[3,4,5] and from B:[0,1,2,3,4,5,6] C2 从 A:[3,4,5] 和 B:[0,1,2,3,4,5,6] 读取

note: in case an new client is added the partition in A should be rebalance and all partition in B should be read.注意:如果添加了新客户端,则应重新平衡 A 中的分区,并读取 B 中的所有分区。

I tried implementing my custom balance strategy but failed, Please let me know if this can be done and how to do it.我尝试实施我的自定义平衡策略但失败了,请让我知道这是否可以完成以及如何做到这一点。

For any consumer in the same consumer group, it is not possible for multiple to be listening to any overlapping partitions.对于同一消费者组中的任何消费者,多个消费者不可能监听任何重叠的分区。 In other words, listening to all partitions of topic B cannot be done unless you move C2 consumer to its own unique group, regardless of the rebalancing strategy for consumer groups for topic A.换句话说,除非您将 C2 消费者移动到其自己的唯一组,否则无法监听主题 B 的所有分区,而不管主题 A 的消费者组的再平衡策略如何。

You need to implement both Partition Consumer and Group Consumer in your service.您需要在服务中同时实现分区消费者和组消费者。

1. Group Consumer 1. 团体消费者

Use Group Consumer to consume messages from topic "A".使用 Group Consumer 消费来自主题“A”的消息。 You can implement ConsumerGroup interface in Sarama library.您可以在 Sarama 库中实现ConsumerGroup接口。 Both your consumers "C1" and "C2" need to subscribe to topic "A" as a group (using the same client ID).您的消费者“C1”和“C2”都需要作为一个组订阅主题“A”(使用相同的客户端 ID)。

2. Partition Consumer 2.分区消费者

Use Partition consumers to consume messages in topic "B".使用分区消费者来消费主题“B”中的消息。 Use Consumer interface in Sarama library for this.为此,请使用 Sarama 库中的Consumer接口。 "C1" and "C2" need to subscribe to all partitions in starting up and also after a rebalance. “C1”和“C2”需要在启动和重新平衡之后订阅所有分区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM