gpt4 book ai didi

apache-kafka - kafka-python:生产者无法连接

转载 作者:行者123 更新时间:2023-12-04 00:03:00 24 4
gpt4 key购买 nike

kafka-python (1.0.0) 在连接到代理时抛出错误。
同时/usr/bin/kafka-console-producer 和/usr/bin/kafka-console-consumer 工作正常。

Python 应用程序以前也运行良好,但是在 zookeeper 重启后,它不再连接。

我正在使用文档中的裸骨示例:

from kafka import KafkaProducer
from kafka.common import KafkaError

producer = KafkaProducer(bootstrap_servers=['hostname:9092'])

# Asynchronous by default
future = producer.send('test-topic', b'raw_bytes')

我收到此错误:
Traceback (most recent call last):   File "pp.py", line 4, in <module>
producer = KafkaProducer(bootstrap_servers=['hostname:9092']) File "/usr/lib/python2.6/site-packages/kafka/producer/kafka.py", line 246, in __init__
self.config['api_version'] = client.check_version() File "/usr/lib/python2.6/site-packages/kafka/client_async.py", line 629, in check_version
connect(node_id) File "/usr/lib/python2.6/site-packages/kafka/client_async.py", line 592, in connect
raise Errors.NodeNotReadyError(node_id) kafka.common.NodeNotReadyError: 0 Exception AttributeError: "'KafkaProducer' object has no attribute '_closed'" in <bound method KafkaProducer.__del__ of <kafka.producer.kafka.KafkaProducer object at 0x7f6171294c50>> ignored

在逐步执行 (/usr/lib/python2.6/site-packages/kafka/client_async.py) 时,我注意到第 270 行评估为 false:
270         if not self._metadata_refresh_in_progress and not self.cluster.ttl() == 0:
271 if self._can_send_request(node_id):
272 return True
273 return False

在我的情况下 self._metadata_refresh_in_progress 是假的,但 ttl() = 0;

同时 kafka-console-* 正在愉快地推送消息:
/usr/bin/kafka-console-producer --broker-list hostname:9092 --topic test-topic
hello again
hello2

有什么建议吗?

最佳答案

我遇到了同样的问题,上述解决方案均无效。然后我阅读了异常消息,似乎必须指定 api_version,所以

producer = KafkaProducer(bootstrap_servers=['localhost:9092'],api_version=(0,1,0))

note: tuple (1,0,0) matching to kafka version 1.0.0



工作正常(至少无一异常(exception)地完成,现在必须说服它接受消息;))

关于apache-kafka - kafka-python:生产者无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35689238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com