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

來源:互聯網
上載者:User

標籤:

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

1. Bindings綁定

    上篇文章中我們是這麼做的綁定:

 

[python] view plaincopy 
  1. channel.queue_bind(exchange=exchange_name,  
  2.                    queue=queue_name)  

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

 

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

[python] view plaincopy 
  1. channel.queue_bind(exchange=exchange_name,  
  2.                    queue=queue_name,  
  3.                    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 
  1. channel.exchange_declare(exchange=‘direct_logs‘,  
  2.                          type=‘direct‘)  

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

 

 

[python] view plaincopy 
  1. channel.basic_publish(exchange=‘direct_logs‘,  
  2.                       routing_key=severity,  
  3.                       body=message)  

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

 

 

5. Subscribing

對於queue,我們需要綁定severity:

 

[python] view plaincopy 
  1. result = channel.queue_declare(exclusive=True)  
  2. queue_name = result.method.queue  
  3.   
  4. for severity in severities:  
  5.     channel.queue_bind(exchange=‘direct_logs‘,  
  6.                        queue=queue_name,  
  7.                        routing_key=severity)  


 

6. 最終版本

The code for emit_log_direct.py:

 

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


The code for receive_logs_direct.py:

 

 

[python] view plaincopy 
  1. #!/usr/bin/env python  
  2. import pika  
  3. import sys  
  4.   
  5. connection = pika.BlockingConnection(pika.ConnectionParameters(  
  6.         host=‘localhost‘))  
  7. channel = connection.channel()  
  8.   
  9. channel.exchange_declare(exchange=‘direct_logs‘,  
  10.                          type=‘direct‘)  
  11.   
  12. result = channel.queue_declare(exclusive=True)  
  13. queue_name = result.method.queue  
  14.   
  15. severities = sys.argv[1:]  
  16. if not severities:  
  17.     print >> sys.stderr, "Usage: %s [info] [warning] [error]" % \  
  18.                          (sys.argv[0],)  
  19.     sys.exit(1)  
  20.   
  21. for severity in severities:  
  22.     channel.queue_bind(exchange=‘direct_logs‘,  
  23.                        queue=queue_name,  
  24.                        routing_key=severity)  
  25.   
  26. print ‘ [*] Waiting for logs. To exit press CTRL+C‘  
  27.   
  28. def callback(ch, method, properties, body):  
  29.     print " [x] %r:%r" % (method.routing_key, body,)  
  30.   
  31. channel.basic_consume(callback,  
  32.                       queue=queue_name,  
  33.                       no_ack=True)  
  34.   
  35. channel.start_consuming()  

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

 

 

[python] view plaincopy 
  1. $ python receive_logs_direct.py warning error > logs_from_rabbit.log  

列印所有log到螢幕:

 

 

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.