RabbitMQ
基础
关于MQ:
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。
RabbitMQ安装
1
2
3
4
5
6
7
8
|
安装配置epel源
$ rpm
-
ivh http:
/
/
dl.fedoraproject.org
/
pub
/
epel
/
6
/
i386
/
epel
-
release
-
6
-
8.noarch
.rpm
安装erlang
$ yum
-
y install erlang
安装RabbitMQ
$ yum
-
y install rabbitmq
-
server
|
启动/停止:
1
|
systemctl start
/
stop rabbitmq
|
安装python-API:
1
2
3
4
5
6
7
|
pip install pika
or
easy_install pika
or
源码
https:
/
/
pypi.python.org
/
pypi
/
pika
|
API基础操作
先来看看使用RabbitMQ之前,怎么实现消息队列:利用Queue和Thread,每线程往内存里的队列里put一个数,另一个程序再去内存队列里取数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
Queue
import
threading
message
=
Queue.Queue(
10
)
def
producer(i):
while
True
:
message.put(i)
def
consumer(i):
while
True
:
msg
=
message.get()
for
i
in
range
(
12
):
t
=
threading.Thread(target
=
producer, args
=
(i,))
t.start()
for
i
in
range
(
10
):
t
=
threading.Thread(target
=
consumer, args
=
(i,))
t.start()
|
对于RabbitMQ来说,生产和消费不再针对内存里的一个Queue对象,而是某台服务器上的RabbitMQ Server实现的消息队列。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
pika
# ######################### 生产者 #########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel() #创建通道
channel.queue_declare(queue
=
'hello'
)
#队列名称
channel.basic_publish(exchange
=
'',
routing_key
=
'hello'
, #路由名称
body
=
'Hello World!'
)
#发送内容
print
(
" [x] Sent 'Hello World!'"
)
connection.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import
pika
# ########################## 消费者 ##########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.queue_declare(queue
=
'hello'
)
#声明,队列名称,和producer创建的重复没有关系
def
callback(ch, method, properties, body):
print
(
" [x] Received %r"
%
body)
channel.basic_consume(callback, #获取body后执行回调函数
queue
=
'hello'
,
no_ack
=
True
) #自动应答开启,会给MQ服务器发送一个ack:‘已经收到了’。
print
(
' [*] Waiting for messages. To exit press CTRL+C'
)
channel.start_consuming()
|
消费者运行起来后会和RabbitMQ建立长连接,一旦生产者放数据到队列里,消费者就能获取到该值,并进行处理。
1
2
|
[root@localhost ~]# netstat -ntp |grep beam
tcp6
0
0
192.168
.
136.8
:
5672
192.168
.
136.1
:
52587
ESTABLISHED
1146
/beam
|
消息安全
1、no-ack = False(自动应答关闭)
如果生产者遇到情况(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那么,RabbitMQ会重新将该任务添加到队列中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import
pika
#no-ack
########################### 消费者 ##########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.queue_declare(queue
=
'hello'
)
def
callback(ch, method, properties, body):
print
(
" [x] Received %r"
%
body)
import
time
time.sleep(
10
)
print
'ok'
ch.basic_ack(delivery_tag
=
method.delivery_tag)
#主动发送ack
#打印‘ok’后才告诉MQ,这个消息已经处理完了。
channel.basic_consume(callback,
queue
=
'hello'
,
no_ack
=
False
)
#自动应答关闭,与channel.basic_ack共同使用
print
(
' [*] Waiting for messages. To exit press CTRL+C'
)
channel.start_consuming()
|
2、durable
make message persistent 使消息持久化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import
pika
#durable
########################## 生产者 #########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.queue_declare(queue
=
'hello'
, durable
=
True
)
#开启持久化
channel.basic_publish(exchange
=
'',
routing_key
=
'hello'
,
body
=
'Hello World!'
,
properties
=
pika.BasicProperties(
delivery_mode
=
2
,
# make message persistent
))
print
(
" [x] Sent 'Hello World!'"
)
connection.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
pika
#durable
########################## 消费者 #########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
# make message persistent
channel.queue_declare(queue
=
'hello'
, durable
=
True
)
def
callback(ch, method, properties, body):
print
(
" [x] Received %r"
%
body)
import
time
time.sleep(
10
)
print
'ok'
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_consume(callback,
queue
=
'hello'
,
no_ack
=
False
)
print
(
' [*] Waiting for messages. To exit press CTRL+C'
)
channel.start_consuming()
|
消息获取顺序
默认消息队列里的数据是按照奇偶顺序被消费者拿走,例如:消费者1 去队列中获取 奇数 序列的任务,消费者2去队列中获取 偶数 序列的任务。
channel.basic_qos(prefetch_count=1) 表示谁来谁取,不再按照奇偶数排列.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
pika
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'10.211.55.4'
))
channel
=
connection.channel()
# make message persistent
channel.queue_declare(queue
=
'hello'
)
def
callback(ch, method, properties, body):
print
(
" [x] Received %r"
%
body)
import
time
time.sleep(
10
)
print
'ok'
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_qos(prefetch_count
=
1
) #增加这行
channel.basic_consume(callback,
queue
=
'hello'
,
no_ack
=
False
)
print
(
' [*] Waiting for messages. To exit press CTRL+C'
)
channel.start_consuming()
|
发布&订阅
与消息队列区别:
消息队列中的数据只要被消费一次便消失。
创建队列的数量:
同一份消息,有多少订阅者,就要创建多少个队列。(RabbitMQ实现发布和订阅时,会为每一个订阅者创建一个队列,而发布者发布消息时,会将消息放置在所有相关队列中。)
语法:
exchange type = fanout #fanout==>输出到很多
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# ######################### 发布者 #########################
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'fanout_name'
,
type
=
'fanout'
)
message
=
' '
.join(sys.argv[
1
:])
or
"info: Hello World!"
channel.basic_publish(exchange
=
'fanout_name'
,
#自命名exchange
routing_key
=
'',
body
=
message)
print
(
" [x] Sent %r"
%
message)
connection.close()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# ########################## 订阅者1 ##########################
import
pika
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'fanout_name'
,
type
=
'fanout'
) #创建exchange(if not exist)
result
=
channel.queue_declare(exclusive
=
True
)
queue_name
=
result.method.queue #获取队列名称
channel.queue_bind(exchange
=
'fanout_name'
,queue
=
queue_name) #通过上面两个值绑定队列
print
(
' [*] Waiting for fanout_name. To exit press CTRL+C'
)
def
callback(ch, method, properties, body):
print
(
" [x] %r"
%
body)
channel.basic_consume(callback,
queue
=
queue_name,
no_ack
=
True
)
channel.start_consuming()
|
创建多个订阅者,能更好的体现它的效果。
运行结果总结:
每个订阅者创建一个exchange队列,名称自定,发布者会把数据发送给所有叫这个名字的队列。因为数据只能被消费一次,所以有多少个订阅者,就有多少个队列。
发送到指定(not 固定)队列
之前事例,发送消息时明确指定某个队列并向其中发送消息,RabbitMQ还支持根据关键字发送
1、按关键字寻找队列发送
exchange type = direct
队列绑定关键字,发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# ######################### 生产者 #########################
#关键字发送
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'direct_logs'
,
type
=
'direct'
)
message
=
'Hello World!'
channel.basic_publish(exchange
=
'direct_logs'
,
routing_key
=
"yes"
,
#"yes","no","db"
body
=
message)
print
(
" [x] Sent %r"
%
(message))
connection.close()
|
模拟两个消费者,一个消费者的队列是("yes","db"),另一个消费者队列("no","db")。如果生产者发送的队列关键字是"yes"or"no",其一匹配;如果生产者发送的队列关键字是"db",则两个消费者都能接收到。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
########################### 消费者1 ##########################
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'direct_logs'
,
type
=
'direct'
)
result
=
channel.queue_declare(exclusive
=
True
)
queue_name
=
result.method.queue
channel.queue_bind(exchange
=
'direct_logs'
,
queue
=
queue_name,
routing_key
=
'yes'
)
channel.queue_bind(exchange
=
'direct_logs'
,
queue
=
queue_name,
routing_key
=
'db'
)
print
(
' [*] Waiting for logs. To exit press CTRL+C'
)
def
callback(ch, method, properties, body):
print
(
" [x] %r:%r"
%
(method.routing_key, body))
channel.basic_consume(callback,
queue
=
queue_name,
no_ack
=
True
)
channel.start_consuming()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
########################### 消费者2 ##########################
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'direct_logs'
,
type
=
'direct'
)
result
=
channel.queue_declare(exclusive
=
True
)
queue_name
=
result.method.queue
channel.queue_bind(exchange
=
'direct_logs'
,
queue
=
queue_name,
routing_key
=
'no'
)
channel.queue_bind(exchange
=
'direct_logs'
,
queue
=
queue_name,
routing_key
=
'db'
)
print
(
' [*] Waiting for logs. To exit press CTRL+C'
)
def
callback(ch, method, properties, body):
print
(
" [x] %r:%r"
%
(method.routing_key, body))
channel.basic_consume(callback,
queue
=
queue_name,
no_ack
=
True
)
channel.start_consuming()
|
2、模糊匹配
exchange type = topic
在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。
# 表示可以匹配 0 个 或 多个 单词
* 表示只能匹配 一个 单词
1
2
3
|
发送者路由值 队列中
python.topic.cn python.
*
-
-
不匹配
python.topic.cn python.
# -- 匹配
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# ######################### 生产者 #########################
#模糊匹配
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'topic_logs'
,
type
=
'topic'
)
message
=
'Hello World!'
channel.basic_publish(exchange
=
'topic_logs'
,
routing_key
=
"python.topic"
,
body
=
message)
print
(
" [x] Sent %r"
%
(message))
connection.close()
|
消费者1是‘*’匹配,消费者2是‘#’匹配:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
########################### 消费者1 ##########################
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'topic_logs'
,
type
=
'topic'
)
result
=
channel.queue_declare(exclusive
=
True
)
queue_name
=
result.method.queue
channel.queue_bind(exchange
=
'topic_logs'
,
queue
=
queue_name,
routing_key
=
'python.*'
) #只匹配python.后有一个单词的
print
(
' [*] Waiting for topic_logs. To exit press CTRL+C'
)
def
callback(ch, method, properties, body):
print
(
" [x] %r:%r"
%
(method.routing_key, body))
channel.basic_consume(callback,
queue
=
queue_name,
no_ack
=
True
)
channel.start_consuming()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
########################### 消费者2 ##########################
import
pika
import
sys
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'topic_logs'
,
type
=
'topic'
)
result
=
channel.queue_declare(exclusive
=
True
)
queue_name
=
result.method.queue
channel.queue_bind(exchange
=
'topic_logs'
,
queue
=
queue_name,
routing_key
=
'python.#'
) #匹配python.后所有单词
print
(
' [*] Waiting for topic_logs. To exit press CTRL+C'
)
def
callback(ch, method, properties, body):
print
(
" [x] %r:%r"
%
(method.routing_key, body))
channel.basic_consume(callback,
queue
=
queue_name,
no_ack
=
True
)
channel.start_consuming()
|
从结果得出结论,如果生产者发送的routing_key是:
python.topic.cn --> 只有消费者2能接收到
python.cn --> 消费者1和消费者2都能接收到
python. --> 消费者1和消费者2都能接收到
python --> 只有消费者2能接收到
网络搜索的概念:
Topic Exchange – 主题式交换器,通过消息的路由关键字和绑定关键字的模式匹配,将消息路由到被绑定的队列中。
这种路由器类型可以被用来支持经典的发布/订阅消息传输模型——使用主题名字空间作为消息寻址模式,将消息传递给那些部分或者全部匹配主题模式的多个消费者。
主题交换器类型的工作方式如下: 绑定关键字用零个或多个标记构成,每一个标记之间用“.”字符分隔。
绑定关键字必须用这种形式明确说明,并支持通配符:“*”匹配一个词组,“#”零个或多个词组。
因此绑定关键字“*.stock.#”匹配路由关键字“usd.stock”和“eur.stock.db”,但是不匹配“stock.nasdaq”
参考来源:http://www.cnblogs.com/wupeiqi/