RBMQ案例四:路由模式

 

 

使僅訂閱消息的子集成爲可能。例如,我們將能夠僅將關鍵錯誤消息定向到日誌文件(以節省磁盤空間),同時仍然能夠在控制檯上打印所有日誌消息。
 
通過路由來匹配對應的消息

一、消息發佈端

#!/usr/bin/env python
import pika
import sys
import json
import datetime


def get_message():
    # 產生消息入口處
    for i in range(100):  # 生成100條消息
        for str_t in ['info', 'warning', 'error']: # 生成三種類型的消息
            message = json.dumps({'id': "%s-90000%s" % (str_t, i), "amount": 100 * i, "name": "%s" % str_t,
                                  "createtime": str(datetime.datetime.now())})
            producer(message, str_t)


def producer(message, severity):
    # 登陸並創建信道
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672,
                                  credentials=pika.PlainCredentials('guest', 'guest')))
    channel = connection.channel()

    channel.exchange_declare(exchange='direct_logs', exchange_type='direct', durable=True)
    channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message)
    print(" [x] Sent %r:%r" % (severity, message))
    connection.close()


if __name__ == "__main__":
    get_message()  # 程序執行入口

二、接收所有的消息all

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672,
                              credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct',durable=True)

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


for severity in ['info','warning','error']:
    channel.queue_bind( exchange='direct_logs', queue=queue_name, routing_key=severity)

print(' [*] Waiting for all logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

三、接收所有的消息info

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672,
                              credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct',durable=True)

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


# for severity in ['info','warning','error']:
#     channel.queue_bind( exchange='direct_logs', queue=queue_name, routing_key=severity)
channel.queue_bind( exchange='direct_logs', queue=queue_name, routing_key='info')

print(' [*] Waiting for info. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

四、接收所有的消息error

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672,
                              credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct',durable=True)

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


channel.queue_bind( exchange='direct_logs', queue=queue_name, routing_key='error')

print(' [*] Waiting for error. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

五、接收消息warning

#!/usr/bin/env python
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672,
                              credentials=pika.PlainCredentials('guest', 'guest')))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct',durable=True)

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


# for severity in ['info','warning','error']:
channel.queue_bind( exchange='direct_logs', queue=queue_name, routing_key='warning')

print(' [*] Waiting for warning. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r:%r" % (method.routing_key, body))


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章