基于python實現監聽Rabbitmq系統日志代碼示例
介紹
rabbitmq默認有7個交換機,其中amq.rabbitmq.log為系統日志的交換機,這個日志為topic類型,會有三個等級的(routing_key)的日志發送到這個交換機上。
代碼如下
#!/usr/bin/env python# -*- coding: utf-8 -*-import pika# ########################### 訂閱者 ###########################credentials = pika.PlainCredentials('用戶名','密碼')connection = pika.BlockingConnection(pika.ConnectionParameters( ’ip’, 5672, ’/’, credentials=credentials))channel = connection.channel()# 聲明隊列channel.queue_declare(queue=’info_queue’,durable=True)channel.queue_declare(queue=’error_queue’,durable=True)channel.queue_declare(queue=’warning_queue’,durable=True)# 綁定channel.queue_bind(exchange=’amq.rabbitmq.log’,queue='info_queue',routing_key='info')channel.queue_bind(exchange=’amq.rabbitmq.log’,queue='error_queue',routing_key='error')channel.queue_bind(exchange=’amq.rabbitmq.log’,queue='warning_queue',routing_key='warning')print(’ [*] Waiting for logs. To exit press CTRL+C’)def callback(ch, method, properties, body): print(' [x] %r' % body) print(' [x] Done') ch.basic_ack(delivery_tag=method.delivery_tag)channel.basic_consume('info_queue',callback,auto_ack=False)channel.basic_consume('error_queue',callback,auto_ack=False)channel.basic_consume('warning_queue',callback,auto_ack=False)channel.start_consuming()’’’然后發布者只需要給exchange發送消息,然后exchange綁定的多個隊列都有這個消息了。訂閱者就收到這個消息了。’’’
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. React+umi+typeScript創建項目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析3. SharePoint Server 2019新特性介紹4. ASP中常用的22個FSO文件操作函數整理5. 三個不常見的 HTML5 實用新特性簡介6. ASP調用WebService轉化成JSON數據,附json.min.asp7. .Net core 的熱插拔機制的深入探索及卸載問題求救指南8. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁9. 讀大數據量的XML文件的讀取問題10. 解決ASP中http狀態跳轉返回錯誤頁的問題
