发送给具有相同消费者组名称的所有消费者的消息 [英] Messages sent to all consumers with the same consumer group name

查看:35
本文介绍了发送给具有相同消费者组名称的所有消费者的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下消费者代码:

from kafka.client import KafkaClient
from kafka.consumer import SimpleConsumer

kafka = KafkaClient("localhost", 9092)

consumer = SimpleConsumer(kafka, "my-group", "my-topic")
consumer.seek(0, 2)
for message in consumer:
  print message

kafka.close()

然后我用脚本生成消息:

Then I produce message with script:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-topic

问题是,当我将消费者作为两个不同的进程启动时,我会在每个进程中收到新消息.但是我希望它只发送给一个消费者,而不是广播.

The thing is that when I start consumers as two different processes then I receive new messages in each process. However I want it to be sent to only one consumer, not broadcasted.

在 Kafka 文档中(https://kafka.apache.org/documentation.html) 有写:

In documentation of Kafka (https://kafka.apache.org/documentation.html) there is written:

如果所有的消费者实例都有相同的消费者组,那么这个就像传统的队列平衡消费者负载一样.

If all the consumer instances have the same consumer group, then this works just like a traditional queue balancing load over the consumers.

我看到这些消费者的组是相同的 - my-group.

I see that group for these consumers is the same - my-group.

如何让新消息只被一个消费者读取而不是广播?

How to make it so that new message is read by exactly one consumer instead of broadcasting it?

推荐答案

直到 kafka v. 0.8.1(2014 年 3 月 12 日发布)才正式支持消费者组 API.对于之前的服务器版本,消费者组无法正常工作.在这篇文章中,kafka-python 库目前不会尝试发送组偏移数据:

the consumer-group API was not officially supported until kafka v. 0.8.1 (released Mar 12, 2014). For server versions prior, consumer groups do not work correctly. And as of this post the kafka-python library does not currently attempt to send group offset data:

https://github.com/mumrah/kafka-python/blob/c9d9d0aad2447bb8bad0e62c97365e5101001e4b/kafka/consumer.py#L108-L115

这篇关于发送给具有相同消费者组名称的所有消费者的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆