Rabbitmq -- topic,

Source: Internet
Author: User

Rabbitmq -- topic,
I. Preface

As mentioned above, direct Exchange routing rules fully match binding key and routing key, but this strict matching method cannot meet actual business requirements in many cases. Exchange of the topic type is extended in the matching rules. It is similar to the Exchage of the direct type and routes messages to the Queue that matches the binding key and the routing key, however, the matching rules here are somewhat different, and the conventions are as follows:

  • Routing key is a period ". "separator string (we will be separated by the period". "Each separate string is called a word), such as" stock. usd. nyse "," nyse. vmw and quick. orange. rabbit"
  • The binding key and the routing key are also strings separated by periods (.).
  • There can be two special characters "*" and "#" in the binding key for Fuzzy Matching. "*" is used to match a word, "#" is used to match multiple words (it can be zero words)

  

Take the configuration in as an example, routingKey = "quick. orange. rabbit messages are routed to Q1 and Q2 at the same time, routingKey = "lazy. orange. the fox messages are routed to Q1 and Q2, routingKey = "lazy. brown. the fox message is routed to Q2, routingKey = "lazy. pink. rabbit messages are routed to Q2 (only once delivered to Q2, although the two bindingkeys of this routingKey and Q2 match); routingKey = "quick. brown. fox ", routingKey =" orange ", routingKey =" quick. orange. male. rabbit messages will be discarded because they do not match any bindingKey.

2. Exchange topic

There are not many changes to the topic and direct, that is, the routing key and bind key need to be modified.

Production end:

#-*-Coding: UTF-8-*-import pika # create a connection = pika. blockingConnection (pika. connectionParameters (host = 'localhost') # create a channel = connection. channel () # declare exchange and channel type. exchange_declare (exchange = 'topic _ log', exchange_type = 'topic ') # input information in the format *. info from *. info test is similar to input_data = input ('>> :'). strip () # splits the input information by spaces and converts it to a list of data_list = input_data.split ('') # triples. If the input information exists, use the input information data_li St [0]; otherwise, use 'Anonymous. info 'severity = data_list [0] if len (data_list)> 1 else' anonymous. info 'message = ''. join (data_list [2:]) or 'hello, world! '# The routing_key here is data_list [0] Or 'info' channel. basic_publish (exchange = 'topic _ log', routing_key = severity, body = message) print ('[x] Sent % r: % R' % (severity, message) connection. close ()

Consumer:

  

#-*-Coding: UTF-8-*-import pikaconnection = pika. blockingConnection (pika. connectionParameters (host = 'localhost') channel = connection. channel () # declare exchange and channel type. exchange_declare (exchange = 'topic _ log', exchange_type = 'topic ') result = channel. queue_declare (exclusive = True) queue_name = result. method. queue # Here we define some lists, the list content is as follows # the two lists are used to test the match with routing_key # The first one is only allowed to receive info information # the second one is allowed to receive error and mysql information # severities = ['*. info'] severities = ['*. error ', 'mysql. * '] for severity in severities: channel. queue_bind (exchange = 'topic _ log', 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 started two consumers respectively during the test.

Use severities = ['*. info'] In the first consumer1.

Use severities = ['*. error', 'mysql. *'] In the second consumer2.

The producer inputs:

appache.info from appache info testnginx.error from nginx error testmysql.info from mysql info test

You can see that the log information is summarized into two consumer, where consumer1 receives information from appache.info and mysql.info, while consumer2 receives information from nginx. error and mysql.info.

Related Article

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.