RibbitMQ学习笔记延迟队列

总目录如下:
1.RibbitMQ学习笔记之MQ 的相关概念
2.RibbitMQ学习笔记之MQ练习
3.RibbitMQ学习笔记之MQ练习
4.RibbitMQ学习笔记之MQ发布确认
5.RibbitMQ学习笔记之交换机实战
6.RibbitMQ学习笔记之死信队列
7.RibbitMQ学习笔记延迟队列
8.RibbitMQ学习笔记之发布确认高级
9.RibbitMQ学习笔记之 RabbitMQ 其他知识点扩展
10.RibbitMQ学习笔记之RabbitMQ 集群


7. 延迟队列

7.1. 延迟队列概念

在这里插入图片描述

延时队列,队列内部是有序的,最重要的特性就体现在它的延时属性上,延时队列中的元素是希望在指定时间到了以后或之前取出和处理,简单来说,延时队列就是用来存放需要在指定时间被处理的元素的队列。

7.2. 延迟队列使用场景

  1. 订单在十分钟之内未支付则自动取消

  2. 新创建的店铺,如果在十天内都没有上传过商品,则自动发送消息提醒。

  3. 用户注册成功后,如果三天内没有登陆则进行短信提醒。

  4. 用户发起退款,如果三天内没有得到处理则通知相关运营人员。

  5. 预定会议后,需要在预定的时间点前十分钟通知各个与会人员参加会议

这些场景都有一个特点,需要在某个事件发生之后或者之前的指定时间点完成某一项任务,如:发生订单生成事件,在十分钟之后检查该订单支付状态,然后将未支付的订单进行关闭;看起来似乎使用定时任务,一直轮询数据,每秒查一次,取出需要被处理的数据,然后处理不就完事了吗?如果数据量比较少,确实可以这样做,比如:对于“如果账单一周内未支付则进行自动结算”这样的需求,如果对于时间不是严格限制,而是宽松意义上的一周,那么每天晚上跑个定时任务检查一下所有未支付的账单,确实也是一个可行的方案。但对于数据量比较大,并且时效性较强的场景,如:“订单十分钟内未支付则关闭“,短期内未支付的订单数据可能会有很多,活动期间甚至会达到百万甚至千万级别,对这么庞大的数据量仍旧使用轮询的方式显然是不可取的,很可能在一秒内无法完成所有订单的检查,同时会给数据库带来很大压力,无法满足业务要求而且性能低下。

在这里插入图片描述

7.3. RabbitMQ 中的 TTL

TTL 是什么呢?TTL 是 RabbitMQ 中一个消息或者队列的属性,表明一条消息或者该队列中的所有消息的最大存活时间

7.3.1 两者的区别

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

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

7.4. 整合 springboot

7.4.1. 创建项目

在这里插入图片描述

7.4.2. 添加依赖

<dependencies>
<!--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>2.9.2</version>
 
</dependency> <dependency>
 
<groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version>
 
</dependency>
<!--RabbitMQ  测试依赖-->
 
<dependency> <groupId>org.springframework.amqp</groupId>
 
<artifactId>spring-rabbit-test</artifactId> <scope>test</scope>
 
</dependency> </dependencies>

7.4.3. 修改配置文件

spring.rabbitmq.host=182.92.234.71
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
wo 本地的
spring.rabbitmq.host=39.107.43.12
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

7.4.4. 添加 Swagger 配置类

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://atguigu.com", "1551388580@qq.com"))
.build();
 
}
 
}
 
 

7.5. 队列 TTL

在这里插入图片描述

7.5.1. 代码架构图

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

7.5.2. 配置文件类代码
 
@Configuration
public class TtlQueueConfig {
 
public static final String X_EXCHANGE = "X"; 
public static final String QUEUE_A = "QA"; 
public static final String QUEUE_B = "QB";
 
public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
 public static final String DEAD_LETTER_QUEUE = "QD";
 
// 声明 xExchange
 
@Bean("xExchange")
public DirectExchange xExchange(){
return new DirectExchange(X_EXCHANGE);
 
}
// 声明 xExchange
 
@Bean("yExchange")
public DirectExchange yExchange(){
return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
}
 
//声明队列 A ttl 为 10s 并绑定到对应的死信交换机

@Bean("queueA") public Queue queueA(){
Map<String, Object> args = new HashMap<>(3);
 
//声明当前队列绑定的死信交换机 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(){
 
Map<String, Object> args = new HashMap<>(3);
 
//声明当前队列绑定的死信交换机 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 queue1B,
 @Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queue1B).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");
}
 
}
 
package com.atguigu.springbootrabbitmq.config;

import org.mapstruct.Qualifier;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@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_EXCHANGE  别名注入的时候用
    @Bean("xEXCHANGE")
    public DirectExchange x_EXCHANGE() {
        return new DirectExchange(X_EXCHANGE);
    }


    //声明y_EXCHANGE  别名注入的时候用
    @Bean("yEXCHANGE")
    public DirectExchange y_EXCHANGE() {
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }


    //队列一
    //声明y_EXCHANGE  别名注入的时候用  ttl为10s
    @Bean("queueA")
    public Queue queueA() {
        //3  代表map的长度 因为现在是3个 就写一个3  可不写
        Map<String, Object> arguments = new HashMap<>(3);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信的RoutingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置ttl  单位ms
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).
                withArguments(arguments).build();

    }


    //队列二
    //声明y_EXCHANGE  别名注入的时候用  ttl为10s
    @Bean("queueB")
    public Queue queueB() {
        //3  代表map的长度 因为现在是3个 就写一个3  可不写
        Map<String, Object> arguments = new HashMap<>(3);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信的RoutingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        //设置ttl  单位ms
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).
                withArguments(arguments).build();

    }




    //队列三 死信队列
    //声明y_EXCHANGE  别名注入的时候用  ttl为10s
    @Bean("queueD")
    public Queue queueQD() {
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).
               build();

    }


//绑定  交换机和队列 进行绑定

    @Bean
    public Binding queueABindxingX(@org.springframework.beans.factory.annotation.Qualifier("queueA") Queue queueA,
                                   @org.springframework.beans.factory.annotation.Qualifier("xEXCHANGE") DirectExchange xEXCHANGE){
  //x的交换机与qa队列绑定 他们的routing的值是XA
        return BindingBuilder.bind(queueA).to(xEXCHANGE).with("XA");
    }


    //绑定  交换机和队列 进行绑定

    @Bean
    public Binding queueBBindxingX(@org.springframework.beans.factory.annotation.Qualifier("queueB") Queue queueB,
                                   @org.springframework.beans.factory.annotation.Qualifier("xEXCHANGE") DirectExchange xEXCHANGE){
        //x的交换机与qb队列绑定 他们的routing的值是XB
        return BindingBuilder.bind(queueB).to(xEXCHANGE).with("XB");
    }


    //绑定  死信交换机和队列 进行绑定

    @Bean
    public Binding queueDBindxingX(@org.springframework.beans.factory.annotation.Qualifier("queueD") Queue queueD,
                                   @org.springframework.beans.factory.annotation.Qualifier("yEXCHANGE") DirectExchange yEXCHANGE){
        //x的交换机与qb队列绑定 他们的routing的值是XB
        return BindingBuilder.bind(queueD).to(yEXCHANGE).with("YD");
    }



}
7.5.3. 消息生产者代码
@Slf4j 
@RequestMapping("ttl")
 @RestController
 
public class SendMsgController { 
@Autowired
 
private RabbitTemplate rabbitTemplate;
 
@GetMapping("sendMsg/{message}")
 
public void sendMsg(@PathVariable String message){

log.info("当前时间:{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
 
rabbitTemplate.convertAndSend("X",  "XA",  "消息来自 ttl 为 10S 的队列: "+message);
 
rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
}
 
}
 

7.5.4. 消息消费者代码
在这里插入图片描述

@Slf4j
 
@Component
 
public class DeadLetterQueueConsumer {
 
@RabbitListener(queues = "QD")
 
public void receiveD(Message message, Channel channel) throws IOException {
 String msg = new String(message.getBody());
 
log.info("当前时间:{},收到死信队列信息{}", new Date().toString(), msg);
}
}

发起一个请求 http://localhost:8080/ttl/sendMsg/嘻嘻嘻

第一条消息在 10S 后变成了死信消息,然后被消费者消费掉,第二条消息在 40S 之后变成了死信消息,然后被消费掉,这样一个延时队列就打造完成了。

不过,如果这样使用的话,岂不是每增加一个新的时间需求,就要新增一个队列,这里只有 10S 和 40S 两个时间选项,如果需要一个小时后处理,那么就需要增加TTL 为一个小时的队列,如果是预定会议室然后提前通知这样的场景,岂不是要增加无数个队列才能满足需求?

7.6. 延时队列优化

7.6.1. 代码架构图

在这里新增了一个队列 QC,绑定关系如下,该队列不设置TTL 时间

在这里插入图片描述

7.6.2. 配置文件类代码

@Component
public class MsgTtlQueueConfig {
 
public static final String Y_DEAD_LETTER_EXCHANGE = "Y"; public static final String QUEUE_C = "QC";
 
//声明队列 C 死信交换机
 
@Bean("queueC") public Queue queueB(){
 
Map<String, Object> args = new HashMap<>(3);
//声明当前队列绑定的死信交换机
 
args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
//声明当前队列的死信路由 key
 
args.put("x-dead-letter-routing-key", "YD");
//没有声明 TTL 属性
 
return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
}
//声明队列 B 绑定 X 交换机
 
@Bean
 
public Binding queuecBindingX(@Qualifier("queueC") Queue queueC, @Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueC).to(xExchange).with("XC");
}
 
}

7.6.3. 消息生产者代码

@GetMapping("sendExpirationMsg/{message}/{ttlTime}")
 
public void sendMsg(@PathVariable String message,@PathVariable String ttlTime) { 
rabbitTemplate.convertAndSend("X", "XC", message, correlationData -
 
>
{ correlationData.getMessageProperties().setExpiration(ttlTime); return correlationData;
});
 
log.info("当前时间:{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(),ttlTime, message);
 
}

发起请求代码

package com.atguigu.springbootmq.consumer;



import com.atguigu.springbootmq.config.TtlQueueConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


import java.util.Date;

/**
 * 队列 ttl的消费者
 */

@Slf4j
@Component  //装饰器语法而已,
public class DeadLetterQueueConsumer {

    //接受消息
    @RabbitListener(queues="QD")
  //  @RabbitListener(queues = {TtlQueueConfig.DEAD_LETTER_QUEUE})
    public void receiveD(Message message, Channel channel)throws Exception{
        String msg = new String(message.getBody());
        log.info("当前时间:{},收到死信队列信息{}",new Date().toString(),msg);

    }

}
package com.atguigu.springbootmq.Controller;

import com.atguigu.springbootmq.config.TtlQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 发送延迟信息
 * 生产者
 */
@Slf4j //日志
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //开始发送消息
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间:{},发送一条信息给两个TTl队列:{}",new Date().toString(),message);


        rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
        rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
//        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XA","消息来自10s的"+message);
//        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XB","消息来自40s的"+message);
    }
//开始发消息  和ttl
    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable String message,@PathVariable String ttlTime){
        log.info("当前时间:{},发送一条时长:{}毫秒信息给TTl队列C:{}",new Date().toString(),ttlTime,message);
        rabbitTemplate.convertAndSend(TtlQueueConfig.X_EXCHANGE,"XC",message,msg->{
            //发送消息 延迟时长
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });

    }

}

http://localhost:8080/ttl/sendExpirationMsg/你好 1/20000
http://localhost:8080/ttl/sendExpirationMsg/你好 2/2000

在这里插入图片描述

看起来似乎没什么问题,但是在最开始的时候,就介绍过如果使用在消息属性上设置 TTL 的方式,消息可能并不会按时“死亡“,因为 RabbitMQ 只会检查第一个消息是否过期,如果过期则丢到死信队列,如果第一个消息的延时时长很长,而第二个消息的延时时长很短,第二个消息并不会优先得到执行。

7.7. Rabbitmq 插件实现延迟队列

上文中提到的问题,确实是一个问题,如果不能实现在消息粒度上的 TTL,并使其在设置的TTL 时间及时死亡,就无法设计成一个通用的延时队列。那如何解决呢,接下来我们就去解决该问题。
在这里插入图片描述

7.7.1. 安装延时队列插件

在这里插入图片描述

在官网上下载 https://www.rabbitmq.com/community-plugins.html,下载

rabbitmq_delayed_message_exchange 插件,然后解压放置到 RabbitMQ 的插件目录。

进入 RabbitMQ 的安装目录下的 plgins 目录,执行下面命令让该插件生效,然后重启 RabbitMQ

/usr/lib/rabbitmq/lib/rabbitmq_server-3.8.8/plugins

把插件放到指定的目录
在这里插入图片描述

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

在这里插入图片描述

安装成功了

重启了
systemctl restart rabbitmq-server

https://www.bilibili.com/video/BV1cb4y1o7zz?p=66

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7.7.2. 代码架构图

在这里新增了一个队列delayed.queue,一个自定义交换机 delayed.exchange,绑定关系如下:

在这里插入图片描述

7.7.3. 配置文件类代码

在我们自定义的交换机中,这是一种新的交换类型,该类型消息支持延迟投递机制 消息传递后并不会立即投递到目标队列中,而是存储在 mnesia(一个分布式数据系统)表中,当达到投递时间时,才投递到目标队列中。

@Configuration
public class DelayedQueueConfig {
 
public static final String DELAYED_QUEUE_NAME = "delayed.queue";
 public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange"; 
 public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
 
@Bean
 
public Queue delayedQueue() {
 
return new Queue(DELAYED_QUEUE_NAME);
 
}
//自定义交换机 我们在这里定义的是一个延迟交换机
 
@Bean
public CustomExchange delayedExchange()
 
{ Map<String, Object> args = new HashMap<>();
 
//自定义交换机的类型 args.put("x-delayed-type", "direct");
 
return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
}
 
@Bean
 
public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue, @Qualifier("delayedExchange") CustomExchange
 
delayedExchange) { return
 
BindingBuilder.bind(queue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
}
 
}
 
 

7.7.4. 消息生产者代码

public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange"; public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
 
@GetMapping("sendDelayMsg/{message}/{delayTime}")
 
public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime) { rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, message,
 
correlationData ->{ correlationData.getMessageProperties().setDelay(delayTime); return correlationData;
});
 
log.info(" 当 前 时 间 : {}, 发 送 一 条 延 迟                                            {} 毫 秒 的 信 息 给 队 列                                                                                             delayed.queue:{}",         new
 
Date(),delayTime, message);
}

7.7.5. 消息消费者代码

public static final String DELAYED_QUEUE_NAME = "delayed.queue"; 
@RabbitListener(queues = DELAYED_QUEUE_NAME)
 
public void receiveDelayedQueue(Message message){ 
String msg = new String(message.getBody());
 
log.info("当前时间:{},收到延时队列的消息:{}", new Date().toString(), msg);

发起请求:

http://localhost:8080/ttl/sendDelayMsg/come on baby1/20000
http://localhost:8080/ttl/sendDelayMsg/come on baby2/2000

第二个消息被先消费掉了,符合预期

7.8. 总结

延时队列在需要延时处理的场景下非常有用,使用 RabbitMQ 来实现延时队列可以很好的利用 RabbitMQ 的特性,如:消息可靠发送、消息可靠投递、死信队列来保障消息至少被消费一次以及未被正确处理的消息不会被丢弃。另外,通过 RabbitMQ 集群的特性,可以很好的解决单点故障问题,不会因为单个节点挂掉导致延时队列不可用或者消息丢失。

当然,延时队列还有很多其它选择,比如利用 Java 的 DelayQueue,利用 Redis 的 zset,利用 Quartz

或者利用 kafka 的时间轮,这些方式各有特点,看需要适用的场景

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默语佬

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值