事实证明,我对AMQP的理解是不完整的.
这个想法如下:
客户:
获得连接后的客户端不应该关心除交换机名称和路由密钥之外的任何其他内容.那就是我们不知道最终会进入哪个队列.
channel.basic_publish(exchange='order',
routing_key="order.test.customer",
body=pickle.dumps(data),
properties=pika.BasicProperties(
content_type="text/plain",
delivery_mode=2))
消费者
当通道打开时,我们声明交换和队列
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)
当队列准备就绪时,在“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')
使用路由密钥“order.test.customer”发送到“订单”交换的消息现在将被路由到“测试”队列,消费者可以在其中获取它.