Python操作RabbitMQ之Pika

来源:互联网 发布:mac双系统macos没了 编辑:程序博客网 时间:2024/06/05 20:40

安装RabbitMQ(MAC平台)

  • 安装 brew install rabbitmq
  • 启动 /usr/local/sbin rabbitmq-server
  • 访问: http://localhost:15672 使用guest/guest 登陆

基本概念

RabbitMQ发送消息时,都是先把消息发送给ExChange(交换机),然后再分发给有相应RoutingKey关系的Queue。ExChange和Queue是多对多的关系。RabbitMQ 3.0之后创建ExChange时,有四种类型可选”fanout、direct、topic、headers”。

参考链接

  • fanout(route key不生效)
    这里写图片描述
    所有发送到Fanout Exchange的消息都会被转发到与该Exchange 绑定(Binding)的所有Queue上。Fanout Exchange 不需要处理RouteKey 。只需要简单的将队列绑定exchange 上。这样发送到exchange的消息都会被转发到与该交换机绑定的所有队列上。类似子网广播,每台子网内的主机都获得了一份复制的消息。所以,Fanout Exchange 转发消息是最快的。
  • direcrt
    这里写图片描述
    所有发送到Direct Exchange的消息被转发到RouteKey中指定的Queue。
  • topic
    这里写图片描述
    所有发送到Topic Exchange的消息被转发到所有关心RouteKey中指定Topic的Queue上,Exchange 将RouteKey 和某Topic 进行模糊匹配。此时队列需要绑定一个Topic。可以使用通配符进行模糊匹配,符号“#”匹配一个或多个词,符号“”匹配不多不少一个词。因此“log.#”能够匹配到“log.info.oa”,但是“log.” 只会匹配到“log.error”。
  • headers

python操作

python可选择pika或者kombu

基本使用

此模式下,发送队列的一方把消息存入mq的指定队列后,若有消费者端联入相应队列,即会获取到消息,并且队列中的消息会被消费掉。

  • publish.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()# declare queuechannel.queue_declare(queue='hello')channel.basic_publish(exchange='',                      routing_key='hello',                      body='Hello World!')print(" [x] Sent 'Hello World!'")connection.close()
  • consumer.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()# if the queue doesn't exist, declare in consumer# channel.queue_declare(queue='hello')def callback(ch, method, properties, body):    print(" [x] Received %r" % body)channel.basic_consume(callback,                      queue='hello',                      no_ack=True)print(' [*] waiting. To exit press CTRL+C')# start consuming (block)channel.start_consuming()connection.close()

Exchange

参考链接
fanout

  • publish.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()channel.exchange_declare('test_exchange', exchange_type='fanout')channel.basic_publish(exchange='test_exchange',                      routing_key='',  # fanout doesn't need route key                      body='Hello World!')print(" [x] Sent 'Hello World!'")connection.close()
  • consumer.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()channel.exchange_declare('test_exchange', exchange_type='fanout')result = channel.queue_declare(exclusive=True)queue_name = result.method.queuechannel.queue_bind(exchange='test_exchange', queue=queue_name)def callback(ch, method, properties, body):    print(" [x] Received %r" % body)channel.basic_consume(callback,                      queue=queue_name,                      no_ack=True)print(' [*] waiting. To exit press CTRL+C')# start consuming (block)channel.start_consuming()connection.close()

上述代码是在消费者中绑定的exchange,所以在消费者未启动的情况下,直接执行生产者,产生的消息会被丢弃,当然也可以在生产者进程中绑定,如下面的代码

  • publish.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()# declare exchangechannel.exchange_declare('test_exchange2', exchange_type='fanout')channel.queue_declare("test_queue2")channel.queue_bind(exchange='test_exchange2', queue='test_queue2')channel.basic_publish(exchange='test_exchange2',                      routing_key='',  # fanout doesn't need route key                      body='Hello World!')print(" [x] Sent 'Hello World!'")connection.close()
  • consumer.py
from __future__ import absolute_importimport pikacredentials = pika.PlainCredentials('guest','guest')connection = pika.BlockingConnection(pika.ConnectionParameters(    '127.0.0.1', 5672, '/', credentials))channel = connection.channel()channel.queue_declare("test_queue2")def callback(ch, method, properties, body):    print(" [x] Received %r" % body)channel.basic_consume(callback,                      queue="test_queue2",                      no_ack=True)print(' [*] waiting. To exit press CTRL+C')# start consuming (block)channel.start_consuming()connection.close()

direct
topic
rpc
- consumer.py

#!/usr/bin/env python# coding=utf8import pikaclass Center(object):    def __init__(self):        self.connection = pika.BlockingConnection(pika.ConnectionParameters(            host='localhost'))        self.channel = self.connection.channel()        # 定义接收返回消息的队列        result = self.channel.queue_declare(exclusive=True)        self.callback_queue = result.method.queue        self.channel.basic_consume(self.on_response,                                   no_ack=True,                                   queue=self.callback_queue)    # 定义接收到返回消息的处理方法    def on_response(self, ch, method, props, body):        self.response = body    def request(self, n):        self.response = None        # 发送计算请求,并声明返回队列        self.channel.basic_publish(exchange='',                                   routing_key='compute_queue',                                   properties=pika.BasicProperties(                                       reply_to=self.callback_queue,                                   ),                                   body=str(n))        # 接收返回的数据        while self.response is None:            self.connection.process_data_events()        return int(self.response)center = Center()print " [x] Requesting increase(30)"response = center.request(30)print " [.] Got %r" % (response,)
  • publish.py
# coding=utf8import pika# 连接rabbitmq服务器connection = pika.BlockingConnection(pika.ConnectionParameters(    host='localhost'))channel = connection.channel()# 定义队列channel.queue_declare(queue='compute_queue')print ' [*] Waiting for n'# 将n值加1def increase(n):    return n + 1# 定义接收到消息的处理方法def request(ch, method, properties, body):    print " [.] increase(%s)" % (body,)    response = increase(int(body))    # 将计算结果发送回控制中心    ch.basic_publish(exchange='',                     routing_key=properties.reply_to,                     body=str(response))    ch.basic_ack(delivery_tag=method.delivery_tag)channel.basic_qos(prefetch_count=1)channel.basic_consume(request, queue='compute_queue')channel.start_consuming()