systemctl start
/
stop rabbitmq
pip install pika
or
easy_install pika
or
源码
https:
/
/
pypi.python.org
/
pypi
/
pika
先来看看使用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() |
import
pika
# ######################### 生产者 #########################
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
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,
[root@localhost ~]# netstat -ntp |grep beam
tcp6
0
0
192.168
.
136.8
:
5672
192.168
.
136.1
:
52587
ESTABLISHED
1146
/beam
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()
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() |
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
)
# ######################### 发布者 #########################
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
=
'
# ########################## 订阅者1 ##########################
import
pika
connection
=
pika.BlockingConnection(pika.ConnectionParameters(host
=
'192.168.136.8'
))
channel
=
connection.channel()
channel.exchange_declare(exchange
=
'
# ######################### 生产者 #########################
#关键字发送
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() |
exchange type = topic
在topic类型下,可以让队列绑定几个模糊的关键字,之后发送者将数据发送到exchange,exchange将传入”路由值“和 ”关键字“进行匹配,匹配成功,则将数据发送到指定队列。
# 表示可以匹配
发送者路由值 队列中
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.*' ) 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/