死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到 queue 里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列
应用场景:
1、我们先看一下案例的整体架构图
2、演示第一种情况:消息TTL过期
//正常队列绑定死信队列信息
Map<String, Object> params = new HashMap<>();
//正常队列设置死信交换机
params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
//正常队列设置死信 routingKey
params.put("x-dead-letter-routing-key", "lisi");
首先,过期时间肯定是设置给消息的,所以应该在生产者发出消息前进行设置
这就用到了我们发送消息的第三个参数,通过 AMQP.BasicProperties 指明过期时间
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().expiration("10000").build();
1️⃣ 生产者
package com.atguigu.rabbitmq.eight;
import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
/**
* @author Bonbons
* @version 1.0
* 死信队列的生产者
*/
public class Producer {
public static final String NORMAL_QUEUE = "normal_queue";
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static void main(String[] args) throws Exception{
Channel channel = RabbitMqUtils.getChannel();
//为了实现消息的存活时间为10s
AMQP.BasicProperties properties = new AMQP.BasicProperties()
.builder().expiration("10000").build();
for (int i = 1; i < 11; i++) {
String message = "info" + i;
//演示过期成为死信消息
channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", properties, message.getBytes("UTF-8"));
}
}
}
2️⃣ 消费者1
package com.atguigu.rabbitmq.eight;
import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import java.util.HashMap;
import java.util.Map;
/**
* @author Bonbons
* @version 1.0
* 演示我们死信队列的消费者1号
*/
public class Consumer01 {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static final String DEAD_EXCHANGE = "dead_exchange";
public static final String NORMAL_QUEUE = "normal_queue";
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws Exception{
//获取信道
Channel channel = RabbitMqUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//正常队列绑定死信队列信息
Map<String, Object> params = new HashMap<>();
//正常队列设置死信交换机
params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
//正常队列设置死信 routingKey
params.put("x-dead-letter-routing-key", "lisi");
//声明队列 [此处用到了第四个参数,实现将普通队列与死信交换机关联起来]
channel.queueDeclare(NORMAL_QUEUE, false, false, false, params);
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//将我们的队列和交换机绑定到一起
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
System.out.println("等待接收消息......");
channel.basicConsume(NORMAL_QUEUE, true, (consumersTag, message) -> {
String msg = new String(message.getBody(), "UTF-8");
System.out.println("Consumer01接收到的消息: " + msg);
}, consumersTag -> {});
}
}
3️⃣ 消费者2
package com.atguigu.rabbitmq.eight;
import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import java.util.HashMap;
import java.util.Map;
/**
* @author Bonbons
* @version 1.0
* 演示我们死信队列的消费者2号,就负责接收死信队列的消息
*/
public class Consumer02 {
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws Exception{
//获取信道
Channel channel = RabbitMqUtils.getChannel();
//最普通的消费者
System.out.println("等待接收消息......");
channel.basicConsume(DEAD_QUEUE, true, (consumersTag, message) -> {
System.out.println("Consumer02接收到的消息: " + new String(message.getBody(), "UTF-8"));
}, consumersTag -> {});
}
}
3、演示第二种情况:队列达到最大长度
重点: 当我们想要修改已经存在队列、交换机的属性时,需要将已经存在的删除,然后运行代码重新创建
4、第三种情况:消息被拒并不能返回队列
package com.atguigu.rabbitmq.eight;
import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import java.util.HashMap;
import java.util.Map;
/**
* @author Bonbons
* @version 1.0
* 演示我们死信队列的消费者1号
*/
public class Consumer01 {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static final String DEAD_EXCHANGE = "dead_exchange";
public static final String NORMAL_QUEUE = "normal_queue";
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws Exception{
//获取信道
Channel channel = RabbitMqUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//正常队列绑定死信队列信息
Map<String, Object> params = new HashMap<>();
//正常队列设置死信交换机
params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
//正常队列设置死信 routingKey
params.put("x-dead-letter-routing-key", "lisi");
//设置正常队列的长度限制
// params.put("x-max-length", 6);
//声明队列 [此处用到了第四个参数,实现将普通队列与死信交换机关联起来]
channel.queueDeclare(NORMAL_QUEUE, false, false, false, params);
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//将我们的队列和交换机绑定到一起
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
System.out.println("等待接收消息......");
//此处要开启手动应答,因为如果设置为true(自动应答)就不存在拒绝消息这一说了
channel.basicConsume(NORMAL_QUEUE, false, (consumersTag, message) -> {
String msg = new String(message.getBody(), "UTF-8");
if(msg.equals("info5")){
System.out.println("Consumer01接收到的消息: " + msg + ": 此消息是被C1拒绝的");
//拒绝消息,并设置不重新添加到队列 >> 变成死信消息
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
}else{
System.out.println("Consumer01接收到的消息: " + msg);
}
}, consumersTag -> {});
}
}
这些场景都有一个特点,需要在某个事件发生之后或者之前的指定时间点完成某一项任务,如:发生订单生成事件,在十分钟之后检查该订单支付状态,然后将未支付的订单进行关闭;看起来似乎使用定时任务,一直轮询数据,每秒查一次,取出需要被处理的数据,然后处理不就完事了吗?如果数据量比较少,确实可以这样做,比如:对于“如果账单一周内未支付则进行自动结算”这样的需求,如果对于时间不是严格限制,而是宽松意义上的一周,那么每天晚上跑个定时任务检查一下所有未支付的账单,确实也是一个可行的方案。但对于数据量比较大,并且时效性较强的场景,如:“订单十分钟内未支付则关闭“,短期内未支付的订单数据可能会有很多,活动期间甚至会达到百万甚至千万级别,对这么庞大的数据量仍旧使用轮询的方式显然是不可取的,很可能在一秒内无法完成所有订单的检查,同时会给数据库带来很大压力,无法满足业务要求而且性能低下。【引出我们的延迟队列】
1、如何为消息设置 TTL 呢?
2、如何为队列设置 TTL 呢?
3、两者有什么区别呢?
1、创建项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.14version>
<relativePath/>
parent>
<groupId>com.atguigu.rabbitmqgroupId>
<artifactId>springboot-rabbitmqartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot-rabbitmqname>
<description>springboot-rabbitmqdescription>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbit-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
3、修改配置文件 【连接到我们的RabbitMQ】
spring.rabbitmq.host=8.130.95.101
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
4、添加 Swagger 配置类
package com.atguigu.rabbitmq.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://atguigu.com",
"[email protected]"))
.build();
}
}
1️⃣ 代码架构图
2️⃣ 配置类代码 【定义队列、交换机部分】
package com.atguigu.rabbitmq.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 java.util.HashMap;
import java.util.Map;
/**
* @author Bonbons
* @version 1.0
* 延迟队列演示配置类
*/
@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";
//声明我们的两个交换机
@Bean("xExchange")
public DirectExchange xExchange(){
return new DirectExchange(X_EXCHANGE);
}
@Bean("yExchange")
public DirectExchange yExchange(){
return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
}
//声明我们的两个普通队列
@Bean("queueA")
public Queue queueA(){
Map<String, Object> arguments = new HashMap<>(3);//3代表初始map的长度
//设置死信交换机
arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
//设置routingKey
arguments.put("x-dead-letter-routing-key", "YD");
//设置过期时间
arguments.put("x-message-ttl", 10000);
return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
}
@Bean("queueB")
public Queue queueB(){
Map<String, Object> arguments = new HashMap<>(3);//3代表初始map的长度
//设置死信交换机
arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
//设置routingKey
arguments.put("x-dead-letter-routing-key", "YD");
//设置过期时间
arguments.put("x-message-ttl", 40000);
return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
}
//声明我们的死信队列
@Bean("queueD")
public Queue queueD(){
// return new Queue(DEAD_LETTER_QUEUE); //两种创建队列的方式均可
return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
}
//将我们的两个普通队列绑定到普通交换机X上
@Bean
public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueA).to(xExchange).with("XA");
}
@Bean
public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueB).to(xExchange).with("XB");
}
//将我们的死信队列绑定到死信交换机上
@Bean
public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
@Qualifier("yExchange") DirectExchange yExchange){
return BindingBuilder.bind(queueD).to(yExchange).with("YD");
}
3️⃣ 消息生产者 【编写一个处理请求的控制器】
package com.atguigu.rabbitmq.controller;
import com.atguigu.rabbitmq.config.DelayedQueueConfig;
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;
/**
* @author Bonbons
* @version 1.0
*/
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {
@Autowired
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);
}
}
4️⃣ 消息消费者 【一个组件类里定义Rabbit监听器】
package com.atguigu.rabbitmq.consumer;
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;
/**
* @author Bonbons
* @version 1.0
*/
@Slf4j
@Component
public class DeadLetterQueueConsumer {
//接收消息
@RabbitListener(queues = "QD")
public void receiveMsg(Message message, Channel channel) throws Exception{
String msg = new String(message.getBody());
log.info("当前时间: {}, 收到死信队列的消息: {}", new Date().toString(), msg);
}
}
1️⃣ 代码架构图
//优化我们的延迟队列,再设置一个普通队列,不为其设置过期时间,让它做公共队列
public static final String QUEUE_C = "QC";
//声明这个队列
@Bean
public Queue queueC(){
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");
return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
}
//绑定到我们的交换机X
@Bean
public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
@Qualifier("xExchange") DirectExchange xExchange){
return BindingBuilder.bind(queueC).to(xExchange).with("XC");
}
3️⃣ 消息生产者代码 【增加了一个新的请求映射】
//发送我们优化后的延迟队列的消息
@GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
public void sendMsg(@PathVariable String message, @PathVariable String ttlTime){
log.info("当前时间:{}, 发送一条时长为{}毫秒的消息给TTL队列QC: {}", new Date().toString(), ttlTime, message);
//使用springboot为我们提供的rabbitTemplate来发送延迟消息
rabbitTemplate.convertAndSend("X", "XC", message, msg -> {
//设置过期时间
msg.getMessageProperties().setExpiration(ttlTime);
return msg;
});
}
1️⃣ 安装延迟队列插件 【在我们的第一篇配置文章资源包里有】
在官网上下载 https://www.rabbitmq.com/community-plugins.html,下载 rabbitmq_delayed_message_exchange 插件,然后解压放置到 RabbitMQ 的插件目录
进入 RabbitMQ 的安装目录下的 plgins 目录,执行下面命令让该插件生效,然后重启 RabbitMQ
通过 RabbitMQ 的 web 控制台可以看到添加插件前后交换机处的变换
2️⃣ 代码架构图
3️⃣ 配置类代码
在我们自定义的交换机中,这是一种新的交换类型,该类型消息支持延迟投递机制 消息传递后并不会立即投递到目标队列中,而是存储在 mnesia(一个分布式数据系统)表中,当达到投递时间时,才投递到目标队列中
新建的配置类
package com.atguigu.rabbitmq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* @author Bonbons
* @version 1.0
*/
@Configuration
public class DelayedQueueConfig {
//定义我们的交换机、队列、RoutingKey
public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
public static final String DELAYED_QUEUE_NAME = "delayed.queue";
public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
//声明交换机
@Bean("delayedExchange")
public CustomExchange delayedExchange(){
Map<String, Object> arguments = new HashMap<>();
//延迟类型
arguments.put("x-delayed-type", "direct");
/**
* 1、交换机的名字
* 2、交换机的类型 [指明是延迟消息]
* 3、是否需要持久化
* 4、是否开启自动删除
* 5、其他参数
*/
return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message",
true, false, arguments);
}
//声明队列
@Bean
public Queue delayedQueue(){
return new Queue(DELAYED_QUEUE_NAME);
}
//将交换机与队列进行绑定
@Bean
public Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue") Queue delayedQueue,
@Qualifier("delayedExchange") CustomExchange delayedExchange){
return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
}
}
4️⃣ 消息生产者
//发送我们使用插件的延迟队列消息
@GetMapping("/sendDelayedMsg/{message}/{delayedTime}")
public void sendMsg(@PathVariable String message, @PathVariable Integer delayedTime){
log.info("当前时间:{}, 发送一条时长为{}毫秒的消息给延迟队列delayed.queue: {}", new Date().toString(), delayedTime, message);
rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME, DelayedQueueConfig.DELAYED_ROUTING_KEY, message, msg -> {
//设置延迟消息
msg.getMessageProperties().setDelay(delayedTime);
return msg;
});
}
5️⃣ 消息消费者
新的监听器
package com.atguigu.rabbitmq.consumer;
import com.atguigu.rabbitmq.config.DelayedQueueConfig;
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;
/**
* @author Bonbons
* @version 1.0
*/
@Slf4j
@Component
public class DelayQueueConsumer {
//设置监听器
@RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
public void receiveDelayQueue(Message message){
String msg = new String(message.getBody());
log.info("当前时间: {}, 收到延迟队列的消息: {}", new Date().toString(), msg);
}
}
6️⃣ 总结