Python操作RabbitMQ之Pika

本文介绍了如何在MAC平台上安装RabbitMQ,并详细阐述了RabbitMQ的基本概念,包括交换机(Exchange)的四种类型:fanout、direct、topic、headers。接着,文章重点讲解了使用Python的Pika库进行RabbitMQ操作,包括基本的发布和消费消息,以及Exchange的使用示例。文中提供了多个Python示例代码,帮助理解不同Exchange类型的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装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_import
import pika
credentials = pika.PlainCredentials('guest','guest')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '127.0.0.1', 5672, '/', credentials))
channel = connection.channel()
# declare queue
channel.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_import
import pika

credentials = 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_import
import pika
credentials = 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_import
import pika

credentials = 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.queue
channel.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_import
import pika
credentials = pika.PlainCredentials('guest','guest')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '127.0.0.1', 5672, '/', credentials))
channel = connection.channel()
# declare exchange
channel.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_import
import pika

credentials = 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=utf8
import pika


class 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=utf8
import pika

# 连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='localhost'))
channel = connection.channel()

# 定义队列
channel.queue_declare(queue='compute_queue')
print ' [*] Waiting for n'


# 将n值加1
def 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值