基于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. vue實現web在線聊天功能2. JavaScript實現頁面動態驗證碼的實現示例3. JavaEE SpringMyBatis是什么? 它和Hibernate的區別及如何配置MyBatis4. Springboot 全局日期格式化處理的實現5. SpringBoot+TestNG單元測試的實現6. 完美解決vue 中多個echarts圖表自適應的問題7. 解決Android Studio 格式化 Format代碼快捷鍵問題8. 在Chrome DevTools中調試JavaScript的實現9. Python使用urlretrieve實現直接遠程下載圖片的示例代碼10. Java使用Tesseract-Ocr識別數字
