Python中具有相同组ID的多个消费者

时间:2017-10-21 18:52:56

标签: python apache-kafka kafka-python

有人知道如何在Python中运行具有相同组ID的多个消费者吗? 我试过了

a = Consumer({'bootstrap.servers': 'localhost:9092', 'group.id': 'dd1',
              'default.topic.config': {'auto.offset.reset': 'smallest'}})
b = Consumer({'bootstrap.servers': 'localhost:9092', 'group.id': 'dd1',
              'default.topic.config': {'auto.offset.reset': 'smallest'}})
c = Consumer({'bootstrap.servers': 'localhost:9092', 'group.id': 'dd1',
              'default.topic.config': {'auto.offset.reset': 'smallest'}})
a.subscribe([topic_to_read])
b.subscribe([topic_to_read])
c.subscribe([topic_to_read]) 
running = True
while running:
    msg1 = a.poll(timeout=timeout)
    msg2 = b.poll(timeout=timeout)
    msg3 = c.poll(timeout=timeout)

但这不起作用。 所以我尝试使用多处理lib,但我无法使其工作。

2 个答案:

答案 0 :(得分:0)

组ID是每个消费者的唯一ID。如果您与多个消费者订阅同一主题,则必须具有不同的组ID,否则只有其中一个消费者会收到消息。

答案 1 :(得分:0)

检查您对该主题的分区数。组ID中的使用者数量不应超过该组使用的主题的分区数。此外,额外的消费者将保持闲置状态。 如果你为每个消费者分配不同的客户/消费者,也要检查。