RabbitMQ快速入门(四、RabbitMQ高级特性:TTL延时队列)

18 篇文章 0 订阅
5 篇文章 0 订阅
本文介绍了RabbitMQ中TTL的概念和设置方法,包括消息和队列的TTL属性,以及两者的区别。通过整合Springboot,展示了如何创建和配置TTL队列,利用死信队列实现延迟处理。最后,给出了从消息生产到消费的完整示例,验证了延时队列功能的实现。
摘要由CSDN通过智能技术生成


前言

跟随B站尚硅谷学习TTL:
下面是TTL视频:
b站尚硅谷TTL视频(springboot集成RabbitMQ)

提示:以下是本篇文章正文内容,下面案例可供参考

一、TTL是什么?

TTL 是什么呢?TTL 是 RabbitMQ 中一个消息或者队列的属性,表明一条消息或者该队列中的所有消息的最大存活时间,单位是毫秒。换句话说,如果一条消息设置了 TTL 属性或者进入了设置 TTL 属性的队列,那么这条消息如果在 TTL 设置的时间内没有被消费,则会成为"死信"。如果同时配置了队列的 TTL 和消息的TTL,那么较小的那个值将会被使用,有两种方式设置 TTL。

二、TTL的设置

1.消息设置TTL

rabbitTemplate.convertAndSend("X","XC",message,correlationData->{
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });

2.队列设置TTL

在交换机,队列,交换机队列绑定的配置类中设置,后面会有代码演示springboot集成rabbitmq的配置类。

HashMap<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由Key
        args.put("x-dead-letter-routing-key","YD");
        //声明队列的TTL
        args.put("x-message-ttl",40000);

3.两者的区别

如果设置了队列的 TTL 属性,那么一旦消息过期,就会被队列丢弃(如果配置了死信队列被丢到死信队列中),而第二种方式,消息即使过期也不一定会被马上丢弃,因为消息是否过期是在即将投递到消费者之前判定的,如果当前队列有严重的消息积压情况,则已过期的消息也许还能存活较长时间;另外,还需要注意的一点是,如果不设置 TTL,表示消息永远不会过期,如果将 TTL 设置为 0,则表示除非此时可以直接投递该消息到消费者,否则该消息将会被丢弃。

前一小节我们介绍了死信队列,刚刚又介绍了 TTL,至此利用 RabbitMQ 实现延时队列的两大要素已经集齐,接下来只需要将它们进行融合,再加入一点点调味料,延时队列就可以新鲜出炉了。想想看,延时队列,不就是想要消息延迟多久被处理吗,TTL 则刚好能让消息在延迟多久之后成为死信,另一方面,成为死信的消息都会被投递到死信队列里,这样只需要消费者一直消费死信队列里的消息就完事了,因为里面的消息都是希望被立即处理的消息。


三、整合springboot

1.创建Springboot项目,导入依赖

springboot项目想必学到这里的小伙伴们已经都会了,就不演示了,我这里直接给出pom的依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <groupId>com.xyh</groupId>
    <artifactId>springboot-rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-rabbitmq</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <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.projectlombok</groupId>
                <artifactId>lombok</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.修改配置文件

spring:
  rabbitmq:
    host: 192.168.209.128
    port: 5672
    username: xyh
    password: xyh980616
    virtual-host: /xyh

3.添加swagger配置类(API接口测试,此处可以不添加)

package com.xyh.springbootrabbitmq.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("随便写", "http://随便写.com", 
"随便写@qq.com"))
 .build();
 }
}

4.创建TTL

4.1 代码架构图

创建两个队列 QA 和 QB,两者队列 TTL 分别设置为 10S 和 40S,然后在创建一个交换机 X 和死信交换机 Y,它们的类型都是 direct,创建一个死信队列 QD,它们的绑定关系如下:
在这里插入图片描述

4.2 TTL配置类

package com.xyh.springbootrabbitmq.Config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;

import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * @author xyh
 * @Decration Ttl队列 配置文件类代码
 * */
@Configuration
public class TtlQueueConfig {

    //普通交换机的名称
    public static final String X_EXCHANGE = "X";
    //死信交换机名称
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    //普通队列名称
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    //死信队列名称
    public static final String DEAD_LETTER_QUEUE = "QD";

    //声明X交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);

    }
    //声明Y交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }
    //声明队列A ttl为10s 并绑定到对应的死信交换机
    @Bean("queueA")
    public Queue queueA(){
        HashMap<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由Key
        args.put("x-dead-letter-routing-key","YD");
        //声明队列的TTL
        args.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }

    //声明队列A 绑定到x交换机
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,@Qualifier("xExchange")DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    //声明队列B ttl为40s 并绑定到对应的死信交换机
    @Bean("queueB")
    public Queue queueB(){
        HashMap<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //声明当前队列的死信路由Key
        args.put("x-dead-letter-routing-key","YD");
        //声明队列的TTL
        args.put("x-message-ttl",40000);

        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();

    }
    //声明队列B 绑定到X交换机
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,@Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    //声明死信队列 QD
    @Bean("queueD")
    public Queue queueD(){
        return new Queue(DEAD_LETTER_QUEUE);
    }

    //声明死信队列QD绑定关系
    @Bean
    public Binding deadLetterBindingQAD(@Qualifier("queueD") Queue queueD,@Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }


}


4.3消息生产者

发送消息:


("ttl")
public class SendMsgController {
    
    private RabbitTemplate rabbitTemplate;

    ("sendMsg/{message}")
    public void sendMsg( String message){
        System.out.println("发送一条信息给两个TTL队列:"+"发布时间:"+new Date()+"消息内容:"+message);


        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列:"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列:"+message);

        /*rabbitTemplate.convertAndSend("X","XC",message,correlationData->{
            correlationData.getMessageProperties().setExpiration(ttlTime);
            return correlationData;
        });*/
    }
}

此时肯定在队列QA,QB里面有两条消息


4.4消息消费者

@Slf4j
@Component
public class DeadLetterQueueConsumer {

    @RabbitListener(queues = "QD")
    public void receiveD(Message message, Channel channel){
        String s = new String(message.getBody());

        System.out.println("收到死信队列信息:"+"发布时间:"+new Date()+"消息内容:"+s);

    }
}

接受死信队列的消息,看看过完延时时间后是否消息进入死信队列


4.5 测试

1.启动SpringbootApplication类:
发现启动成功,都已经创建好了队列
在这里插入图片描述
2.发送请求地址:
http://localhost:8080/ttl/sendMsg/嘻嘻嘻
应该来说QA队列中的消息存在十秒发送到死信队列,QB中的消息存在40秒发送到死信队列
在这里插入图片描述
在这里插入图片描述
查看控制台打印结果:
发现确实死信队列隔10s和40s收到消息
在这里插入图片描述
第一条消息在 10S 后变成了死信消息,然后被消费者消费掉,第二条消息在 40S 之后变成了死信消息,
然后被消费掉,这样一个延时队列就打造完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值