一、前言
之前我们介绍了消息对了,以及简单的介绍了RabbitMQ的作用,现在我们就来简单的看看RabbitMQ基本示例,进一步剖析RabbitMQ的强大功能。
二、RabbitMQ的安装要求
RabbitMQ依赖的语言 erlang 下载地址:
RabbitMQ软件下载:
RabbitMQ的安装步骤:
安装python rabbitMQ modul:pip install pika 或者 easy_install pika ,源码:
RabbitMQ的使用文档:
三、启动RabbitMQ
已完成安装RabbitMQ,到cmd的中输入:services.msc,如图:
然后找到RabbitMQ,右击鼠标->启动:
三、实现最简单的队列通信图
四、基本示例
4.1、send端(producer)
说明:建立socket->声明管道->声明queue->通过一个exchange发送内容至queue->关闭连接
import pika#通过这个实例先建立一个socketconnection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))#声明一个管道channel = connection.channel()#声明queuechannel.queue_declare(queue="shuaigaogao") #这边给queue起名字叫"shuaigaogao"#n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.channel.basic_publish(exchange="", routing_key="shuaigaogao", #queue的名字 body="hello world") #body是你发送的内容print("[x] Sent 'hello world'")#直接关闭连接connection.close()
4.2、receive端(consumers)
说明:创建socket连接->创建管道->声明queue->创建回调函数callback->消费的消息->开启消费
## 消费者有可能在其他的机器上import pika#建立一个socket连接connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))#创建一个管道channel = connection.channel()#You may ask why we declare the queue again ‒ we have already declared it in our previous code.# We could avoid that if we were sure that the queue already exists. For example if send.py program#was run before. But we're not yet sure which program to run first. In such cases it's a good# practice to repeat declaring the queue in both programs.channel.queue_declare(queue="shuaigaogao")def callback(ch,method,properites,body): print("--->",ch,method,properites) print(" [x] Received %r" % body)channel.basic_consume(#消费的消息 callback, #如果收到消息,就调用callback函数来处理消息 queue="shuaigaogao",#queue的名字 no_ack=True)print(' [*] Waiting for messages. To exit press CTRL+C')#这个start只要一启动,就一直运行,它不止收一条,而是永远收下去,没有消息就在这边卡住channel.start_consuming()
输出:
[*] Waiting for messages. To exit press CTRL+C--->('::1', 5672, 0, 0) params= >>> [x] Received b'hello world'
从上面的输出可以看的出callback中的ch,method,properites分别是:
- ch:是send端管道的内存对象的地址
- method:指的send端的是发给谁,发给哪个Q的一些信息,一般不怎么用
- properites:send端的属性,这边至的send端发过来给recive端的属性
- body:是send端发过来的消息
五、远程配置RabbitMQ
刚刚我们配置的RabbitMQ是放在Windows的,而且是在本地的。那如果远程连接RabbitMQ Server的话,需要配置权限
5.1、创建自己的账号
首先在RabbitMQ server上创建一个账号 sudo rabbitmqctl add_user 用户名 密码
sudo rabbitmqctl add_user zhangqigao gaogao0808
5.2、配置权限
需要配置权限,允许从外面访问
sudo rabbitmqctl set_permissions -p / zhangqigao ".*" ".*" ".*"
参数说明:
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.
5.3、客户端连接的时候需要配置认证参数
#认证信息credentials = pika.PlainCredentials('alex', 'alex3714')#连接信息 connection = pika.BlockingConnection(pika.ConnectionParameters( '10.211.55.5',5672,'/',credentials))channel = connection.channel()
六、总结
- RabbitMQ的默认端口是15672,在python中使用的模块是pika。
- consumers中如果不声明queue的话,则如果consumers先启动,则会报错。如果是producer先启动,consumers后启动则不报错。但是如果说consumer声明了,consumer先启动就不会报错。如果是producers先启动的话,则忽略。
- 所有的socket传输都是bytes类型。
- 消费者和生产者不一定在同一台机器上,在其他机器上运行也是可以的。
- consumers启动以后会一直运行下去,它会永远的收下去。producers可以运行多次,只要运行一次,consumers就会接收一次。