RabbitMQ advanced features
Consumption current limit
What is consumer end flow restriction
Scenario:
Assuming that the MQ server receives many unprocessed messages, these messages will instantly hit the consumer client. When such a large number of messages are received, the client cannot process them. Therefore, it is necessary to limit the flow between MQ and the consumer to limit the consumption flow when the consumer consumes
Solution:
RabbitMQ provides a qos (quality of service assurance) function. On the premise of non automatic confirmation messages, it sets the preprocessed traffic of channel or queue to limit the traffic. The method is as follows:
void BasicQos(uint prefetchSize,ushort prefetchCount,bool global);
Parameter Description:
prefetchSize: 0
prefetchCount: the maximum number of messages consumed. If there are already the maximum number of messages in the channel or queue and they are not signed in, no more messages will be consumed
global: whether to apply the above settings to the channel. Basically, false is used
Or configure it in application.yml
Code configuration
spring: rabbitmq: host: 127.0.0.1 port: 5672 username: admin password: admin virtual-host: my_vhost listener: simple: prefetch: 1 # The number of preprocessed messages, that is, how many messages can be processed in one request
TTL
TTL full name time to live
If the current message has not been consumed after reaching the lifetime, it will be cleared
RabbitMQ can set the expiration time for messages or for the entire queue
Queue expiration time
// Set queue expiration time when creating a queue @Bean public Queue topicQueue() { return QueueBuilder.nonDurable("topic_queue").ttl(1000).build(); }
Message expiration time
Message messageBody = new Message(message.getBytes()); // Set message parameters MessageProperties messageProperties = messageBody.getMessageProperties(); // Set message expiration time messageProperties.setExpiration("5000"); rabbitTemplate.convertAndSend("topic_exchange", routing, messageBody);
Summary:
- When the expiration time is set for both messages and queues, whichever is shorter
- When the queue expires, messages in the entire queue are cleared
- When there are multiple messages in the queue, only the top message will be judged whether it has expired
Dead letter queue
Dead letter queue, English abbreviation: DLX, Dead Letter Exchange (Dead Letter Exchange). When a message is called Dead message, it can be re sent to another switch, which is DLX
There are three situations in which messages are called dead letter:
- Queue message length reached limit
- The consumer refuses to sign in the message, and the request is set to false (do not return to the queue)
- The message has set an expiration time, which has not been consumed
Set dead letter switch and dead letter queue and bind them
// There is no difference between dead letter switch and dead letter queue @Bean public Exchange topicExchangeDlx() { return ExchangeBuilder.topicExchange("topic_exchange_dlx").build(); } @Bean public Queue topicQueueDlx() { return QueueBuilder.nonDurable("topic_queue_dlx").build(); } @Bean public Binding topicBindingDlx(@Qualifier("topicQueueDlx") Queue topicQueue, @Qualifier("topicExchangeDlx") Exchange topicExchange) { return BindingBuilder.bind(topicQueue).to(topicExchange).with("dlx_queue.#").noargs(); // dlx_queue. # is the routing rule }
Bind a dead letter switch to a queue
@Bean public Queue topicQueue() { return QueueBuilder.nonDurable("topic_queue") // Binding dead letter switch .deadLetterExchange("topic_exchange_dlx") // Set dead letter routingkey .deadLetterRoutingKey("dlx_queue.boot") .ttl(3000) .maxLength(2) .build(); }
After configuring the above code, when the queue topic_ When there are three situations of dead letter in the queue, i.e. expiration, rejection and long queue, the message will be directly put into the dead letter queue
Summary:
- Dead letter switch and dead letter queue are no different from ordinary ones
- When the message is called dead letter, it will be routed by the dead letter switch to the corresponding dead letter queue according to the routingKey rule
Delay queue
Delay queue: messages will not be consumed immediately after they arrive at the queue, but only after they arrive at the specified time
Application scenario:
- Scheduled order expiration mechanism or similar scenario
RabbitMQ implements delay queue and TTL + dead letter queue
Set the 30 minute expiration time for ordinary queue messages. After expiration, they will be put into the dead letter queue, and then the business system only needs to consume the messages in the dead letter queue
Implementation steps
-
Define dead letter switch and dead letter queue
-
Define common switch and common queue and bind dead letter switch
-
Set the message expiration time to 30 minutes
-
Listen to the dead letter queue and get the message for business processing
The above steps are similar to the code of dead letter queue, and are not repeated
Logging and monitoring
Message tracking
RabbitMQ trace