In the previous article, we built a simple log system. Next, we'll enrich it: the ability to use different severity to listen to different levels of log. For example, we hope that only the error log will be saved to disk.
1. Bindings Bindings
In the previous article we did this by binding:
[Python]View Plaincopy
- Channel.queue_bind (Exchange=exchange_name,
- Queue=queue_name)
The binding is actually associated with Exchange and queue. Or so: The queue is interested in the content of Exchagne, and exchange has to deliver its message into the queue.
In fact, bindings can take routing_key this parameter. In fact, the name of this parameter and the basic_publish parameter name are the same. To avoid confusion, we make it a binding key.
Use a key to create the binding:
[Python]View Plaincopy
- Channel.queue_bind (Exchange=exchange_name,
- Queue=queue_name,
- routing_key=' black ')
For fanout exchange, this parameter is ignored.
2. Direct Exchange
The routing algorithm for Direct exchange is very simple: it can be explained by the exact match of the binding key.
Exchange X and the two queue are bound together. Q1 's binding key is orange. Q2 's binding key is black and green.
When P publish key is orange, Exchange puts it in Q1. If it's black or green, then it's going to Q2. The rest of the message will be discarded.
3. Multiple bindings multiple queue bindings the same key is possible. For example, Q1 and Q2 are bound to black. In other words, for routing key is Black's message, it will be deliver to Q1 and Q2. The rest of the message will be discarded.
4. Emitting logs
First, we're going to create a direct exchange:
[Python]View Plaincopy
- Channel.exchange_declare (exchange=' direct_logs ',
- type=' direct ')
We will use log severity as the routing key, so that consumer can be processed differently for different severity logs.
Publish
[Python]View Plaincopy
- Channel.basic_publish (exchange=' direct_logs ',
- Routing_key=severity,
- Body=message)
We use three kinds of severity: ' Info ', ' warning ', ' error '.
5. Subscribing
For queue, we need to bind severity:
[Python]View Plaincopy
- result = Channel.queue_declare (exclusive=True)
- Queue_name = Result.method.queue
- For severity in severities:
- Channel.queue_bind (exchange=' direct_logs ',
- Queue=queue_name,
- routing_key=severity)
6. Final version
The code for emit_log_direct.py:
[Python]View Plaincopy
- #!/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) > 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 ()
The code for receive_logs_direct.py:
[Python]View Plaincopy
- #!/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:
- 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 ()
We want to record the log of warning and error in a file:
[Python]View Plaincopy
- $ python receive_logs_direct.py warning error > Logs_from_rabbit.log
Print all log to screen:
[Python]View Plaincopy
- $ python receive_logs_direct.py Info warning error
- [*] Waiting for logs. To exit Press CTRL + C
Go RABBITMQ Message Queuing (v): Routing message routing