1. pika的安装
Pika是Python的RabbitMQ(AMQP 0-9-1)客户端库,相当于pymysql在python中的作用,主要负责连接RabbitMQ。
具体文档可见官网:https://pypi.org/project/pika/
安装pika:
pip install pika
2. RabbitMQ[简单模式]
简单模式的重要参数(其它模式也适用):
消费者
auto_ack=True # 消费者一旦接收到完整任务,队列里的任务就会删除
auto_ack=False # 消费者必须正确处理完任务后,发送下方回执信息,队列里的任务才会删除(造成拥塞)
ch.basic_ack(delivery_tag = method.delivery_tag) #当上面为false 时,告诉生产者任务执行完成
生产者
channel.queue_declare(queue='hello', durable=True) # 消息持久化
properties=pika.BasicProperties(delivery_mode=2,)
消费者
channel.basic_qos(prefetch_count=1) # 表示谁处理完了谁就来取,不再按照顺序依次来取
生产者发送消息到队列中:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello',durable=True)
channel.basic_publish(exchange='',
routing_key='hello',
body='认识你自己',
properties=pika.BasicProperties(delivery_mode=2,))
print(" [x] Sent '认识你自己'")
connection.close()
消费者从队列中取出消息:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello',durable=True)
def callback(ch, method, properties, body):
print(" [x] Received %r" % str(body,encoding ='utf8'))
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume('hello',
callback,
False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
3. RabbitMQ[exchange模式]
发布订阅 |
发布订阅和简单的消息队列区别在于,发布订阅会将消息发送给所有的订阅者(消费者),而消息队列中的数据被消费一次便消失。所以,RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者(生产者)发布消息时,会将消息放置在所有消费者订阅的相关队列中。
生产者发送消息到交换机中:
import pika
# 连接rabbitmq服务器
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs', # 声明一个名为logs的交换机
exchange_type='fanout')
message = "理解他人"
向交换机发送数据message
channel.basic_publish(exchange='logs',
routing_key='',
body=message)
print(" [x] Sent %r" % message)
connection.close()
消费者创建随机队列绑定交换机,并从队列中获取消息:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs',
exchange_type='fanout')
# 声明一个随机队列
result = channel.queue_declare('',exclusive=True)
# 获取队列名称
queue_name = result.method.queue
# 为创建好的随机队列绑定一个名为logs的交换机
channel.queue_bind(exchange='logs',
queue=queue_name)
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %s" % str(body,encoding='utf8'))
channel.basic_consume(queue=queue_name,
on_message_callback=callback,
auto_ack=True)
channel.start_consuming()
关键字 |
之前事例,发送消息时明确指定某个队列并向其中发送消息,RabbitMQ还支持根据关键字发送,即:队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据关键字判定应该将数据发送至指定队列。
生产者指定消息
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct')
message = "理解他人"
channel.basic_publish(exchange='direct_logs',
routing_key='info', # 看这里
body=message)
print(" [x] Sent %r" % message)
connection.close()
消费者监听不同的关键字:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',exchange_type='direct')
result = channel.queue_declare('',exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key='info') # 看这里
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r:%s" % (method.routing_key, str(body,encoding='utf8')))
channel.basic_consume(queue=queue_name,
on_message_callback=callback,
auto_ack=True)
channel.start_consuming()
模糊匹配 |
在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。
- #表示可以匹配 0个或多个单词
- *表示只能匹配 一个 单词
这种模式由于用得比较少,这里不再赘述