Python RabbitMQ 消息队列
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消 息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
RabbitMQ 是什么?: 消息队列 .
其他队列 :- queue - redis列表 - rabbitmq - zeromq
为什么要有消息队列?:
- 生产者消费者
- 数据通信
- rest api,http协议发送的json格式数据
- webservice,http协议发送的xml格式数据
- rpc,基于socket并使用自己封装的协议进行数据传输
RabbitMQ安装
服务端 LInux
yum install rabbitmq-server
客户端
pip3 install pika
运行
rabbitmq-server systemctl start rabbitmq-server sudo rabbitmqctl add_user wupeiqi 123 # 设置用户为administrator角色 sudo rabbitmqctl set_user_tags wupeiqi administrator # 设置权限 sudo rabbitmqctl set_permissions -p "/" root ".*" ".*" ".*" systemctl restart rabbitmq-server
a. 普通消息队列
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # 创建一个队列:s91 channel.queue_declare(queue='s91') # 向队列s91中发送一个 Hello World! channel.basic_publish(exchange='',routing_key='s91',body='66') connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.queue_declare(queue='s91') def callback(ch, method, properties, body): print(body) channel.basic_consume(callback,queue='s91',no_ack=True) channel.start_consuming()
b.ack
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # 创建一个队列:s91 channel.queue_declare(queue='s91') # 向队列s91中发送一个 Hello World! channel.basic_publish(exchange='',routing_key='s91',body='66') connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # channel.queue_declare(queue='s91') def callback(ch, method, properties, body): print(body) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_consume(callback,queue='s91',no_ack=False) channel.start_consuming()
c.服务端持久化
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # make message persistent channel.queue_declare(queue='s92', durable=True) channel.basic_publish(exchange='', routing_key='s92', body='Hello World!', properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # make message persistent channel.queue_declare(queue='s92', durable=True) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_consume(callback,queue='s92',no_ack=False) channel.start_consuming()
d.取数据顺序
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # make message persistent channel.queue_declare(queue='s92', durable=True) channel.basic_publish(exchange='', routing_key='s92', body='Hello World!', properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() # make message persistent channel.queue_declare(queue='s92', durable=True) def callback(ch, method, properties, body): print(" [x] Received %r" % body) ch.basic_ack(delivery_tag = method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(callback,queue='s92',no_ack=False) channel.start_consuming()
e.fanout
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e1',exchange_type='fanout') message = "Hello World!" channel.basic_publish(exchange='e1',routing_key='',body=message) connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e1',exchange_type='fanout') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e1',queue=queue_name) def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e1',exchange_type='fanout') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e1',queue=queue_name) def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e1',exchange_type='fanout') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e1',queue=queue_name) def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
f.direct
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e2',exchange_type='direct') message = "Hello World!" channel.basic_publish(exchange='e2',routing_key='error',body=message) connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e2',exchange_type='direct') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e2',queue=queue_name,routing_key='info') channel.queue_bind(exchange='e2',queue=queue_name,routing_key='error') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e2',exchange_type='direct') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e2',queue=queue_name,routing_key='error') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
g.topic
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e3',exchange_type='topic') message = "Hello World!" channel.basic_publish(exchange='e3',routing_key='info.xx.uu',body=message) connection.close()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e3',exchange_type='topic') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e3',queue=queue_name,routing_key='info.*') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) channel = connection.channel() channel.exchange_declare(exchange='e3',exchange_type='topic') # 随机生成对列名 result = channel.queue_declare(exclusive=True) queue_name = result.method.queue # 让队列和e1绑定 channel.queue_bind(exchange='e3',queue=queue_name,routing_key='info.#') def callback(ch, method, properties, body): print(" [x] %r" % body) channel.basic_consume(callback,queue=queue_name,no_ack=True) channel.start_consuming()
h.超时时间
import pika credentials = pika.PlainCredentials("root","123") connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.13.92',credentials=credentials)) connection.add_timeout(5, lambda: channel.stop_consuming()) channel = connection.channel() channel.queue_declare(queue='s91') def callback(ch, method, properties, body): print(body) channel.basic_consume(callback,queue='s91',no_ack=True) channel.start_consuming()
使用:
a. 普通消息队列
b. 批量向多个队列中发送
c. 根据关键字匹配向队列中发送
d. 模糊匹配向队列中发送
问题:
1. exchange的作用?
- exchange和队列进行绑定
- 用户向队列发送数据时,无序再找队列,直接向exchange中发送即可。
2. rabbitmq中有几种exchange?
- fanout,只要绑定就发
- dirct,确定关键字
- topic,模糊匹配
3. 消息持久化和ack
- 服务端(durable)
- 客户端(ack)
看官方文档 -----------------------------------------------------
http://www.rabbitmq.com/getstarted.html