繁体   English   中英

从消息中心上的主题检索消息

[英]Retrieve messages from a topic on a message hub

我正在尝试使用Confluent Kafka Python从bluemix的消息中心上的某个主题获取消息。 我的代码在下面,但是有些不起作用。 主题和消息中心已经启动并正在运行,因此代码中可能包含一些内容。

from confluent_kafka import Producer, KafkaError, Consumer

consumer_settings = {
'bootstrap.servers': 'broker-url-here',
'group.id': 'mygroup',
'default.topic.config': {'auto.offset.reset': 'smallest'},
'sasl.mechanisms': 'PLAIN',
'security.protocol': 'ssl',
'sasl.username': 'username-here',
'sasl.password': 'password-here',
}

c = Consumer(**consumer_settings)
c.subscribe(['topic-here'])

running = True

while running:
    msg = c.poll()
    if msg.error():
        print("Error while retrieving message")
        c.close()
        sys.exit(10)
    elif (msg is not None):
        for x in msg:
            print(x)
    else:
        sys.exit(10)

当我运行代码时,似乎卡在msg = c.poll() 因此,我想它要么无法连接,要么无法检索消息。 凭据本身是正确的。

消费逻辑看起来不错,但消费者的配置不正确。

  • 需要将security.protocol设置为sasl_ssl
  • ssl.ca.location需要指向包含受信任证书的PEM文件。 该文件的位置因每个操作系统而异,但最常见的是:
    • Bluemix / Ubuntu: /etc/ssl/certs
    • 红帽: /etc/pki/tls/cert.pem
    • macOS: /etc/ssl/certs.pem

我们还有一个使用此客户端的示例应用程序,可以轻松启动或部署到Bluemix: https : //github.com/ibm-messaging/message-hub-samples/tree/master/kafka-python-console-sample

暂无
暂无

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

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