RabbitMQ from entry to advanced (initial delay queue)

Get into the habit of writing together! This is the 16th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the event details .

Seven, delay queue

7.1 Delay Queue Concept

Delay queue, the interior of the queue is ordered, and the most important feature is reflected in its delay attribute. The elements in the delay queue are expected to be taken out and processed after or before the specified time. In short, delay A queue is a queue used to store elements that need to be processed at a specified time.

7.2 Delayed queue usage scenarios

  1. If the order is not paid within ten minutes, it will be automatically cancelled

  2. For newly created stores, if no products have been uploaded within ten days, a message reminder will be sent automatically.

  3. After the user has successfully registered, if the user does not log in within three days, a SMS reminder will be sent.

  4. The user initiates a refund, and if it is not processed within three days, the relevant operator will be notified.

  5. After the meeting is scheduled, each participant needs to be notified ten minutes before the scheduled time to participate in the meeting

These scenarios have a characteristic that a certain task needs to be completed at a specified time point after or before an event occurs ,

For example: When an order generation event occurs, check the payment status of the order after ten minutes, and then close the unpaid order; it seems to use a timed task, poll the data all the time, check it every second, and take out the data that needs to be processed. And then it's over, isn't it?

If the amount of data is relatively small, this can indeed be done. For example, for the requirement of "automatic settlement if the bill is not paid within a week", if the time is not strictly limited, but a week in a loose sense, then run every night. A cron job to check all unpaid bills is indeed a viable option. However, for scenarios with a relatively large amount of data and strong timeliness,

For example: "Close the order if it is not paid within ten minutes", there may be a lot of unpaid order data in the short term, and it may even reach the level of one million or even ten million during the event. It is obvious that the polling method is still used for such a huge amount of data. It is not advisable, it is very likely that the checks of all orders cannot be completed within one second, and at the same time, it will put a lot of pressure on the database, cannot meet business requirements, and has low performance.

7.3 Queue setting TTL

What is TTL? TTL is an attribute of a message or queue in RabbitMQ, indicating the maximum survival time of a message or all messages in the queue, in milliseconds.

In other words, if a message has a TTL attribute set or enters a queue with a TTL attribute set, the message will become a "dead letter" if it is not consumed within the time set by the TTL. **If both queue TTL and message TTL are configured, the smaller value will be used. There are two ways to set TTL.

7.3.1 Queue setting TTL

Set the queue's "x-message-ttl" property when creating the queue

image-20220316103009860

7.3.2 Message Setting TTL

is to set TTL for each message

image-20220316103119220

If the TTL property of the queue is set, then once the message expires, it will be discarded by the queue (if the dead letter queue is configured to be thrown into the dead letter queue), and in the second method, even if the message expires, it will not necessarily be immediately discarded. Discard, because whether the message expires is determined before it is about to be delivered to the consumer. If there is a serious backlog of messages in the current queue, the expired message may survive for a long time; in addition, it should be noted that if If the TTL is not set, it means that the message will never expire. If the TTL is set to 0, it means that the message will be discarded unless it can be directly delivered to the consumer at this time.

TTL has just been introduced. So far, the two elements of using RabbitMQ to realize the delay queue have been collected. Next, you only need to integrate them and add a little seasoning, and the delay queue can be freshly released. Think about it, == delay queue, isn't it just how long you want the message to be delayed to be processed, TTL is just enough to make the message become dead letter after the delay ==, on the other hand, the message that becomes the dead letter will be delivered to In the dead letter queue, the consumer only needs to keep consuming the messages in the dead letter queue, because the messages in it are all messages that want to be processed immediately.

7.4 Integrate SpringBoot

7.4.1 Add dependencies

<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!--RabbitMQ 依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!--RabbitMQ 测试依赖-->
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
复制代码

7.4.2 Modify the configuration file

spring:
  rabbitmq:
    host: 192.168.42.96
    port: 5672
    username: admin
    password: 123
复制代码

7.4.3 Add Swagger configuration class

package com.caq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig() {

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("rabbitmq 接口文档")
                .description("本文档描述了 rabbitmq 微服务接口定义")
                .version("1.0")
                .contact(new Contact("enjoy6288", "http://[email protected]", "[email protected]"))
                .build();
    }
}

复制代码

Guess you like

Origin juejin.im/post/7086991917367443463