The Exchange-4 of RABBITMQ

Source: Internet
Author: User
Tags rabbitmq

The core idea of the RABBITMQ message model is that producers do not send messages directly to the queue. Producers do not usually know which consumers the message will be received from, according to the RABBITMQ in the first introduction, the producer does not send the message directly to the queue. Understanding will be handed over to exchange, so you need to define the exchange's message distribution model for message distribution, that is, the publisher /subscriber mode, using this model to achieve decoupling between producers and consumers.

So why did we just define a queue in our previous example and not define exchange, which is not a violation of the core idea that the producer will not send the message directly to the queue? Let's review the code before the producer releases the message:

Channel.basicpublish ("", QueueName, Messageproperties.persistent_text_plain, Msg.getbytes ());

The first argument is an empty string, which is actually exchange, which defines a default exchange.

If an empty string is used to declare an exchange, then the "Amq.direct" Exchange is used. When we create a queue, the default is to have a routingkey bound to this default exchange with the new queue name, so we've written the queuename in the second argument.

Exchange

With regard to exchange, a producer can send messages to only one interchange component (Exchange), Exchange is a very simple thing, on the one hand it receives messages from the producer, on the other hand it will put messages from the producer into the queue, Exchange must know how to receive a message, and the received message should be added to a specific queue? Or in multiple queues? Or the received message is discarded, and this rule is defined by the Exchange type.

There are several definition types for Exchange: Direct, topic (subject), headers (title), and Fanout, which can be viewed by command Rabbitmqctl list_exchanges

The following are three common types: direct, topic, headers.

Direct Exchange

Any messages sent to direct Exchange will be forwarded to the queue specified in Routekey.

1. In general, you can use RABBITMQ's own Exchange: "" (The Exchange name is an empty string, previously referred to as default Exchange).

2. No binding (binding) action is required for Exchange in this mode

3. Message delivery requires a "Routekey", and requires that the message is exactly the same as the key, which can be easily understood as the name of the queue to be sent to.

4. If the queue name specified in the Routekey does not exist in the Vhost, the message is discarded.

A simple Key code example:

Producers

Channel.exchangedeclare (Exchange_name, "direct"); Channel.basicpublish (Exchange_name,

Note: Exchange_name can be an empty string "".

Consumers:

Channel.exchangedeclare (Exchange_name, "direct"="Routingkey");
fanout Exchange

Any messages sent to Fanout Exchange will be forwarded to all the queue with that Exchange binding (binding).

1. Patterns that can be understood as routing tables (messages are routed to the queue through Exchange)

2. This mode does not require Routekey

3. This mode requires Exchange to be bound with the queue in advance, one exchange can bind multiple queues, and one queue can bind to multiple exchange.

4. If exchange that receives the message is not bound to any queue, the message is discarded.

A simple Key code example:

Producers:

Fanout means that messages are distributed to multiple consumers in the form of broadcasts

Channel.exchangedeclare (Exchange_name, "fanout"); Channel.basicpublish (Exchange_name,

Consumers:

Channel.exchangedeclare (Exchange_name, "fanout"="");
Topic Exchange

Any messages sent to topic Exchange will be forwarded to all queues that are concerned with the topic specified in the Routekey (queue)

1. This mode is more complex, simply put, is that each queue has its own topic of interest, all messages with a "title" (Routekey), Exchange will forward the message to all the topics of interest can match the Routekey fuzzy queue.

2. This mode requires Routekey, perhaps to bind exchange and queue in advance.

3. When binding, provide a topic that the queue cares about, such as "#.log.#" to indicate that the queue is concerned with all messages involving log.

4. "#" means 0 or more keywords, and "*" denotes a keyword. such as "log.*" can Match "Log.warn", cannot match "log.warn.timeout", but "log.#" can match the above two.

5. If Exchange does not find a queue that can match Routekey, this message is discarded.

A simple Key code example:

Producers:

Channel.exchangedeclare (exchange_name, "topic"={"Usa.weather.1", "China.weather.1", "usa.people.1", " China.people.1 "};  for   = key+ ": Message"; Channel.basicpublish (Exchange_name,key,null, Msg.getbytes ())}

Consumers:

Channel.exchangedeclare (exchange_name, "topic"== "*.weather.*"; Channel.queuebind (Queue, Exchange_name, key);

The Exchange-4 of RABBITMQ

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.