Second, Routing (using the Java client)
in the previous study, built a simple logging system, able to broadcast all the logs to multiple receivers, in this part of the learning, will add a new feature is that you can only subscribe to a specific message source, that is able to directly send critical error log messages to the log file to save, Unimportant log information files are not guaranteed to exist on the disk, but can still be output at the console, then this is the route distribution mechanism for the messages that we want to learn. third, Bindings (binding)The binding (bindings) has been created in the previous study and the code is as follows:
Channel. Queuebind(queuenameexchange_name"");
A binding is a relationship between Exchange and queue, which can simply be understood as: The queue is getting messages from this exchange.
Bindings can take an additional routingkey parameter, in order to avoid collisions with the Basicpublish parameter, called a binding key, which is how to create a binding with Routingkey.
Channel. Queuebind(queuenameexchange_name"Black");
a binding key relies on the type of exchange, which, like previous exchange with the fanout type, completely ignores the value of the binding key. Iv. Direct Exchange (directly switch)
The previously implemented logging system broadcasts all the messages to all consumers and now expands them to allow filtering of messages based on the severity of the information, such as a log message that expects a program to write to disk to receive only the wrong message, rather than wasting the disk saving all the log messages.
In order to achieve this goal, using an fanout type of exchange is obviously not able to meet this requirement because it can only broadcast all messages.
For this purpose, you will use a direct exchange instead of Fanout Exchange,direct Exchange to use a simple routing algorithm that matches the message to the queue that will be reached through the bound key.
As you can see from the structure diagram above, direct Exchange X is bound to two queue (Q1,Q2), the first queue bound Routingkey is orange, the second has two routingkey bound, One routingkey is black and the other Routingkey green.
Note: Sending messages with Routingkey Orange to X (Exchange), x routing the message to Q1, sending messages with Routingkey black and green will be routed to Q2, all other messages will be discarded.
Five,multiple bindings (multi-binding) multiple queue bindings with the same routingkey are allowed, in the above example, X and Q1 can be bound together with Routingkey:black, in which case the direct Exchange will broadcast the message to all matching queues, like fanout type Exchange, where a message Routingkey black will be sent to Q1 and Q2. Vi. emitting logs (sent logs)using direct instead of the fanout type of exchange, sending a message to a direct exchange will be based on the importance of the message as Routingkey, so that the receiver can select the log information it wants to receive. You must first create an exchange.
Channel. Exchangedeclare(exchange_name"direct");
Next, send a message:
Channel. Basicpublish(exchange_nameseveritynullmessage. GetBytes());
to simplify the program, severity is set to one of the three types of info, warning, and error. Vi. Subscribing (Subscribe to messages)The recipient creates a new binding based on the severity of interest.
StringQueueName=Channel.Queuedeclare().Getqueue();for (string severity : argvchannel. queuebind (queuenameexchange_name< Span class= "o" style= "font-weight:bold;" >, severity "}
Vii.Putting it all together(code implementation) The Emitlogdirect.java code listing is as follows:
Public classEmitlogdirect {Private StaticFinal String Exchange_name ="Direct_logs"; Public Static voidMain (string[] argv) throws Java.io.IOException {ConnectionFactory Factory=NewConnectionFactory (); Factory.sethost ("localhost"); Connection Connection=factory.newconnection (); Channel Channel=Connection.createchannel (); Channel.exchangedeclare (Exchange_name,"Direct"); String severity=getseverity (argv); String message=getMessage (argv); Channel.basicpublish (exchange_name, severity,NULL, Message.getbytes ()); System. out. println ("[x] Sent '"+ severity +"': '"+ Message +"'"); Channel.close (); Connection.close (); } //..}
The Receivelogsdirect code listing is as follows:
Public classReceivelogsdirect {Private StaticFinal String Exchange_name ="Direct_logs"; Public Static voidMain (string[] argv) throws Java.io.IOException, Java.lang.InterruptedException { ConnectionFactory Factory=NewConnectionFactory (); Factory.sethost ("localhost"); Connection Connection=factory.newconnection (); Channel Channel=Connection.createchannel (); Channel.exchangedeclare (Exchange_name,"Direct"); String QueueName=Channel.queuedeclare (). Getqueue (); if(Argv.length <1) {System.err.println ("Usage:receivelogsdirect [INFO] [WARNING] [ERROR]"); System.exit (1); } for(String severity:argv) {channel.queuebind (queuename, exchange_name, severity); } System. out. println ("[*] waiting for messages. To exit Press CTRL + C"); Queueingconsumer Consumer=NewQueueingconsumer (channel); Channel.basicconsume (QueueName,true, consumer); while(true) {queueingconsumer.delivery Delivery=Consumer.nextdelivery (); String message=NewString (Delivery.getbody ()); String Routingkey=Delivery.getenvelope (). Getroutingkey (); System. out. println ("[x] Received '"+ Routingkey +"': '"+ Message +"'"); } }}
Compile as usual (see previous tutorials for compiling and classpath recommendations). Now, for the sake of convenience, we will use an example of the run-time classpath of an environment variable $CP (%cp% on Windows). If you only want to save "warning" and "error" (instead of "message") log messages to a file, open a console and type:
[*] waiting for logs. To exit Press CTRL + C
[x' error ':' Run. Run. Or it'll explode. '
$CP receivelogsdirect Warning Error > Logs_from_rabbit.log
$CP Receivelogsdirect Info Warning Error
"Run. Run. Or it'll explode. "
Janet: Looking at the IT architecture in the Big Data era (7) rabbitmq--case of Message Queuing (routing set sail)