標籤:
上篇文章中,我們構建了一個簡單的日誌系統。接下來,我們將豐富它:能夠使用不同的severity來監聽不同等級的log。比如我們希望只有error的log才儲存到磁碟上。
1. Bindings綁定
上篇文章中我們是這麼做的綁定:
[python] view plaincopy
- 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 :
[python] view plaincopy
- channel.queue_bind(exchange=exchange_name,
- queue=queue_name,
- routing_key=‘black‘)
對於fanout的exchange來說,這個參數是被忽略的。
2. Direct exchange
Direct exchange的路由演算法非常簡單:通過binding key的完全符合,可以通過來說明。
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都會被丟棄。
4. Emitting logs
首先是我們要建立一個direct的exchange:
[python] view plaincopy
- channel.exchange_declare(exchange=‘direct_logs‘,
- type=‘direct‘)
我們將使用log的severity作為routing key,這樣Consumer可以針對不同severity的log進行不同的處理。
publish:
[python] view plaincopy
- channel.basic_publish(exchange=‘direct_logs‘,
- routing_key=severity,
- body=message)
我們使用三種severity:‘info‘, ‘warning‘, ‘error‘.
5. Subscribing
對於queue,我們需要綁定severity:
[python] view plaincopy
- 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. 最終版本
The code for emit_log_direct.py:
[python] view plaincopy
- #!/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:
[python] view plaincopy
- #!/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] view plaincopy
- $ python receive_logs_direct.py warning error > logs_from_rabbit.log
列印所有log到螢幕:
[python] view plaincopy
- $ python receive_logs_direct.py info warning error
- [*] Waiting for logs. To exit press CTRL+C
(轉)RabbitMQ訊息佇列(五):Routing 訊息路由