即使在成功连接并在kafka消费者控制台中获取消息之后,也无法使用来自kafka主题(使用Python)的消息[重复]

问题描述 投票:0回答:1

我有一个使用Kafka的Debezium设置。我可以按照文档中的描述使用来自kafka控制台的消息。但是,当我在本地创建使用Python的kafka使用者时,我无法使用消息。应该注意的是,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引导程序服务器创建一个使用者,但最终出现错误:

kafka.errors.NoBrokersAvailable: NoBrokersAvailable

我的本地是Mac OS。

仅供参考:我可以获得其他所有内容,例如主题:

>>> 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
python python-3.x apache-kafka kafka-consumer-api debezium
1个回答
0
投票

如果没有看到你的compose文件,localhost:9092可能无法在你的Python代码中使用docker命令

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

Connect to Kafka running in Docker from local machine

© www.soinside.com 2019 - 2024. All rights reserved.