(This example is a client of net that is used, written in C #),description, Chinese square brackets "" denotes nouns.
In the previous tutorial, we improved our logging system. Instead of using a "fanout" switch that only enables virtual broadcasts, the "Direct" type of switch is used, which allows us to selectively receive logs.
Although the use of the "Direct" type of "message switch" improves our system, it still has limitations-it cannot route a selection based on multiple criteria.
In our logging system, we may subscribe not only to the log based on severity, but also to the source from which the logs are issued. You may be aware of this concept from the Syslog UNIX tool, which is based on the severity (Info/warn/crit ... ) and facilities (Auth/cron/kern ... ) routing logs.
This gives us a lot of flexibility-we might want to listen to important bugs from "cron" and listen to all the logs of "Kern".
To implement it in our logging system, we need to understand a more complex topic type of "message switch".
1, topic type of "message switch"
Messages sent to the "Topic" type "message switch" cannot have any routing_key-it must be a list of words separated by points. These words can be anything, but usually they specify some of the features associated with the message. Examples of several valid routing keywords: "Stock.usd.nyse", "NYSE.VMW", "Quick.orange.rabbit". There can be any number of words in the routing keyword, up to 255 bytes.
The binding key must also be in the same form. The logic behind the "Topic" type of "message switch" is similar to the "Direct" type of "message switch"-messages sent with a specific "routing key" are passed to all queues that are bound to the matching binding key. However, there are two important special cases for the binding key:
* (STAR) can replace one word.
# (pound sign) can replace 0 or more words.
In one example, the simplest explanation is:
In this example, we will send all messages describing the animals. The message will be sent using a "routing key" consisting of three words (two dots). The first word in the routing key describes the speed, the second color, and the third category: <speed>.<color>.<species>.
We have created three bindings: the Q1 Binding Key "*.orange.*" and the Q2 are "*.*.rabbit" and "lazy.#" bindings.
These "binding keys" to express the meaning can be summed up as:
Q1 is interested in all the orange animals.
Q2 wanted to hear everything about rabbits and everything about the lazy animals.
Messages that have the route key set to "Quick.orange.rabbit" are passed to two queues. The message "Lazy.orange.elephant" will also send their two queues. On the other hand, "Quick.orange.fox" is only sent to the first queue, while "Lazy.brown.fox" can only be sent to the second queue. "Lazy.pink.rabbit" will be passed to the second queue only once, even if it matches two bindings. "Quick.brown.fox" does not match any bindings, so it will be discarded.
What happens if we violate the Convention and send one or four word messages, such as "orange" or "quick.orange.male.rabbit"? Then these messages will not match any bindings and will be lost.
On the other hand, "lazy.orange.male.rabbit" even if it has four characters, it will match the previous binding and will be passed to the second queue.
Description: "Topic" type of "message switch"
This type of "message switch" is powerful and can act like other "message switches."
when the queue is bound with a "#" (hash) binding key, it will receive all messages, regardless of the "routing key", as if using the "fanout" type of "message switch".
when the special characters "*" (Asterisks) and "#" (hashes) are not used for binding, the "Topic" type of "message switch" behaves like a "message switch" using the "Direct" type.
2. Code Integration
We will use the "Topic" type "message switch" in our logging system. We will start with a working hypothesis, and the log's "routing key" will have two words: "<facility>.<severity>".
The code is almost identical to the previous tutorial.
EmitLogTopic.cs's Code:
1 usingSystem;2 usingSystem.Linq;3 usingrabbitmq.client;4 usingSystem.Text;5 6 classEmitlogtopic7 {8 Public Static voidMain (string[] args)9 {Ten varFactory =NewConnectionFactory () {HostName ="localhost" }; One using(varConnection =Factory. CreateConnection ()) A using(varChannel =connection. Createmodel ()) - { -Channel. Exchangedeclare (Exchange:"Topic_logs", theType"Topic"); - - varRoutingkey = (args. Length >0) ? args[0] :"Anonymous.info"; - varMessage = (args. Length >1) +?string. Join (" ", args. Skip (1 ). ToArray ()) -:"Hello world!"; + varBODY =Encoding.UTF8.GetBytes (message); AChannel. Basicpublish (Exchange:"Topic_logs", at Routingkey:routingkey, -Basicproperties:NULL, - body:body); -Console.WriteLine ("[x] Sent ' {0} ': ' {1} '", Routingkey, message); - } - } in}
ReceiveLogsTopic.cs's Code:
1 usingSystem;2 usingrabbitmq.client;3 usingRabbitMQ.Client.Events;4 usingSystem.Text;5 6 classReceivelogstopic7 {8 Public Static voidMain (string[] args)9 {Ten varFactory =NewConnectionFactory () {HostName ="localhost" }; One using(varConnection =Factory. CreateConnection ()) A using(varChannel =connection. Createmodel ()) - { -Channel. Exchangedeclare (Exchange:"Topic_logs", type:"Topic"); the varQueueName =Channel. Queuedeclare (). QueueName; - - if(args. Length <1) - { +Console.Error.WriteLine ("Usage: {0} [Binding_key ...]", -Environment.getcommandlineargs () [0]); +Console.WriteLine ("Press [Enter] to exit."); A console.readline (); atEnvironment.exitcode =1; - return; - } - - foreach(varBindingkeyinchargs) - { in Channel. Queuebind (Queue:queuename, -Exchange"Topic_logs", to routingkey:bindingkey); + } - theConsole.WriteLine ("[*] waiting for messages. To exit Press CTRL + C"); * $ varConsumer =NewEventingbasicconsumer (channel);Panax NotoginsengConsumer. Received + = (model, ea) = - { the varBODY =ea. Body; + varMessage =Encoding.UTF8.GetString (body); A varRoutingkey =ea. Routingkey; theConsole.WriteLine ("[x] Received ' {0} ': ' {1} '", + Routingkey, - message); $ }; $ Channel. Basicconsume (Queue:queuename, -Noack:true, - Consumer:consumer); the -Console.WriteLine ("Press [Enter] to exit.");Wuyi console.readline (); the } - } Wu}
3. Run the following example:
All logs are received:
CD receivelogstopicdotnet Run "#"
To receive all logs from the device "Kern":
CD receivelogstopicdotnet Run "Kern. *”
Or if you only want to hear information about the "critical" log:
ReceiveLogsTopic.exe "*. Critical"
You can create multiple bindings:
CD receivelogstopicdotnet Run "Kern. * "*.critical"
and issue the log using the "Routing key" "Kern.critical" type:
CD emitlogtopicdotnet Run "kern.critical" "A critial Kernel Error"
It's interesting to write these programs. Note that the code does not make any assumptions about the "routing key" or "binding key," and you may want to operate with more than two "route key" parameters.
Today is the end, if the English is better, you can view the original, the original address is: http://www.rabbitmq.com/tutorials/tutorial-five-dotnet.html
"C #" RABBITMQ Learning Document (v) Topic (subject. Wildcard mode)