• rabbitMQ direct实现广播


    原理图:

    消息广播者:

    '''
    
    exchange 类型:
    fanout:所有bind到此exchange 的queue 都可以接收到消息
    
    direct: 通过routingkey和exchange 决定的那个唯一的queue 可以接收到消息
        接收端可以接收指定级别的消息:info , warning , error
    
    topic: 所有符合routingkey的routingkey所bind的queue 可以接收消息
    
    表达式符合说明:
        #代表一个或多个字符
        *代表任何字符
    
    
    header:通过headers来决定把消息发给哪些queue
    '''
    
    import  pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',exchange_type='direct')
    
    msg = "using direct .."
    #消息级别
    severity = 'info'
    channel.basic_publish(exchange='direct_logs',routing_key=severity,body=msg)
    
    print('send %s:%s'%(severity,msg))
    
    channel.close()

    消息接收者:

    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',exchange_type='direct')
    
    
    result = channel.queue_declare(exclusive=True)
    
    queueName = result.method.queue
    
    severities = 'info'
    
    channel.queue_bind(exchange='direct_logs',queue=queueName,routing_key=severities)
    
    def callback(ch , method ,properties, body):
        print("接收:",body)
        print("routingKey:", method.routing_key)
    channel.basic_consume(callback,queue=queueName,no_ack=True)
    
    channel.start_consuming()
    channel.close()
  • 相关阅读:
    【转】ThinkPHP 页面跳转
    thinkphp中select()和find()的区别
    (Python)异常处理try...except、raise
    python中try except处理程序异常的方法
    SNMP消息传输机制
    公钥私钥+数字证书原理
    转:使用python的Flask实现一个RESTful API服务器端
    转:xxe attack学习
    转:php防止sql注入的一点心得
    转:在 Ubuntu 上使用 Nginx 部署 Flask 应用
  • 原文地址:https://www.cnblogs.com/gaizhongfeng/p/8087027.html
Copyright © 2020-2023  润新知