简体   繁体   English

使用 pika 时消息队列中的 exchange 和 routing_key 是什么?

[英]What is exchange and routing_key in a message queue while using pika?

I was setting uprabitMQ as message queue to use with my Django application.我将 uprabitMQ 设置为与我的 Django 应用程序一起使用的消息队列。 According to the documentation I need to specify the exchange and routing key to the queue.根据文档,我需要指定队列的交换和路由键。 What I don't understand is what exactly are both of them and what do we use it for.我不明白它们到底是什么以及我们用它做什么。 Also can one queue only have one routing key?一个队列也可以只有一个路由键吗?

#!/usr/bin/env python
import pika
credentials = pika.PlainCredentials('username', 'password')
parameters = pika.ConnectionParameters('IPADDRESS', 5672, 'vhostcheck', credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue='testqueue')
channel.basic_publish(exchange='',
                  routing_key='hello',
                  body='Hello W0rld!!')
print("Sent 'Hello World!'")
connection.close()

In channel.basic_publish(exchange='', routing_key='hello', body='Hello W0rld!!') , what are exchange and routing_key and what do we use it for?channel.basic_publish(exchange='', routing_key='hello', body='Hello W0rld!!')中, exchangerouting_key是什么,我们用它做什么?

This is explained nicely in the relevant documentation( https://www.rabbitmq.com/tutorials/tutorial-one-python.html ).这在相关文档( https://www.rabbitmq.com/tutorials/tutorial-one-python.html )中有很好的解释。 For exchange='': this is the "default exchange identified by an empty string. This exchange is special ‒ it allows us to specify exactly to which queue the message should go".对于 exchange='':这是“由空字符串标识的默认交换。这种交换很特殊——它允许我们准确指定消息应该去哪个队列”。 And for routing_key='hello': "the queue name needs to be specified in the routing_key parameter".而对于 routing_key='hello':“需要在 routing_key 参数中指定队列名称”。 The documentation also explains the related terms clearly, such as "exchange", "queue", and so on.文档中还对相关术语进行了清晰的解释,例如“exchange”、“queue”等。 This is one of the simplest possible configurations for Rabbit - just to get you started.这是 Rabbit 最简单的配置之一 - 只是为了让您入门。

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

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