如何解决Rabbitmq pika.BasicProperties 不在消息中发送属性
我正在使用 pika 向rabbitmq 发送消息。 我需要发送额外的属性,所以我使用 pika.BasicProperties,但是当我在 Wireshark 中看到此消息时,没有添加任何属性。
我的代码:
import pika,uuid
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='test_60',durable=True,arguments={'x-message-ttl' : 21600000})
routingKey = "test_server"
message = '{"test_int":268,"Timestamp":1610022012203}'
correlation_id = str(uuid.uuid4())
reply_to = "test_60"
message_type = "SendRequest"
channel.basic_publish(exchange='',routing_key=routingKey,body=message,properties=pika.BasicProperties(
headers={"testId": "60"},delivery_mode=2,correlation_id = correlation_id,reply_to = reply_to,type = message_type))
print('message sent')
print(correlation_id)
在 Wireshark 中,这条消息看起来像这样,所以没有属性,我不知道这个例子有什么问题。
解决方法
prop = pika.BasicProperties(
content_type='application/json',content_encoding='utf-8',headers={'key': 'value'},delivery_mode = 1,)
channel.basic_publish(
exchange='',routing_key=qname,properties=prop,body='{message: hello}'
)
界面:
Wireshark:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。