RabbitMQ Routing 消息路由

上篇文章中,我们构建了一个简单的日志系统。接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log。比如我们希望只有error的log才保存到磁盘上。

1. Bindings绑定

上篇文章中我们是这么做的绑定:

  1. channel.queue_bind(exchange=exchange_name,
  2. queue=queue_name)
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 :

  1. channel.queue_bind(exchange=exchange_name,
  2. queue=queue_name,
  3. routing_key='black')
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:

  1. channel.exchange_declare(exchange='direct_logs',
  2. type='direct')
channel.exchange_declare(exchange='direct_logs',
type='direct')

我们将使用log的severity作为routing key,这样Consumer可以针对不同severity的log进行不同的处理。
publish:

  1. channel.basic_publish(exchange='direct_logs',
  2. routing_key=severity,
  3. body=message)
channel.basic_publish(exchange='direct_logs',
routing_key=severity,
body=message)

我们使用三种severity:'info', 'warning', 'error'.

5. Subscribing

对于queue,我们需要绑定severity:

  1. result = channel.queue_declare(exclusive=True)
  2. queue_name = result.method.queue
  3. for severity in severities:
  4. channel.queue_bind(exchange='direct_logs',
  5. queue=queue_name,
  6. routing_key=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:

  1. #!/usr/bin/env python
  2. import pika
  3. import sys
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(
  5. host='localhost'))
  6. channel = connection.channel()
  7. channel.exchange_declare(exchange='direct_logs',
  8. type='direct')
  9. severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
  10. message = ' '.join(sys.argv[2:]) or 'Hello World!'
  11. channel.basic_publish(exchange='direct_logs',
  12. routing_key=severity,
  13. body=message)
  14. print " [x] Sent %r:%r" % (severity, message)
  15. 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') 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:

  1. #!/usr/bin/env python
  2. import pika
  3. import sys
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(
  5. host='localhost'))
  6. channel = connection.channel()
  7. channel.exchange_declare(exchange='direct_logs',
  8. type='direct')
  9. result = channel.queue_declare(exclusive=True)
  10. queue_name = result.method.queue
  11. severities = sys.argv[1:]
  12. if not severities:
  13. print >> sys.stderr, "Usage: %s [info] [warning] [error]" % \
  14. (sys.argv[0],)
  15. sys.exit(1)
  16. for severity in severities:
  17. channel.queue_bind(exchange='direct_logs',
  18. queue=queue_name,
  19. routing_key=severity)
  20. print ' [*] Waiting for logs. To exit press CTRL+C'
  21. def callback(ch, method, properties, body):
  22. print " [x] %r:%r" % (method.routing_key, body,)
  23. channel.basic_consume(callback,
  24. queue=queue_name,
  25. no_ack=True)
  26. channel.start_consuming()
#!/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记录到一个文件中:

  1. $ python receive_logs_direct.py warning error > logs_from_rabbit.log
$ python receive_logs_direct.py warning error > logs_from_rabbit.log

打印所有log到屏幕:

  1. $ python receive_logs_direct.py info warning error
  2. [*] Waiting for logs. To exit press CTRL+C
$ python receive_logs_direct.py info warning error
[*] Waiting for logs. To exit press CTRL+C

转载自: anzhsoft: http://blog.csdn.net/anzhsoft/article/details/19630147

参考资料:

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

上一篇:Windows下Redis3.2.10及图像化工具redis-desktop-manager安装教程


下一篇:MPC学习笔记1:基于状态空间模型的预测控制(2)