python使用rabbitMQ介紹四(路由模式)
阿新 • • 發佈:2019-01-13
一、模式介紹
路由模式,與釋出-訂閱模式一樣,訊息傳送到exchange中,消費者把佇列繫結到exchange上。
這種模式在exchange上新增添加了一個路由鍵(routing-key),生產者釋出訊息的時候新增路由鍵(routing-key),消費者繫結佇列到交換機時新增鍵值(routing-key),這樣就可以接收到對應的訊息。
路由模式的direct exchange。
佇列模型:
與釋出-訂閱模式不同的是,每個消費者佇列接收的訊息不同,根據訊息的routing-key把訊息傳送到不同的佇列中。
當所有的消費佇列繫結的routing-key一樣時,路由模式行為與釋出-訂閱模式一樣。
二、程式碼示意
釋出者:不再建立佇列,傳送訊息到exchange(交換機)中。exchange_type為direct。
1 import pika 2 import sys 3 4 connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) 5 channel = connection.channel() 6 7 channel.exchange_declare(exchange='direct_logs', 8 exchange_type='direct') 9 10 severity = ['info', 'warning', 'error'] 11 for i in range(20): 12 message = '{} Hello World! {}'.format(i, severity[i % 3]) 13 channel.basic_publish(exchange='direct_logs', 14 routing_key=severity[i % 3], 15 body=message) 16 print(" [x] Sent: {}".format(message)) 17 connection.close()
每個消費者繫結的佇列定義不同的routing-key,接收到不同的訊息。
以info為示例:
1 import pika 2 import sys 3 4 connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) 5 channel = connection.channel() 6 7 channel.exchange_declare(exchange='direct_logs', 8 exchange_type='direct') 9 10 result = channel.queue_declare(exclusive=True) 11 queue_name = result.method.queue 12 13 channel.queue_bind(exchange='direct_logs', 14 queue=queue_name, 15 routing_key='info') 16 17 print(' [*] Waiting for logs. To exit press CTRL+C') 18 19 def callback(ch, method, properties, body): 20 print(" [x] %r:%r" % (method.routing_key, body)) 21 22 channel.basic_consume(callback, 23 queue=queue_name, 24 no_ack=True) 25 26 channel.start_consuming()
執行結果輸出:
釋出者:
[x] Sent: 0 Hello World! info [x] Sent: 1 Hello World! warning [x] Sent: 2 Hello World! error [x] Sent: 3 Hello World! info [x] Sent: 4 Hello World! warning [x] Sent: 5 Hello World! error [x] Sent: 6 Hello World! info [x] Sent: 7 Hello World! warning [x] Sent: 8 Hello World! error [x] Sent: 9 Hello World! info [x] Sent: 10 Hello World! warning [x] Sent: 11 Hello World! error [x] Sent: 12 Hello World! info [x] Sent: 13 Hello World! warning [x] Sent: 14 Hello World! error [x] Sent: 15 Hello World! info [x] Sent: 16 Hello World! warning [x] Sent: 17 Hello World! error [x] Sent: 18 Hello World! info [x] Sent: 19 Hello World! warning
Info輸出:
[*] Waiting for logs. To exit press CTRL+C [x] 'info':b'0 Hello World! info' [x] 'info':b'3 Hello World! info' [x] 'info':b'6 Hello World! info' [x] 'info':b'9 Hello World! info' [x] 'info':b'12 Hello World! info' [x] 'info':b'15 Hello World! info' [x] 'info':b'18 Hello World! info'
Warning輸出:
[*] Waiting for logs. To exit press CTRL+C [x] 'warning':b'1 Hello World! warning' [x] 'warning':b'4 Hello World! warning' [x] 'warning':b'7 Hello World! warning' [x] 'warning':b'10 Hello World! warning' [x] 'warning':b'13 Hello World! warning' [x] 'warning':b'16 Hello World! warning' [x] 'warning':b'19 Hello World! warning'
Error輸出:
[*] Waiting for logs. To exit press CTRL+C [x] 'error':b'2 Hello World! error' [x] 'error':b'5 Hello World! error' [x] 'error':b'8 Hello World! error' [x] 'error':b'11 Hello World! error' [x] 'error':b'14 Hello World! error' [x] 'error':b'17 Hello World! error'
可以看到,不同的消費者收到不同級別的日誌資訊。
三、佇列資訊
管理頁面,exchange頁面,點選“direct_logs”上檢視佇列情況,可以看到三個不同routing_key的佇列
routing key列展示了對應的key。