基于python實(shí)現(xiàn)監(jiān)聽Rabbitmq系統(tǒng)日志代碼示例
介紹
rabbitmq默認(rèn)有7個交換機(jī),其中amq.rabbitmq.log為系統(tǒng)日志的交換機(jī),這個日志為topic類型,會有三個等級的(routing_key)的日志發(fā)送到這個交換機(jī)上。
代碼如下
#!/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()’’’然后發(fā)布者只需要給exchange發(fā)送消息,然后exchange綁定的多個隊列都有這個消息了。訂閱者就收到這個消息了。’’’
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫方法2. 解決ajax的delete、put方法接收不到參數(shù)的問題方法3. Jsp中request的3個基礎(chǔ)實(shí)踐4. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子5. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)6. idea修改背景顏色樣式的方法7. IntelliJ IDEA設(shè)置自動提示功能快捷鍵的方法8. XML入門的常見問題(一)9. jsp EL表達(dá)式詳解10. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
