[英]Why kafka-python fails to connect to Bluemix message hub service?
[英]connect to a heroku kafka instance with kafka-python from outside heroku
我已经设置了一个heroku kafka实例,并且尝试使用python使用者连接到它。 通过运行heroku config -s > .env
,我在名为.env
的文件中拥有heroku环境,然后在运行此python程序之前加载并导出它:
import os
from kafka import KafkaConsumer
for variable in ['KAFKA_TRUSTED_CERT', 'KAFKA_CLIENT_CERT', 'KAFKA_CLIENT_CERT_KEY']:
with open(f'{variable}.txt', "w") as text_file:
print(os.environ[variable], file=text_file)
consumer = KafkaConsumer('test-topic',
bootstrap_servers=os.environ['KAFKA_URL'],
security_protocol="SSL",
ssl_certfile="KAFKA_CLIENT_CERT.txt",
ssl_keyfile="KAFKA_CLIENT_CERT_KEY.txt"
)
for msg in consumer:
print (msg)
我找不到任何看起来像它们可以从变量加载证书的选项,因此在启动程序时将它们全部放入文件中。
当我运行程序时,它会创建临时文件,并且不会抱怨,但是不会打印任何消息。
当我像这样使用heroku cli撰写主题时
heroku kafka:topics:write test-topic "this is a test"
python客户端不打印消息,但是我可以通过以下方式查看消息
heroku kafka:topics:tail test-topic
有人知道我在python使用者配置中缺少什么吗?
在Heroku Kafka官方文档中:
https://devcenter.heroku.com/articles/kafka-on-heroku#using-kafka-in-python-applications
它指出使用Kafka助手是有益的。 如果您查看源代码:
https://github.com/heroku/kafka-helper/blob/master/kafka_helper.py
可以看到他们正在将Kafka变量写入文件并创建ssl_context。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.