参见:http://www.voidcn.com/article/p-ekljdajr-bqs.html
https://www.jianshu.com/p/79ca08116d57
https://www.cnblogs.com/alex3714/articles/5248247.html
一.安装
1.安装(在Python中):
pip install pika
or
easy_install pika
2.源码:
https://pypi.python.org/pypi/pika
二.介绍
1.介绍:
RabbitMQ是1个由Erlang编写的消息中间件,即实现了高级消息队列协议(AMQP)的开源消息代理软件
2.消息队列(Message Queue;MQ):
消息是指在应用间传送的数据,可以很简单,如只包含文本字符串,也可以更复杂,如包含嵌入对象
队列(Queue)是1种先进先出的数据结构,消息队列也是1种队列
消息队列是1种应用间的通信方式:发送消息后可以立即返回,由消息系统来确保消息的可靠传递
消息发布者只管把消息发布到MQ中而不管使用者;消息使用者只管从MQ中取消息而不管发布者
这样发布者和使用者都不用知道对方的存在
3.为何使用MQ:
参见:https://blog.csdn.net/tanqian351/article/details/98203482
不仅需要实现不同线程/进程间的通信,还需要实现不同应用/语言间的通信,而后者是Python完成不了的
主要应用场景包括:异步处理/应用解耦/流量消峰/最终一致性
4.实现最简单的队列通信:
RabbitMQ中可以同时存在多个队列,各个队列彼此独立
三.配置权限(以Linux为例)
- 仅在进行远程连接时需要,进行本地连接时不需要
#在rabbitmq server上创建1个用户:
sudo rabbitmqctl add_user [user] [pwd]
#实例:
sudo rabbitmqctl add_user john 123
#配置权限:
sudo rabbitmq set_permissions [-p vhost] {user} {conf} {write} {read}
#参数说明:
vhost:The name of the virtual host to which to grant the user access, defaulting to /.
user:The name of the user to grant access to the specified virtual host.
conf:A regular expression matching resource names for which the user is granted configure permissions.
write:A regular expression matching resource names for which the user is granted write permissions.
read:A regular expression matching resource names for which the user is granted read permissions.
#通常为:
sudo rabbitmqctl set_permissions -p / [user] ".*" ".*" ".*"
#实例:
sudo rabbitmqctl set_permissions -p / john ".*" ".*" ".*"
#客户端连接时需配置认证参数:
credentials = pika.PlainCredentials('john','123')#user,pwd
connection=pika.BlockingConnection(
pika.ConnectionParameters(
'10.211.55.5',#IP
5672,#端口
'/',#虚拟主机
credentials
)
)
四.语法