• RabbitMQ--Publish/Subscribe(四)


    先前例子中,我们创建了一个简单的日志系统,广播messages到consumer接收方。

    但如果有日志错误级别的,不同的consumer接收不同错误级别的信息。比如consumer1接收info和warnging信息,consumer2接收error日志信息。这样的话,我们需要使用类型为direct的exchange type。

    sender_direct.py

    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',
                             type='direct')
    
    severity = sys.argv[1] if len(sys.argv) > 2 else 'info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='direct_logs',
                          routing_key=severity,
                          body=message)
    print(" [x] Sent %r:%r" % (severity, message))
    connection.close()

    recv_direct.py

    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',
                             type='direct')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    
    severities = sys.argv[1:]
    if not severities:
        sys.stderr.write("Usage: %s [info] [warning] [error]
    " % sys.argv[0])
        sys.exit(1)
    
    for severity in severities:
        channel.queue_bind(exchange='direct_logs',
                           queue=queue_name,
                           routing_key=severity)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()
  • 相关阅读:
    pom
    Java API操作Hadoop可能会遇到的问题以及解决办法
    hadoop在windows上的配置文件
    UNC路径
    spark在windows的配置
    Oracle系统表整理+常用SQL语句收集(转载)
    sbt配置文件
    (转)Flink简介
    spark osx:WARN NativeCodeLoader:62
    试图加载格式不正确的程序。 (异常来自HRESULT:0x8007000B)
  • 原文地址:https://www.cnblogs.com/xiaoming279/p/6281791.html
Copyright © 2020-2023  润新知