RABBITMQ Learning Record--basic concepts

Source: Internet
Author: User
Tags amq

Send the monkeys to the reinforcements.

Messag queue, is the producer to throw things, consumers take away. But there is a lot of detail to be involved.

Basic concepts

is actually the official website document Porter.

Routing model

Take a look at the lifeline of a message, the producer sends the message to exchange, and then, depending on the type of exchange and the routing key (a parameter when the message is sent), route the message to a different queue, in which case the image is sent to a line and can actually be more than one. The consumer then takes the message away from the queue. Unlike Kafka, a queue inside a rabbitmq is taken away by a consumer, and it is impossible to be picked up by someone else (without considering an ACK).

Queue

A queue is a container that stores messages. Some properties

    • Name
    • Durable (the queue would survive a broker restart)
    • Exclusive (used by only one connection and the queue would be a deleted when that connection closes)
    • Auto-delete (queue is deleted when last consumer unsubscribes)
    • Arguments (some brokers use it to implement additional features like message TTL)

Before using a queue, you need to declare it first. Both producers and consumers can affirm the statement. When declaring a queue that already exists, there is nothing, but if it is declared again, the existing queue parameters are different from the arguments stated at the time, and they will be an error.

Exchange

Producers do not contact the queue, and all messages are transferred to the corresponding queue through Exchange. Each queue is bound to one or more exchange bindings. When declaring a queue, it is already bound to the default exchange.

There are four types of exchange, each of which has a different routing method ~

Let's take a look at the properties of exchange.

    • Name
    • Durability (exchanges survive broker restart)
    • Auto-delete (Exchange is deleted if all queues has finished using it)
    • Arguments (These is broker-dependent)

Before you say the Exchange type, you should know the concept of routing key first. -When the queue and exchange bindings, there is a routing key, in order to distinguish with the following routing key, or is called the binding key. -When sending a message, there is a parameter called routing Key-exchange type, bind key, routing key three things together, determines the end of the message is routed to which/which queue inside.

    1. Direct Exchange

      The message sent to direct will find the same binding key as the Routing key queue, sent past. In general, this exchange is for point-to-point messages, and a message is fixed to a specific queue. But it is also possible to send to multiple queues.

#!/usr/bin/env python#-*-Coding:utf-8-*-Import PikaImport SYSdef Main():            Body = "'.Join(SYS.argv[1:]) or ' Hello World ' Connection = Pika.blockingconnection(Pika.connectionparameters(Host=' localhost '))    Channel = Connection.Channel()    Channel.Queue_declare(Queue=' Hello1 ')    Channel.Queue_bind(Exchange=' Amq.direct ', Queue=' Hello1 ',Routing_key="Hello")    Channel.Queue_declare(Queue=' Hello2 ')    Channel.Queue_bind(Exchange=' Amq.direct ', Queue=' Hello2 ',Routing_key="Hello")    Channel.Basic_publish(Exchange=' Amq.direct ',                        Routing_key=' Hello ',                        Body=Body)    Connection.Close()if __name__ == ' __main__ ':    Main()
    1. Fanout

      This type of exchange sends messages to every queue that he or she binds to, and routing/binding key is ignored. Suitable for broadcast (and simple subscription?)

    2. Topic

      A more flexible route, routing key can be used with wildcard characters.

      * (Star) can substitute for exactly one word.
      # (hash) can substitute for zero or more words.

      Look directly at the picture.

    3. The way to headers this route is still very flexible.

      If you need to bind to a specific string, but more than one property. For example, a server message, there is an OS, CPU cores, memory size, I hope all of these match successfully, can also be a successful match, sent to your queue. Using headers exchange at this time is more convenient.

      When binding, an important parameter is X-match. If all, it means that all property matches are successful before this queue is sent. If any, that is, any one of the properties match successfully.

      There is also a python example Using pika to create headers exchanges with RabbitMQ in Python

    4. Default Exchange is listed separately on the default website, but in my opinion, default Exchange is a direct exchange. Just a few special places:

      1. When a queue is created, it is automatically bound to default Exchange. The binding key is the queue name.
      2. A queue cannot unbind from default Exchange (I am not 100% sure)

I'm just about to translate the official website document and record it. Here's a more detailed introduction to Exchange. Working with RabbitMQ exchanges This is described in more detail.

RABBITMQ Learning Record--basic concepts

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.