zoukankan      html  css  js  c++  java
  • rabbitMQ fanout 实现广播


    exchange 类型:
    fanout:所有bind到此exchange 的queue 都可以接收到消息

    direct: 通过routingkey和exchange 决定的那个唯一的queue 可以接收到消息

    topic: 所有符合routingkey的routingkey所bind的queue 可以接收消息

    表达式符合说明:
    #代表一个或多个字符
    *代表任何字符


    header:通过headers来决定把消息发给哪些queue

    工作原理:

    消息发布者:

    '''
    
    广播
    '''
    import pika
    import  sys
    
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    
    #声明一个exchange
    channel.exchange_declare(exchange='logs',exchange_type='fanout')
    
    message = 'hello my using fanout'
    
    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message
                          )
    
    print("message:",message)
    
    connection.close()

    消息接收者:

    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',exchange_type='fanout')
    
    #不指定queue 的名称,rabbitMQ 会随机生成一个 queue name , exclusive=True会使queue 的消费者断开后, 自动删除queue
    result = channel.queue_declare(exclusive=True)
    
    
    #获取随机生成的queue name
    queue_name = result.method.queue
    
    channel.queue_bind(exchange='logs',queue=queue_name)
    
    print("接收数据~~")
    
    def callback(ch,method,properties,body):
        print("接收到的数据:",body)
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    channel.start_consuming()
  • 相关阅读:
    Java数组的内存结构
    2014.11.9--最近的小感悟
    十一两天感悟
    Are you happiness
    Are you busy?
    Lesson 81-82 Are they true
    Lesson 79-80 People are getting sick
    Lesson 77-78 Socially Discriminated Against
    Lesson 75-76 Shopping on the web
    更新单点,求区间—— luogu 3374
  • 原文地址:https://www.cnblogs.com/gaizhongfeng/p/8086859.html
Copyright © 2011-2022 走看看