簡體   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