RabbitMQ
一,RabbitMQ簡單介紹:
RabbitMQ是一個在AMQP基礎上完整的,可複用的企業消息系統。他遵循Mozilla Public License開源協定。
MQ全稱為Message Queue,
消息隊列(MQ)是一種應用程式對應用程式的通信方法。應用程式通過讀寫出入隊列的消息(針對應用程式的資料)來通信,而無需專用連接配接來連結它們。消 息傳遞指的是程式之間通過在消息中發送資料進行通信,而不是通過直接調用彼此來通信,直接調用通常是用于諸如
遠端過程調用的技術。排隊指的是應用程式通過 隊列來通信。隊列的使用除去了接收和發送應用程式同時執行的要求。
二,安裝
pip install pika
三,簡單隊列
1丶使用API操作RabbitMQ
基于Queue實作生産者消費者模型
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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()
View Code
對于RabbitMQ來說,生産和消費不再針對記憶體裡的一個Queue對象,而是某台伺服器上的RabbitMQ Server實作的消息隊列。
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
connection = pika.BaseConnection(pika.ConnectionParameters(host='10.211.55.4')) # 封裝socket邏輯部分
channel = connection.channel() # 拿到操作句柄
channel.queue_declare(queue='hello') # 通過channel建立一個隊列,再給給隊列取名字
channel.basic_publish(exchange='', # 通過句柄給
routing_key='hello', # 把body的資料放到名為hello的隊列裡去
body='Hello World!',
))
print("[x] Sent 'Hello World!")
connection.close()
生産者
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
connection = pika.BaseConnection(pika.ConnectionParameters(host='10.211.55.4'))
channel = connection.channel()
channel.queue_declare(queue='hello') # 建立隊列
def callback(ch, method, properties, body): # 就是個回調函數
print(" [x] Received %r" % body)
channel.basic_consume(callback, # 函數名;取出資料就執行這個函數
queue='hello', # 隊列名
no_ack=Ture) # 無應答是(Ture);有應答(False)
print(' [*] Waiting for messages.To exit press CTRL+C')
channel.start_consuming()
消費者
2丶acknowledgment消息不丢失
no-ack = False,如果消費者遇到情況(its channel is closed, connection is closed, or TCP connection is lost)挂掉了,那麼,RabbitMQ會重新将該任務添加到隊列中。
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
connection = pika.BaseConnection(pika.ConnectionParameters(host='10.211.55.4'))
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) # 調為有應答要加上的(下面的要改no_ack=False)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, # 函數名;取出資料就執行這個函數
queue='hello', # 隊列名
no_ack=False) # 無應答是(Ture);有應答(False)
print(' [*] Waiting for messages.To exit press CTRL+C')
channel.start_consuming()
3丶durable消息不丢失
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
channel = connection.channel()
# make message persistent
channel.queue_declare(queue='hello', durable=True) # durable=True這個參數是把資料儲存到硬碟
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!',
properties=pika.BasicProperties(
delivery_mode=2, # 傳遞模式從預設的1,改為2
))
print(" [x] Sent 'Hello World!'")
connection.close()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.211.55.4'))
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) # 改為False,表示有應答
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
4丶消息擷取順序
預設消息隊列裡的資料是按照順序被消費者拿走,例如:消費者1 去隊列中擷取 奇數 序列的任務,消費者1去隊列中擷取 偶數 序列的任務。
channel.basic_qos(prefetch_count=1) 表示誰來誰取,不再按照奇偶數排列
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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) # prefetch_count=1這個參數就讓取的方式改變,不在順序取資料
channel.basic_consume(callback,
queue='hello',
no_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
四,exchange
1、fanout模式
釋出訂閱
釋出訂閱和簡單的消息隊列差別在于,釋出訂閱會将消息發送給所有的訂閱者,而消息隊列中的資料被消費一次便消失。
是以,RabbitMQ實作釋出和訂閱時,會為每一個訂閱者建立一個隊列,而釋出者釋出消息時,會将消息放置在所有相關隊列中。
exchange type = fanout
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
# 建立交換機
channel.exchange_declare(exchange='logs', # 名字
type='fanout') # 類型
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
channel.basic_publish(exchange='logs', # 往名字為logs的交換機裡
routing_key='', # 把資料直接放到交換機裡,不用放到隊列中,是以預設為空
body=message)
print(" [x] Sent %r" % message)
connection.close() # 關閉
釋出者
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
#建立交換機
channel.exchange_declare(exchange='logs',
type='fanout')
# 随機建立一個隊列
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs', # 随機生成的隊列綁定交換機
queue=queue_name)
print(' [*] Waiting for logs. 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() # 阻塞住,等待生産者把資料放到消費者,并監聽
訂閱者
圖形解釋:
2、dirct模式
關鍵字發送
RabbitMQ 還支援根據關鍵字發送,即:隊列綁定關鍵字,發送者将資料根據關鍵字發送到消息exchange,
exchange 根據 關鍵字 判定應該将資料發送至指定隊列。
exchange type = direct
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)
print(" [x] Sent %r:%r" % (severity, message))
connection.close()
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
type='direct')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
severities = sys.argv[1:]
if not severities:
sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
sys.exit(1)
for severity in severities: # 用循環是可以綁定多個隊列
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key='severity') # 定義的參數(關鍵字)
channel.queue_bind(exchange='direct_logs',
queue=queue_name,
routing_key='alex')
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()
圖形解釋:
在交換機中用一關鍵字,隻有隊列裡有關鍵字交換機才會發送資料給綁定的隊列。
3、topic
模糊比對
在topic類型下,可以讓隊列綁定幾個模糊的關鍵字,之後發送者将資料發送到exchange,exchange将傳入”路由值“和 ”關鍵字“進行比對,
比對成功,則将資料發送到指定隊列。exchange type = topic
# 表示可以比對 0 個 或 多個 單詞
* 表示隻能比對 一個 單詞
發送者路由值 隊列中
old.boy.python old.* -- 不比對
old.boy.python old.# -- 比對
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
type='topic')
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs',
routing_key=routing_key,
body=message)
print(" [x] Sent %r:%r" % (routing_key, message))
connection.close()
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='topic_logs',
type='topic')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
binding_keys = sys.argv[1:]
if not binding_keys:
sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
sys.exit(1)
for binding_key in binding_keys:
channel.queue_bind(exchange='topic_logs',
queue=queue_name,
routing_key=binding_key)
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()