简体   繁体   English

从heroku外部使用kafka-python连接到heroku kafka实例

[英]connect to a heroku kafka instance with kafka-python from outside heroku

I have set up a heroku kafka instance, and I am trying to connect to it using the python consumer. 我已经设置了一个heroku kafka实例,并且尝试使用python使用者连接到它。 I have the heroku environment in a file called .env by going heroku config -s > .env , and then load and export it before running this python program: 通过运行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)

I couldn't find any options that looked like they could load the certificates from a variable, so I put them all in files when I start the program. 我找不到任何看起来像它们可以从变量加载证书的选项,因此在启动程序时将它们全部放入文件中。

When I run the program, it creates the temp files and doesn't complain, but doesn't print any messages. 当我运行程序时,它会创建临时文件,并且不会抱怨,但是不会打印任何消息。

When I write to the topic using the heroku cli like this 当我像这样使用heroku cli撰写主题时

heroku kafka:topics:write test-topic "this is a test"

the python client doesn't print the message, but I can see the message by going python客户端不打印消息,但是我可以通过以下方式查看消息

heroku kafka:topics:tail test-topic

Does anybody know what I am missing in the python consumer configuration? 有人知道我在python使用者配置中缺少什么吗?

In the official Heroku Kafka documentation: 在Heroku Kafka官方文档中:

https://devcenter.heroku.com/articles/kafka-on-heroku#using-kafka-in-python-applications https://devcenter.heroku.com/articles/kafka-on-heroku#using-kafka-in-python-applications

it states that using the Kafka helper is beneficial. 它指出使用Kafka助手是有益的。 If you look at the source code: 如果您查看源代码:

https://github.com/heroku/kafka-helper/blob/master/kafka_helper.py https://github.com/heroku/kafka-helper/blob/master/kafka_helper.py

one can see that they are writing the Kafka variables to files and creating an ssl_context. 可以看到他们正在将Kafka变量写入文件并创建ssl_context。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM