简体   繁体   English

如何使用 Pika 发送和接收 RabbitMQ 消息?

[英]How can I use Pika to send and receive RabbitMQ messages?

I'm having some issue getting Pika to work with routing keys or exchanges in a way that's consistent with it AMQP or RabbitMQ documentation.我在让 Pika 以与 AMQP 或 RabbitMQ 文档一致的方式使用路由密钥或交换时遇到了一些问题。 I understand that the RabbitMQ documentation uses an older version of Pika, so I have disregarded their example code.我知道 RabbitMQ 文档使用旧版本的 Pika,所以我忽略了他们的示例代码。

What I'm trying to do is define a queue, "order" and have two consumers, one that handle the exchange or routing_key "production" and one that handles "test".我想要做的是定义一个队列,“订单”并有两个消费者,一个处理交换或 routing_key“生产”,一个处理“测试”。 From looking at that RabbitMQ documentation that should be easy enough to do by using either a direct exchange and routing keys or by using a topic exchange.通过查看 RabbitMQ 文档,通过使用直接交换和路由键或使用主题交换应该很容易做到。

Pika however doesn't appear to know what to do with the exchanges and routing keys.然而,Pika 似乎不知道如何处理交换和路由密钥。 Using the RabbitMQ management tool to inspect the queues, it's pretty obvious that Pika either didn't queue the message correctly or that RabbitMQ just threw it away.使用 RabbitMQ 管理工具检查队列,很明显 Pika 没有正确地对消息进行排队,或者 RabbitMQ 只是将其丢弃。

On the consumer side it isn't really clear how I should bind a consumer to an exchange or handle routing keys and the documentation isn't really helping.在消费者方面,我并不清楚我应该如何将消费者绑定到交换或处理路由密钥,并且文档并没有真正帮助。

If I drop all ideas or exchanges and routing keys, messages queue up nicely and are easily handled by my consumer.如果我放弃所有的想法或交换和路由键,消息会很好地排队并且很容易被我的消费者处理。

Any pointers or example code people have would be nice.人们拥有的任何指针或示例代码都会很好。

As it turns out, my understanding of AMQP was incomplete. 事实证明,我对AMQP的理解是不完整的。

The idea is as following: 这个想法如下:

Client : 客户

The client after getting the connection should not care about anything else but the name of the exchange and the routing key. 获得连接后的客户端不应该关心除交换机名称和路由密钥之外的任何其他内容。 That is we don't know which queue this will end up in. 那就是我们不知道最终会进入哪个队列。

channel.basic_publish(exchange='order',
                      routing_key="order.test.customer",
                      body=pickle.dumps(data),
                      properties=pika.BasicProperties(
                          content_type="text/plain",
                          delivery_mode=2))

Consumer 消费者

When the channel is open, we declare the exchange and queue 当通道打开时,我们声明交换和队列

channel.exchange_declare(exchange='order', 
                         type="topic", 
                         durable=True, 
                         auto_delete=False)

channel.queue_declare(queue="test", 
                      durable=True, 
                      exclusive=False, 
                      auto_delete=False, 
                      callback=on_queue_declared)

When the queue is ready, in the "on_queue_declared" callback is a good place, we can bind the queue to the exchange, using our desired routing key. 当队列准备就绪时,在“on_queue_declared”回调是个好地方,我们可以使用我们想要的路由键将队列绑定到交换机。

channel.queue_bind(queue='test', 
                   exchange='order', 
                   routing_key='order.test.customer')

#handle_delivery is the callback that will actually pickup and handle messages
#from the "test" queue
channel.basic_consume(handle_delivery, queue='test') 

Messages send to the "order" exchange with the routing key "order.test.customer" will now be routed to the "test" queue, where the consumer can pick it up. 使用路由密钥“order.test.customer”发送到“订单”交换的消息现在将被路由到“测试”队列,消费者可以在其中获取它。

While Simon's answer seems right in general, you might need to swap the parameters for consuming虽然 Simon 的回答一般来说似乎是正确的,但您可能需要交换参数以进行消费

channel.basic_consume(queue='test', on_message_callback=handle_delivery) 

Basic setup is sth like基本设置就像

credentials = pika.PlainCredentials("some_user", "some_password")
parameters = pika.ConnectionParameters(
    "some_host.domain.tld", 5672, "some_vhost", credentials
)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

To start consuming:开始消费:

channel.start_consuming()

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

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