RABBITMQ Official Website Tutorial---Topics

Source: Internet
Author: User

(using Python client Pika 0.9.8)


In the previous tutorial we have improved our log system. We use a direct type of exchange instead of a virtual broadcast with an fanout type of exchange to get an optional receive log.


Although using the direct type of exchange improves our system, it is still limited-it is not routed based on multiple criteria.


In our log system, we may want to not only subscribe to logs based on severity, but also source-based production logs. You may have learned this concept from the Unix syslog tool, which is based on the severity of routing logs and facilities.


That would give us a lot of flexibility-we might just want to listen to a serious error from the original ' cron ' rather than all the ' Kem ' logs.


In order to implement our logging system, we need to learn more about topic's exchange.


Topic Exchange

Sending a message to an topic type of exchange cannot have an arbitrary routing_key-it must be a list of words separated by dots. These words may be anything, but usually they specify the characteristics of some connected messages. Some valid routing key examples: "Stock.usd.nyse", "NYSE.VMW", "Quick.orange.rabbit". Probably in the routing key Sea oil more you want the word, the line is 255 bytes.


The binding key must be in the same way. The logic behind the topic type of exchange is similar to direct-the message sent with a special routing key is passed to all the queues that are bound with the matching binding key. However, there are two important special examples of binding keys:

* (Star) can define exactly one word.

# (hash) can subscribe to 0 or more words.

One example is the easiest to explain this:


In this example we will send to all the messages describing the animals. This message will be sent using a routing key consisting of three words (two dots). The first word in the routing key will describe a quick move, the second one is a color, and the third one is the species:

"<celerity>.<colour>.<species>".

We create three bindings: Q1 is bound using the Bind key "*.orange.*" and Q2 is bound using "*.*.rabbit" and "lazy.#".


These bindings can be summarized as:

Q1 is interested in all the orange animals.

Q2 wanted to monitor everything about rabbits and lazy animals.


Messages that are set to "Quick.orange.rabbit" with a routing key are passed to two queues. The message "Lazy.orange.elephant" will also enter both of them. On the other hand, "Quick.orange.fox" will only go into the first queue and "Lazy.brown.fox" only enter the second one. "Lazy.pink.rabbit" will be passed a second queue only once, although it matches two bindings. "Quick.brown.fox" does not histograms any bindings, so it will be discarded.


What happens if we interrupt our communications and send a message with one or four words like "orange" or "quick.orange.male.rabbit"? Those messages will not match any of the bindings and will be lost.


On the other hand, although "lazy.orange.male.rabbit" has four words, it will match the last binding and pass to the second queue.

Topic exchangetopic type of exchange is powerful and can have behavior like other exchange. When a queue is bound with a "#" (hash) binding key-it will receive all messages, ignoring the routing key-just like with fanout type Exchange. When special characters "*" (Star) and "#" (hash) are not used for binding, exchange of type topic will only resemble direct's behavior.


put the code together .

We will use topic type of exchange in our log system. We'll start a job, assuming the log's routing key has two words: "<facility>.<severity>".


All the code is almost similar to the previous tutorial.


Emit_log_topic.py's Code:

#!/usr/bin/env pythonimport pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ')) channel = Connection.channel () channel.exchange_declare (Exchange = ' topic_ Logs ', type= ' topic ') Routing_key = sys.argv[1] If len (SYS.ARGV) > 1 Else ' anonmyous.info ' message = '. Join (sys.argv[2:] ) or ' Hello world! ' Channel.basic_publish (exchange= ' topic_logs ', Routing_key=routing_key, body=message) print "[x] Sent%r:%r"% (routing_ Key, message) Connection.close ()


Receive_logs_topic's Code:

#!/usr/bin/env pythonimport pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ')) channel = Connection.channel () channel.exchange_declare (Exchange = ' topic_ Logs ', type= ' topic ') result = Channel.queue_declare (exclusive=true) queue_name = Result.method.queuebinding_keys =    Sys.argv[1:]if not binding_keys:print >> sys.stderr, "usage:%s [Binding_key] ..."% (sys.argv[0],) sys.exit (1) For Binding_key in Binding_keys:channel.queue_bind (exchange= ' topic_logs ', Queue=queue_name, Routing_key=binding_key ) 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 () 


Receive all logs that can be run:

Python receive_logs_topic.py "#"

In order to receive all "kern" logs from the facility:

Python receive_logs_topic.py "kern.*"

Or if you only want to listen to the "critical" log:

Python receive_logs_topic.py "*.critical"


You can create multiple bindings:

Python receive_logs_topic.py "kern.*" "*.critical"

The routing key for the production log is "kern.critical" type:

Python emit_log_topic.py "kern.critical" "A Critical kernel Error"


?



RABBITMQ Official Website Tutorial---Topics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.