即使成功连接并在kafka consumer consumer cons中获取消息,也无法使用kafka topic中的消息(使用Python)

2024-06-24 11:55:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个德贝齐姆的装置,用卡夫卡。我可以使用来自kafka控制台的消息,如doc中所述。但是,当我在本地使用Python创建kafka消费者时,我无法消费消息。需要注意的是,卡夫卡控制台工作得很好!在

我试着调查这个问题,但没有找到类似的环境/情况

我要连接的python代码是:

from kafka import KafkaConsumer
consumer = KafkaConsumer('dbserver1.inventory.customers', group_id='my-group', bootstrap_servers=['localhost:9092'], auto_offset_reset='earliest')
for message in consumer:
    print(message)

不管现有消息还是推送到此主题的新消息,此消息都将变为空白。在

我确信这些消息是存在的,因为当我打开一个控制台消费者时,我可以看到这些消息。在

只是想弄清楚整个设置: 我遵循了(https://github.com/debezium/debezium-examples/tree/master/tutorial#using-mongodb)文档的每一步(除了最后一步)。 除了我的Python代码之外,一切正常。 我还尝试使用kafka:9092引导服务器创建一个使用者,但结果出现了一个错误:

^{pr2}$

我的本地电脑是macos。在

仅供参考: 我能得到其他一切,比如主题:

>>> consumer = KafkaConsumer('dbserver1.inventory.customers', group_id='my-group', bootstrap_servers=['localhost:9092'], auto_offset_reset='earliest')
>>> consumer.topics()
{'my_connect_offsets', 'my_connect_configs', 'dbserver1.inventory.orders', 'connect-status', 'dbserver1.inventory.customers', 'dbserver1.inventory.products'}

我通过命令启动消费者:

docker-compose -f debezium-mongodb.yaml exec kafka /kafka/bin/kafka-console-consumer.sh \
    --bootstrap-server kafka:9092 \
    --from-beginning \
    --property print.key=true \
    --topic dbserver1.inventory.customers

Tags: kafka代码from消息consumermyconnectgroup
1条回答
网友
1楼 · 发布于 2024-06-24 11:55:39

如果看不到compose文件,localhost:9092很可能无法在基于docker命令的Python代码中工作

  1. 如果Python代码不在容器中运行,则需要从其他端口读取。如果它在容器中运行,则必须使用kafka:9092
  2. 您使用的端口取决于容器的播发侦听器

Connect to Kafka running in Docker from local machine

相关问题 更多 >