RabbitMQ Exchange type and working mode introduction

A RabbitMQ Exchange type

RabbitMQ commonly used switch types are: fanout, direct, topic, headers four.

1.1.Fanout

All messages sent to the exchange will be routed to all queues bound to the exchange

As shown in the picture:

insert image description here

1.2.Direct

The routing rule of the direct type switch is very simple, it will route the message to those queues whose BindingKey and RoutingKey exactly match,As shown below:insert image description here

1.3.Topic

The topic type of exchange is extended on the direct matching rule, and also routes the message to the queue that matches the BindingKey and RoutingKey. The matching rule here is slightly different, and it agrees:

  • BindingKey, like RoutingKey, is .a string separated by;
  • There can be two special characters *and in BindingKey #, which are used for fuzzy matching, where they *are used to match one word, and #they are used to match multiple words (can be 0).
    insert image description here

1.4.Headers

The headers type exchange does not rely on the matching rules of the routing key to route information, but matches according to the headers attribute in the content of the sent message. When binding a queue and an exchange, specify a set of key-value pairs. When a message is sent to the exchange, RabbitMQ will get the headers of the message, and compare whether the key-value pairs in it exactly match those specified when binding the queue and the exchange. A key-value pair of , and if they match, the message will be routed to this queue. Headers-type switches perform poorly and are not practical.

Two RabbitMQ working mode introduction

2.1.work mode (resource competition)

The producer sends a message and starts multiple consumer instances to consume the message. Each consumer only consumes part of the information, which can achieve the effect of load balancing.insert image description here

1. The message producer puts the message into the queue. There can be multiple consumers. Consumer 1 and consumer 2 listen to the same queue at the same time, and the message is consumed. C1 and C2 jointly compete for the content of the current message queue, and whoever gets it first is responsible for consuming the message (hidden danger: in the case of high concurrency, a certain message will be generated by default to be shared by multiple consumers, and a switch (syncronize) can be set to ensure one Messages can only be consumed by one consumer).

2.2.publish/subscribe publish subscription (shared resources)

When using a fanout type switch, routingKey is ignored. Each consumer definition generates a queue and binds to the same Exchange, and each consumer can consume a complete message.

insert image description here
1. Each consumer listens to its own queue
2. The producer sends the message to the broker, and the switch forwards the message to each queue bound to the switch, and each queue bound to the switch will receive the message.

2.3.routing routing mode

insert image description here

Use the direct type of Exchange, send N pieces of consumption and use different routingKeys, the consumer defines a queue and binds the queue, routingKey, and Exchange. At this time, when Exchagne uses direct mode, the message will be forwarded to the corresponding queue for consumption only when the routingKey matches exactly.

In the previous pattern, a message can be broadcast to many receivers. Now we want the receiver to receive only part of the message, for example, we log key error messages to the log file through the direct mode exchange, and at the same time print all log messages normally on the console.

apply –direct switch

There are many applications in the distributed system. These applications need to be monitored by the operation and maintenance platform. One of the important information is the log records of the server.
We need to hand over log records of different log levels to different applications for processing. How to solve?

Use the direct switch

If you want to do different processing for different messages, you cannot use the fanout type switch at this time, because it will only broadcast messages blindly. We need to use a direct type of exchange.The routing algorithm of the direct switch is very simple: As long as the routingKey of the message corresponds to the bindingKey of the queue, the message can be pushed to the queue.

insert image description here

The switch X in the figure above is a direct type switch. Among the two bound queues, the bindingKey of one queue is orange, and the bindingKey of the other queue is black and green. In this way, the messages whose routingKey is orange are sent to the queue Q1, the messages whose routingKeys are black and green are sent to the Q2 queue, and other messages are discarded.

2.4.topic topic mode (a kind of routing mode)

Use a topic-type switch, and use wildcards when binding queues to switches and bindingKeys. When the switch forwards message routing to specific queues, it will fuzzy match according to the message routingKey, which is more flexible.

In the previous mode, we sent messages to different queues according to the different log levels through direct type switches. There is a limitation here, now I want to divide the log messages not only according to the log level, but also according to the log source, how to do it?

For example, I want to monitor the error message sent by the cron service, and I want to monitor all the messages sent from the kern service. At this time, you can use RabbitMQ's topic mode (Topic).

If you want a topic-type switch, routingKey cannot be written casually, it must be a dotted word . Words can be written casually, and the characteristics of messages are generally used in production. Such as: "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit", etc. The dotted word string has a maximum length of 255 bytes. bindingKey must also be of this form. The principle behind the topic type of exchange is similar to that of the direct type: as long as the value of the bindingKey of the queue matches the routingKey of the message, the queue can receive the message. There are two differences:

  1. *(star) matches a word
  2. #Match 0 to more words
    insert image description here
    In the image above, we send a message describing an animal. The routingKey specified when the message is sent contains three words and two dots. The first word indicates the speed of the animal, the second is the color, and the third is the species:  …

Create three bindings:

Q1 binds to *.orange.*
Q2 binds to *.*.rabbitand lazy.#.

  1. Q1 Pay attention to the news of orange color animals

  2. Q2 pay attention to the news of rabbits, and all the news of lazy animals

If there is no match, the message is discarded.
If the routingKey of the sent message is "lazy.orange.male.rabbit", the last binding will be matched.
If # is used for bindingKey in a topic-type switch, it is the behavior of a fanout-type switch.
If * and # are not used in the bindingKey in the topic type switch, it is the behavior of the direct type switch

Guess you like

Origin blog.csdn.net/weixin_43811057/article/details/131377294