RabbitMQ訊息佇列(五):Routing 訊息路由

AskHarries發表於2018-04-02

上篇文章中,我們構建了一個簡單的日誌系統。接下來,我們將豐富它:能夠使用不同的severity來監聽不同等級的log。比如我們希望只有error的log才儲存到磁碟上。

1. Bindings繫結

上篇文章中我們是這麼做的繫結:

channel.queue_bind(exchange=exchange_name,
                   queue=queue_name)複製程式碼

繫結其實就是關聯了exchange和queue。或者這麼說:queue對exchagne的內容感興趣,exchange要把它的Message deliver到queue中。

實際上,繫結可以帶routing_key 這個引數。其實這個引數的名稱和basic_publish 的引數名是相同了。為了避免混淆,我們把它成為binding key。
使用一個key來建立binding :

channel.queue_bind(exchange=exchange_name,
                   queue=queue_name,
                   routing_key='black')複製程式碼

對於fanout的exchange來說,這個引數是被忽略的。

2. Direct exchange

Direct exchange的路由演算法非常簡單:通過binding key的完全匹配,可以通過下圖來說明。

RabbitMQ訊息佇列(五):Routing 訊息路由
exchange X和兩個queue繫結在一起。Q1的binding key是orange。Q2的binding key是black和green。
當P publish key是orange時,exchange會把它放到Q1。如果是black或者green那麼就會到Q2。其餘的Message都會被丟棄。

3. Multiple bindings

多個queue繫結同一個key是可以的。對於下圖的例子,Q1和Q2都繫結了black。也就是說,對於routing key是black的Message,會被deliver到Q1和Q2。其餘的Message都會被丟棄。
RabbitMQ訊息佇列(五):Routing 訊息路由

4. Emitting logs

首先是我們要建立一個direct的exchange:

channel.exchange_declare(exchange='direct_logs',
                         type='direct')複製程式碼

我們將使用log的severity作為routing key,這樣Consumer可以針對不同severity的log進行不同的處理。
publish:

channel.basic_publish(exchange='direct_logs',
                      routing_key=severity,
                      body=message)複製程式碼

我們使用三種severity:’info’, ‘warning’, ‘error’.

5. Subscribing

對於queue,我們需要繫結severity:

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

for severity in severities:
    channel.queue_bind(exchange='direct_logs',
                       queue=queue_name,
                       routing_key=severity)複製程式碼

6. 最終版本

RabbitMQ訊息佇列(五):Routing 訊息路由

The code for emit_log_direct.py:

#!/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()複製程式碼

The code for receive_logs_direct.py:

#!/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:
    print >> sys.stderr, "Usage: %s [info] [warning] [error]" % /
                         (sys.argv[0],)
    sys.exit(1)

for severity in severities:
    channel.queue_bind(exchange='direct_logs',
                       queue=queue_name,
                       routing_key=severity)

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()複製程式碼

我們想把warning和error的log記錄到一個檔案中:

$ python receive_logs_direct.py warning error > logs_from_rabbit.log複製程式碼

列印所有log到螢幕:

$ python receive_logs_direct.py info warning error
 [*] Waiting for logs. To exit press CTRL+C複製程式碼

參考資料:

1. http://www.rabbitmq.com/tutorials/tutorial-four-python.html

2. http://blog.csdn.net/anzhsoft/article/details/19630147

RabbitMQ訊息佇列(五):Routing 訊息路由


相關文章