Go RABBITMQ Message Queuing (v): Routing message routing

Source: Internet
Author: User
Tags rabbitmq

In the previous article, we built a simple log system. Next, we'll enrich it: the ability to use different severity to listen to different levels of log. For example, we hope that only the error log will be saved to disk.

1. Bindings Bindings

In the previous article we did this by binding:

[Python]View Plaincopy
    1. Channel.queue_bind (Exchange=exchange_name,
    2. Queue=queue_name)

The binding is actually associated with Exchange and queue. Or so: The queue is interested in the content of Exchagne, and exchange has to deliver its message into the queue.

In fact, bindings can take routing_key this parameter. In fact, the name of this parameter and the basic_publish parameter name are the same. To avoid confusion, we make it a binding key.
Use a key to create the binding:

[Python]View Plaincopy
    1. Channel.queue_bind (Exchange=exchange_name,
    2. Queue=queue_name,
    3. routing_key=' black ')

For fanout exchange, this parameter is ignored.

2. Direct Exchange

The routing algorithm for Direct exchange is very simple: it can be explained by the exact match of the binding key.


Exchange X and the two queue are bound together. Q1 's binding key is orange. Q2 's binding key is black and green.
When P publish key is orange, Exchange puts it in Q1. If it's black or green, then it's going to Q2. The rest of the message will be discarded.


3. Multiple bindings multiple queue bindings the same key is possible. For example, Q1 and Q2 are bound to black. In other words, for routing key is Black's message, it will be deliver to Q1 and Q2. The rest of the message will be discarded.

4. Emitting logs

First, we're going to create a direct exchange:

[Python]View Plaincopy
    1. Channel.exchange_declare (exchange=' direct_logs ',
    2. type=' direct ')

We will use log severity as the routing key, so that consumer can be processed differently for different severity logs.
Publish

[Python]View Plaincopy
    1. Channel.basic_publish (exchange=' direct_logs ',
    2. Routing_key=severity,
    3. Body=message)

We use three kinds of severity: ' Info ', ' warning ', ' error '.

5. Subscribing

For queue, we need to bind severity:

[Python]View Plaincopy
    1. result = Channel.queue_declare (exclusive=True)
    2. Queue_name = Result.method.queue
    3. For severity in severities:
    4. Channel.queue_bind (exchange=' direct_logs ',
    5. Queue=queue_name,
    6. routing_key=severity)


6. Final version

The code for emit_log_direct.py:

[Python]View Plaincopy
  1. #!/usr/bin/env python
  2. Import Pika
  3. Import Sys
  4. Connection = Pika. Blockingconnection (Pika. Connectionparameters (
  5. host=' localhost '))
  6. Channel = Connection.channel ()
  7. Channel.exchange_declare (exchange=' direct_logs ',
  8. type=' direct ')
  9. Severity = sys.argv[1] if Len (sys.argv) > 1 else ' info '
  10. message = '. Join (sys.argv[2:]) or ' Hello world! '
  11. Channel.basic_publish (exchange=' direct_logs ',
  12. Routing_key=severity,
  13. Body=message)
  14. Print "[x] Sent%r:%r"% (severity, message)
  15. Connection.close ()


The code for receive_logs_direct.py:

[Python]View Plaincopy
  1. #!/usr/bin/env python
  2. Import Pika
  3. Import Sys
  4. Connection = Pika. Blockingconnection (Pika. Connectionparameters (
  5. host=' localhost '))
  6. Channel = Connection.channel ()
  7. Channel.exchange_declare (exchange=' direct_logs ',
  8. type=' direct ')
  9. result = Channel.queue_declare (exclusive=True)
  10. Queue_name = Result.method.queue
  11. severities = sys.argv[1:]
  12. If not severities:
  13. print >> sys.stderr, "Usage:%s [INFO] [WARNING] [ERROR]"% \
  14. (sys.argv[0],)
  15. Sys.exit (1)
  16. For severity in severities:
  17. Channel.queue_bind (exchange=' direct_logs ',
  18. Queue=queue_name,
  19. routing_key=severity)
  20. Print ' [*] waiting for logs. To exit Press CTRL + C '
  21. DEF callback (ch, method, properties, body):
  22. print "[x]%r:%r"% (Method.routing_key, body,)
  23. Channel.basic_consume (Callback,
  24. Queue=queue_name,
  25. no_ack=True)
  26. Channel.start_consuming ()

We want to record the log of warning and error in a file:

[Python]View Plaincopy
    1. $ python receive_logs_direct.py warning error > Logs_from_rabbit.log

Print all log to screen:

[Python]View Plaincopy
      1. $ python receive_logs_direct.py Info warning error
      2. [*] Waiting for logs. To exit Press CTRL + C

Go RABBITMQ Message Queuing (v): Routing message routing

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.