我尝试使用python和质子库作为订阅者(用于接收消息)连接到azure事件中心。
我找到了一个示例代码,最终得到了:
import sys
import optparse
from proton import *
import urllib
mng = Messenger()
mng.start()
nb_partitions = 8
sasKeyName = "NAME"
sasPolicyKey = "KEY"
# safeSasPolicyKey = urllib.quote(sasPolicyKey, "")
safeSasPolicyKey = sasPolicyKey
args = []
for i in range(0, nb_partitions):
address = "amqps://%s:%s@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default" % (
sasKeyName, safeSasPolicyKey)
args.append(address + "/Partitions/" + str(i))
print(args)
for a in args:
# mng.subscribe(a)
mng.subscribe(a)
print "Subscribed to %s" % (a)
msg = Message()
while True:
mng.recv()
while mng.incoming:
try:
mng.get(msg)
except Exception, e:
print e
else:
print msg.address, msg.subject or "(no subject)", msg.properties, msg.body
我使用pip安装python-qpid-proton(0.10)在macosx上运行此代码。
但是我无法获得事件中心的任何消息我知道我每分钟都会从不同的脚本发送消息。 我得到了我打印的以下输出
订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/0 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/1 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/2 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/3 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/4 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/5 订阅 amqps://名称:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/6 订阅 amqps:// NAME:KEY@clienteventhub-ns.servicebus.windows.net/NAME/ConsumerGroups/$Default/Partitions/7
你知道为什么这段代码无效吗? 感谢
答案 0 :(得分:0)
您可能有多种原因无法接收消息。 这也可能是请确保事件中心使用的出站端口5671/9352 未被防火墙阻止的原因之一。
请参阅此link
答案 1 :(得分:-1)
更改消费者群组 - > $Default
至$default
address = "amqps://%s:%s@%s.servicebus.windows.net/%s/ConsumerGroups/$default" % (sasKeyName, safeSasPolicyKey,name_space,eventhub)