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","")
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='')
connection.close()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
b.ack
import pika
credentials = pika.PlainCredentials("root","")
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='')
connection.close()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
c.服务端持久化
import pika
credentials = pika.PlainCredentials("root","")
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()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
d.取数据顺序
import pika
credentials = pika.PlainCredentials("root","")
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()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
e.fanout
import pika
credentials = pika.PlainCredentials("root","")
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()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
import pika
credentials = pika.PlainCredentials("root","")
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()
s3
import pika
credentials = pika.PlainCredentials("root","")
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()
s4
f.direct
import pika
credentials = pika.PlainCredentials("root","")
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()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
import pika
credentials = pika.PlainCredentials("root","")
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()
s3
g.topic
import pika
credentials = pika.PlainCredentials("root","")
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()
s1
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
import pika
credentials = pika.PlainCredentials("root","")
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()
s3
h.超时时间
import pika
credentials = pika.PlainCredentials("root","")
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()
s2
使用:
a. 普通消息队列
b. 批量向多个队列中发送
c. 根据关键字匹配向队列中发送
d. 模糊匹配向队列中发送
问题:
1. exchange的作用?
- exchange和队列进行绑定
- 用户向队列发送数据时,无序再找队列,直接向exchange中发送即可。
2. rabbitmq中有几种exchange?
- fanout,只要绑定就发
- dirct,确定关键字
- topic,模糊匹配
3. 消息持久化和ack
- 服务端(durable)
- 客户端(ack)
看官方文档 -----------------------------------------------------
http://www.rabbitmq.com/getstarted.html
Python RabbitMQ 消息队列的更多相关文章
- Python RabbitMQ消息队列
python内的队列queue 线程 queue:不同线程交互,不能夸进程 进程 queue:只能用于父进程与子进程,或者同一父进程下的多个子进程,进行交互 注:不同的两个独立进程是不能交互的. ...
- openresty 学习笔记番外篇:python访问RabbitMQ消息队列
openresty 学习笔记番外篇:python访问RabbitMQ消息队列 python使用pika扩展库操作RabbitMQ的流程梳理. 客户端连接到消息队列服务器,打开一个channel. 客户 ...
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...
- RabbitMQ消息队列1: Detailed Introduction 详细介绍
1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有 ...
- (转)RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)
在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇文章中,我们将会 ...
- (转)RabbitMQ消息队列(六):使用主题进行消息分发
在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity的log.但是,这也是它之所以叫做简单日志系统的原因, ...
- (转)RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)
上篇文章中,我们把每个Message都是deliver到某个Consumer.在这篇文章中,我们将会将同一个Message deliver到多个Consumer中.这个模式也被成为 "pub ...
- RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)
上篇文章中,我们把每个Message都是deliver到某个Consumer.在这篇文章中,我们将会将同一个Message deliver到多个Consumer中.这个模式也被成为 "pub ...
- 使用EasyNetQ组件操作RabbitMQ消息队列服务
RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现,是实现消息队列应用的一个中间件,消息队列中间件是分布式系统中重要的组件,主要解决应用耦合, ...
随机推荐
- (三)maven出错记录
此处因为是自己做的所以文笔带过,简单描述下 1\首先是jsp页面报错,缺少必要的servletx依赖jar包,需要在pom中配置 <dependency><groupId>ja ...
- 在Build Path中包含其他工程
------------siwuxie095 在 TestBuildPath 的 Build Path 中包含 SupportProje ...
- c++ deque 容器
deque (全名 double ended queue)是一种具有队列和栈一样的数据结构. 在c++标准库中几乎和vector容器的接口完全相同,但它和vector 还是有一些细微的差别. 1. d ...
- 【转】Provisional headers are shown
在chrome开发者工具的 Network 面板中,某些请求头后面会跟着下面这行文字: Provisional headers are shown 这种请求实际上根本没有产生,对应的请求头当然也不应该 ...
- Linux内核的特征
Linux内核的特征 Linux是个人计算机和工作站上的Unix类操作系统.但是,它绝不是简化的Unix.相反,Linux是强有力和具有创新意义的Unix类操作系统.它不仅继承了Unix的特征,而且在 ...
- SP1557 GSS2 - Can you answer these queries II
一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...
- redirect_uri域名与后台配置不一致,错误码:10003
登录公众平台,重新配置下网页授权域名就可以了 参考https://blog.csdn.net/haoxuexiaolang/article/details/79432073
- Monkey稳定性测试环境搭建说明
一.安装Java环境 安装Java环境-JDK:下载地址:http://pan.baidu.com/s/1pJ6Yqs7,jdk安装解压即可. 二.设置环境变量 双击下载的JDK ,设置安装路径.这里 ...
- oracle数据库登录
在做以下操作时,要确保你的数据库环境已经正确安装完成.数据库在实际应用中是比较多的,我们测试人员经常会在前台造一些测试数据,在后台数据库进行验证,当然,不局限于此,数据库也可以作为一个专项测试来谈.反 ...
- POJ 1160 Post Office (四边形不等式优化DP)
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...