上篇文章中,我们构建了一个简单的日志系统。接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log。比如我们希望只有error的log才保存到磁盘上。
1. Bindings绑定
上篇文章中我们是这么做的绑定:
1
2
3 1channel.queue_bind(exchange=exchange_name,
2 queue=queue_name)
3
绑定其实就是关联了exchange和queue。或者这么说:queue对exchagne的内容感兴趣,exchange要把它的Message deliver到queue中。
实际上,绑定可以带routing_key 这个参数。其实这个参数的名称和basic_publish 的参数名是相同了。为了避免混淆,我们把它成为binding key。
使用一个key来创建binding :
1
2
3
4 1channel.queue_bind(exchange=exchange_name,
2 queue=queue_name,
3 routing_key='black')
4
对于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:
1
2
3 1channel.exchange_declare(exchange='direct_logs',
2 type='direct')
3
我们将使用log的severity作为routing key,这样Consumer可以针对不同severity的log进行不同的处理。
publish:
1
2
3
4 1channel.basic_publish(exchange='direct_logs',
2 routing_key=severity,
3 body=message)
4
我们使用三种severity:’info’, ‘warning’, ‘error’.
5. Subscribing
对于queue,我们需要绑定severity:
1
2
3
4
5
6
7
8 1result = channel.queue_declare(exclusive=True)
2queue_name = result.method.queue
3
4for severity in severities:
5 channel.queue_bind(exchange='direct_logs',
6 queue=queue_name,
7 routing_key=severity)
8
6. 最终版本
The code for emit_log_direct.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1#!/usr/bin/env python
2import pika
3import sys
4
5connection = pika.BlockingConnection(pika.ConnectionParameters(
6 host='localhost'))
7channel = connection.channel()
8
9channel.exchange_declare(exchange='direct_logs',
10 type='direct')
11
12severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
13message = ' '.join(sys.argv[2:]) or 'Hello World!'
14channel.basic_publish(exchange='direct_logs',
15 routing_key=severity,
16 body=message)
17print " [x] Sent %r:%r" % (severity, message)
18connection.close()
19
The code for receive_logs_direct.py:
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
32
33
34
35
36 1#!/usr/bin/env python
2import pika
3import sys
4
5connection = pika.BlockingConnection(pika.ConnectionParameters(
6 host='localhost'))
7channel = connection.channel()
8
9channel.exchange_declare(exchange='direct_logs',
10 type='direct')
11
12result = channel.queue_declare(exclusive=True)
13queue_name = result.method.queue
14
15severities = sys.argv[1:]
16if not severities:
17 print >> sys.stderr, "Usage: %s [info] [warning] [error]" % /
18 (sys.argv[0],)
19 sys.exit(1)
20
21for severity in severities:
22 channel.queue_bind(exchange='direct_logs',
23 queue=queue_name,
24 routing_key=severity)
25
26print ' [*] Waiting for logs. To exit press CTRL+C'
27
28def callback(ch, method, properties, body):
29 print " [x] %r:%r" % (method.routing_key, body,)
30
31channel.basic_consume(callback,
32 queue=queue_name,
33 no_ack=True)
34
35channel.start_consuming()
36
我们想把warning和error的log记录到一个文件中:
1
2 1$ python receive_logs_direct.py warning error > logs_from_rabbit.log
2
打印所有log到屏幕:
1
2
3 1$ python receive_logs_direct.py info warning error
2 [*] Waiting for logs. To exit press CTRL+C
3
参考资料: