時間:2020-11-29來源:www.outletmksalestore.com作者:電腦系統城
介紹
rabbitmq默認有7個交換機,其中amq.rabbitmq.log為系統日志的交換機,這個日志為topic類型,會有三個等級的(routing_key)的日志發送到這個交換機上。
代碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#!/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綁定的多個隊列都有這個消息了。訂閱者就收到這個消息了。 ''' |
以上就是本文的全部內容,希望對大家的學習有所幫助
2022-03-01
PHP如何從txt文件中讀取數據詳解2022-03-01
分享5個方便好用的Python自動化腳本2021-03-29
Python中pycharm編輯器界面風格修改方法