RabbitMQ switch, dead letter queue, delay queue, message reliability

Four switches of RabbitMQ

The role of the switch is to receive messages and forward them to the bound queue, four types: Direct, Topic, Headers and Fanout

Direct

Direct type Exchange switch, when the producer sends a message, it will strictly match the queue name bound by the producer

Topic (most flexible)

Bind the routing_key (routing key) to the queue. When sending a message, match the routing_key according to the parameters returned by the sent message, and then distribute the message to the corresponding message queue according to the matching situation;

for example:

The queue bound to the switch is # .abc, the producer sends a message when routing_key is set to any.abc (any can be any value here), then the message sent by the final producer will be sent to bind to # .abc Go to the queue

Headers

When the producer sends a message, no matter what the routing_key is set to, this message is based on the comparison result of the header parameters of the producer and the queue to determine whether to send the message.

There are two ways to set the comparison by setting the header when binding the queue:

x-match: all --------------> indicates that all headers must be matched before being sent to the queue

x-match: any --------------> indicates that the header is sent to the queue as long as there is a match

Fanout

When the producer sends a message, no matter what the routing_key is set, the message will be copied and sent to all queue queues bound to the Fanout type Exchange

 


 

RabbitMQ dead letter queue

Dead letter definition

Dead-letter, dead letter, dead letter occurs in the following situations and let the dead letter enter the dead letter queue:

1. When the consumer calls channel.basicNack or channel.basicReject, and the requeue parameter is set to false

2. The message exists in the queue for more than TTL (time-to-live)

3. The message exceeds the maximum length allowed by the queue;

The dead letter queue needs to set the dead letter queue information when configuring the queue queue

How to deal with dead letters

1. Configure the dead letter queue switch, the dead letter queue queue, the dead letter queue is essentially the same as the ordinary queue, but it can only deal with the dead letter;

2. To set the dead letter queue information for the normal queue, you need to set the following parameters with map:

x-dead-letter-exchange: Dead letter queue switch

x-dead-letter-routing-key: dead letter queue routing-key, note: if this parameter is configured, the routing-key will also be replaced by this parameter after the dead letter comes in, otherwise it will retain its own routing-key

 


 

RabbitMQ delay queue

What is the delay queue

  Delay queue means that the queue needs to be consumed after a specified time .

  Delay queues can be used in unique scenarios. For example, some scheduled notification services can be implemented through delay queues.

TTL implementation delay queue

  First,    what is TTL . TTL is the abbreviation of English time to live , which is the maximum survival time. In the previous section, it was said that after the message queue survived in the queue for more than the time set by the TTL, it would enter the dead letter queue. The delay queue is to set the TTL expiration time for the queue, and after this time expires, the message becomes a dead letter and enters the dead letter queue . This realizes the dead letter queue.

  However, the TTL implementation delay queue has the following problems:

  1. Configure TTL on the queue, which is not extensible. Each business needs a different TTL and a new queue is needed to configure it, which is unreasonable. Therefore, the TTL can be set in the message itself.

  2. The queue has a first-in first-out feature. Before the first message is processed into a dead letter, the second message cannot be processed. For example, queue A comes in first and sets the TTL for 10 seconds, then immediately queues B in and sets the TTL for 1 second. This happens at this time. Before queue A is processed into a dead letter (it takes 10 seconds), queue B sets the TTL to 1 second. In theory, queue B will be processed into a dead letter after 1 second, but, In fact, RabbitMQ will process A first when processing, and the queues behind will wait in turn. So, you need to implement RabbitMQ's delay queue plug-in, you can see the following links and methods.

Plugin implements delay queue

  First, download the plugin on the official website:  rabbitmq_delayed_message_exchange, https://www.rabbitmq.com/community-plugins.html , and then put it in its plugin directory and restart.

  After the installation is successful, it can be processed as a dead letter after 1 second in the queue B of the second queue, and as a dead letter in the first queue of the A queue after 10 seconds. There is no problem with the order of processing.

 


 

Guess you like

Origin www.cnblogs.com/lcmlyj/p/12742979.html