-
RabbitMQ 라우팅Cloud 2015. 3. 16. 09:00원하는 메세지만 선택해서 받기 위한 기능.exchange와 큐를 바인딩하기위해 routing_key 매개변수를 이용함.
예제소스emit_log_direct.py#!/usr/bin/env pythonimport pikaimport sysconnection = 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) > 1 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()receive_logs_direct.py#!/usr/bin/env pythonimport pikaimport sysconnection = 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.queueseverities = sys.argv[1:]if not severities:print >> sys.stderr, "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()테스트 방법먼저 recevier 2개 실행python receive_logs_direct.py warning error > logs_from_rabbit.logpython receive_logs_direct.py info warning error로그 발행python emit_log_direct.py error "Run. Run. Or it will explode."참고'Cloud' 카테고리의 다른 글
RabbitMQ RPC(Remote procedure call) (0) 2015.03.18 RabbitMQ 토픽 (0) 2015.03.17 RabbitMQ를 이용한 메세지 발행/구독 모델 (0) 2015.03.13 RabbitMQ 작업 큐로 이용하기 (0) 2015.03.12 RabbitMQ 파악하기 (0) 2015.03.11 댓글