RabbitMQ之死信队列

举报
哥的时代 发表于 2023/12/25 23:06:21 2023/12/25
【摘要】 一、死信的概念先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到 queue 里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的==原因导致 queue 中的某些消息无法被消费==,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。应用场景...

一、死信的概念

先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到 queue 里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的==原因导致 queue 中的某些消息无法被消费==,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。

应用场景:为了保证订单业务的消息数据不丢失,需要使用到 RabbitMQ 的死信队列机制,当消息消费发生异常时,将消息投入死信队列中.还有比如说: 用户在商城下单成功并点击去支付后在指定时间未支付时自动失效。

二、死信的来源

  • 消息 TTL 过期

  • 队列达到最大长度(队列满了,无法再添加数据到 mq 中)

  • 消息被拒绝(basic.reject 或 basic.nack)并且 requeue=false.

三、实战

image.png

1、消息 TTL 过期

生产者代码:

public class Producer {
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
 	public static void main(String[] argv) throws Exception {
 		try (Channel channel = RabbitMqUtils.getChannel()) {
 			channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 			//设置消息的 TTL 时间
 			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());
 				System.out.println("生产者发送消息:"+message);
 			}
 		}
 	}
}

消费者 C1 代码(==启动之后关闭该消费者 模拟其接收不到消息==)

public class Consumer01 {
 	//普通交换机名称
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
 	//死信交换机名称
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		//声明死信和普通交换机 类型为 direct
 		channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		//声明死信队列
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		//死信队列绑定死信交换机与 routingkey
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		//正常队列绑定死信队列信息
 		Map<String, Object> params = new HashMap<>();
 		//正常队列设置死信交换机 参数 key 是固定值
 		params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
 		//正常队列设置死信 routing-key 参数 key 是固定值
 		params.put("x-dead-letter-routing-key", "lisi");
 
 		String normalQueue = "normal-queue";
 		channel.queueDeclare(normalQueue, false, false, false, params);
 		channel.queueBind(normalQueue, NORMAL_EXCHANGE, "zhangsan");
 		System.out.println("等待接收消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			System.out.println("Consumer01 接收到消息"+message);
 		};
 		channel.basicConsume(normalQueue, true, deliverCallback, consumerTag -> {
 		});
 	}
}

image.png

消费者 C2 代码(==以上步骤完成后 启动 C2 消费者 它消费死信队列里面的消息==)

public class Consumer02 {
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		System.out.println("等待接收死信队列消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			System.out.println("Consumer02 接收死信队列的消息" + message);
	 	};
 		channel.basicConsume(deadQueue, true, deliverCallback, consumerTag -> {
 		});
 	}
}

image.png

2、队列达到最大长度

消息生产者代码去掉 TTL 属性

public class Producer {
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
	public static void main(String[] argv) throws Exception {
 		try (Channel channel = RabbitMqUtils.getChannel()) {
 			channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 			//该信息是用作演示队列个数限制
 			for (int i = 1; i <11 ; i++) {
 				String message="info"+i;
 				channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",null, message.getBytes());
 				System.out.println("生产者发送消息:"+message);
 			}
 		}
 	}
}

C1 消费者修改以下代码(==启动之后关闭该消费者 模拟其接收不到消息==)

public class Consumer01 {
 	//普通交换机名称
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
 	//死信交换机名称
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		//声明死信和普通交换机 类型为 direct
 		channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		//声明死信队列
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		//死信队列绑定死信交换机与 routingkey
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		//正常队列绑定死信队列信息
 		Map<String, Object> params = new HashMap<>();
 		//正常队列设置死信交换机 参数 key 是固定值
 		params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
 		//正常队列设置死信 routing-key 参数 key 是固定值
 		params.put("x-dead-letter-routing-key", "lisi");
 		//设置正常队列长度限制
 		params.put("x-max-length",6);
 		
 		String normalQueue = "normal-queue";
 		channel.queueDeclare(normalQueue, false, false, false, params);
 		channel.queueBind(normalQueue, NORMAL_EXCHANGE, "zhangsan");
 		System.out.println("等待接收消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			System.out.println("Consumer01 接收到消息"+message);
 		};
 		channel.basicConsume(normalQueue, true, deliverCallback, consumerTag -> {
 		});
 	}
}

==注意此时需要把原先队列删除 因为参数改变了==
C2 消费者代码

public class Consumer02 {
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		System.out.println("等待接收死信队列消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			System.out.println("Consumer02 接收死信队列的消息" + message);
	 	};
 		channel.basicConsume(deadQueue, true, deliverCallback, consumerTag -> {
 		});
 	}
}

image.png

3、消息被拒

消息生产者代码

public class Producer {
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
	public static void main(String[] argv) throws Exception {
 		try (Channel channel = RabbitMqUtils.getChannel()) {
 			channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 			//该信息是用作演示队列个数限制
 			for (int i = 1; i <11 ; i++) {
 				String message="info"+i;
 				channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",null, message.getBytes());
 				System.out.println("生产者发送消息:"+message);
 			}
 		}
 	}
}

C1 消费者代码(==启动之后关闭该消费者 模拟其接收不到消息==)

public class Consumer01 {
 	//普通交换机名称
 	private static final String NORMAL_EXCHANGE = "normal_exchange";
 	//死信交换机名称
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		//声明死信和普通交换机 类型为 direct
 		channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		//声明死信队列
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		//死信队列绑定死信交换机与 routingkey
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		//正常队列绑定死信队列信息
 		Map<String, Object> params = new HashMap<>();
 		//正常队列设置死信交换机 参数 key 是固定值
 		params.put("x-dead-letter-exchange", DEAD_EXCHANGE);
 		//正常队列设置死信 routing-key 参数 key 是固定值
 		params.put("x-dead-letter-routing-key", "lisi");
 		String normalQueue = "normal-queue";
 		channel.queueDeclare(normalQueue, false, false, false, params);
 		channel.queueBind(normalQueue, NORMAL_EXCHANGE, "zhangsan");
 		System.out.println("等待接收消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			if(message.equals("info5")){
 				System.out.println("Consumer01 接收到消息" + message + "并拒绝签收该消息");
 				//requeue 设置为 false 代表拒绝重新入队 该队列如果配置了死信交换机将发送到死信队列中
 				channel.basicReject(delivery.getEnvelope().getDeliveryTag(), false);
 			}else {
 				System.out.println("Consumer01 接收到消息"+message);
 				channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
 			}
 		};
 		boolean autoAck = false;
 		channel.basicConsume(normalQueue, autoAck, deliverCallback, consumerTag -> {
 		});
 	}
}

image.png

C2 消费者代码

public class Consumer02 {
 	private static final String DEAD_EXCHANGE = "dead_exchange";
 	public static void main(String[] argv) throws Exception {
 		Channel channel = RabbitUtils.getChannel();
 		channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
 		String deadQueue = "dead-queue";
 		channel.queueDeclare(deadQueue, false, false, false, null);
 		channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");
 		System.out.println("等待接收死信队列消息.....");
 		DeliverCallback deliverCallback = (consumerTag, delivery) -> {
 			String message = new String(delivery.getBody(), "UTF-8");
 			System.out.println("Consumer02 接收死信队列的消息" + message);
	 	};
 		channel.basicConsume(deadQueue, true, deliverCallback, consumerTag -> {
 		});
 	}
}

启动消费者 1 然后再启动消费者 2

image.png


总结

以上就是RabbitMQ之死信队列的相关知识点,希望对你有所帮助。
积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。