(using Python client Pika 0.9.8)
In the previous tutorial, we built a simple log system. We can broadcast log messages to many recipients.
In this tutorial we will add a feature to it-we will subscribe to just a subset of messages to be possible. For example, we can command just the error message to the log file (save to disk space), and it can print all the log messages in the console.
binding
In the previous example we have created the bindings and you can recall the code like this:
Channel.queue_bind (Exchange=exchange_name, Queue=queue_name)
Bindings are a relationship between Exchange and queues. This can be simply read: The queue is interested in messages from this exchange.
The binding can use an additional Routing_key parameter. To avoid confusion with a basic_publish parameter we will call it a bound key. This is how we use a key to create a binding:
Channel.queue_bind (Exchange=exchange_name,queue=queue_name, routing_key= ' black ')
The binding key means that the exchange type has been. Fanout type of exchange, we used the previous, simple ignore its value.
Direct Exchange
The previous tutorial in our log system gives all consumers a broadcast of all the messages. We want to extend it to allow filtering of messages based on their services. For example, we might want to write a script that only receives critical error log messages and does not waste disk space on warnings and information logs.
We use an fanout type of exchange, which does not give us too much extensibility-it can only be broadcast unconsciously.
We will replace it with the direct type of exchange. The routing algorithm that follows the direct type of exchange is simple-a message that enters a routing_key queue with a true match binding key.
To illustrate this, consider the following settings:
In this setting, we see that the direct type of Exchange x has two queues bound to it. The first queue uses the binding key orange binds, the second has two bindings, one with the binding key black and the other with green.
Setting a message like this uses a routing key Orange is published to Exchange that will be routed to the queue Q1. Use the routing key black or green will enter the Q2. All messages will be discarded.
Multiple Bindings
It is perfectly legal to use the same binding key to bind multiple queues. In our example we can have a binding key black between X and Q1. In that example, the direct type of exchange will behave similarly to fanout and will broadcast messages to all matching queues. A black message that uses a routing key is passed to Q1 and Q2.
Production Log
We will use this mode for our log system. Monomer Fanout We will send a message to a direct type of exchange. We will provide log severity as a routing key. The receive script can choose the severity it wants to receive in this way. We focus first on production logs.
We always need to create exchange first:
Channel.exchange_declare (exchange= ' direct_logs ', type= ' direct ')
And we're going to send a message:
Channel.basic_publish (exchange= ' direct_logs ', routing_key=severity,body=message)
For simplifying things we suggest that ' severity ' may be one of ' info ', ' warning ', ' error '.
Subscribe
Receiving the message will only take effect as in the previous tutorial, with an exception-we will create a binding for each of the severity of our interest.
Result=channel.queue_declare (exclusive=true) queue_name = result.method.queuefor severity in severities: Channel.queue_bind (exchange= ' direct_logs ', Queue=queue_name, routing_key=severity)
put the code together .
Emit_log_direct.py's Code:
#!/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 W orld! ' Channel.basic_publish (exchange= ' direct_logs ', routing_key=severity, body=message) print "[x] Sent%r:%r"% (severity, Message) Connection.close ()
Receive_logs_direct.py's Code:
#!/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 ()
RABBITMQ Official website Tutorial---Routing