【问题标题】:Retrieve messages from a topic on a message hub从消息中心的主题中检索消息
【发布时间】:2018-08-25 14:41:22
【问题描述】:

我正在尝试使用 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()。所以我猜它要么无法连接,要么无法检索消息。凭据本身是正确的。

【问题讨论】:

    标签: python apache-kafka ibm-cloud confluent-platform message-hub


    【解决方案1】:

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

    • 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

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2019-06-08
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多