• RabbitMQ之死信队列


    • 用户下单,调用订单服务,然后订单服务调用派单系统通知外卖人员送单,这时候订单系统与派单系统 采用 MQ异步通讯。
    • 在定义业务队列时可以考虑指定一个 死信交换机,并绑定一个死信队列。当消息变成死信时,该消息就会被发送到该死信队列上,这样方便我们查看消息失败的原因。
    • DLX,全称为Dead-Letter-Exchange,死信交换器。消息在一个队列中变成死信(Dead Letter)之后,被重新发送到一个特殊的交换器(DLX)中,同时,绑定DLX的队列就称为“死信队列”。

    以下几种情况导致消息变为死信:

    1. 消息被拒绝(Basic.Reject/Basic.Nack),并且设置requeue参数为false;
    2. 消息过期;
    3. 队列达到最大长度。

            对于RabbitMQ 来说,DLX 是一个非常有用的特性。它可以处理异常情况下,消息不能够被消费者正确消费(消费者调用了Basic.Nack 或者Basic.Reject)而被置入死信队列中的情况,后续分析程序可以通过消费这个死信队列中的内容来分析当时所遇到的异常情况,进而可以改善和优化系统。

    1、原生API案例

    1. package com.lagou.rabbitmq.demo;
    2. import com.rabbitmq.client.Channel;
    3. import com.rabbitmq.client.Connection;
    4. import com.rabbitmq.client.ConnectionFactory;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. public class DeadProducer {
    8. public static void main(String[] args) throws Exception {
    9. ConnectionFactory factory = new ConnectionFactory();
    10. factory.setUri("amqp://root:123456@192.168.80.121:5672/%2f");
    11. Connection connection = factory.newConnection();
    12. Channel channel = connection.createChannel();
    13. Map arguments = new HashMap<>();
    14. // 设置队列中消息TTL
    15. arguments.put("x-message-ttl", 10000);
    16. // 设置该队列所关联的死信交换器(当队列消息TTL到期后依然没有消费,则加入死信队列)
    17. arguments.put("x-dead-letter-exchange", "ex.dlx");
    18. // 设置该队列所关联的死信交换器的routingKey,如果没有特殊指定,使用原队列的routingKey
    19. arguments.put("x-dead-letter-routing-key", "key.dlx");
    20. // 定义一个正常业务的交换器
    21. channel.exchangeDeclare("ex.biz", "direct", true);
    22. // 定义一个正常队列
    23. channel.queueDeclare("queue.biz", true, false, false, arguments);
    24. // 正常队列绑定
    25. channel.queueBind("queue.biz", "ex.biz", "key.biz");
    26. // 定义一个死信交换器(也是一个普通的交换器)
    27. channel.exchangeDeclare("ex.dlx", "direct", true);
    28. // 定义一个死信队列
    29. channel.queueDeclare("queue.dlx", true, false, false, null);
    30. // 死信队列和死信交换器
    31. channel.queueBind("queue.dlx", "ex.dlx", "key.dlx");
    32. channel.basicPublish("ex.biz","key.biz",null,"orderid.8484494".getBytes());
    33. channel.close();
    34. connection.close();
    35. }
    36. }

    2、springboot案例

    2.1、pom.xml添加依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-amqpartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-webartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-testartifactId>
    13. <scope>testscope>
    14. <exclusions>
    15. <exclusion>
    16. <groupId>org.junit.vintagegroupId>
    17. <artifactId>junit-vintage-engineartifactId>
    18. exclusion>
    19. exclusions>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.amqpgroupId>
    23. <artifactId>spring-rabbit-testartifactId>
    24. <scope>testscope>
    25. dependency>
    26. dependencies>

    2.2、application.properties添加RabbitMQ连接信息

    1. spring.application.name=dlx
    2. spring.rabbitmq.host=node1
    3. spring.rabbitmq.virtual-host=/
    4. spring.rabbitmq.username=root
    5. spring.rabbitmq.password=123456
    6. spring.rabbitmq.port=5672

    2.3、主入口类:

    1. package com.lagou.rabbitmq.demo;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class RabbitmqDemo {
    6. public static void main(String[] args) {
    7. SpringApplication.run(RabbitmqDemo08.class, args);
    8. }
    9. }

    2.4、RabbitConfig类:

    1. package com.lagou.rabbitmq.demo.config;
    2. import org.springframework.amqp.core.*;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import java.util.HashMap;
    6. import java.util.Map;
    7. @Configuration
    8. public class RabbitConfig {
    9. @Bean
    10. public Queue queue() {
    11. Map props = new HashMap<>();
    12. // 消息的生存时间 10s
    13. props.put("x-message-ttl", 10000);
    14. // 设置该队列所关联的死信交换器(当队列消息TTL到期后依然没有消费,则加入死信队列)
    15. props.put("x-dead-letter-exchange", "ex.go.dlx");
    16. // 设置该队列所关联的死信交换器的routingKey,如果没有特殊指定,使用原队列的routingKey
    17. props.put("x-dead-letter-routing-key", "go.dlx");
    18. Queue queue = new Queue("q.go", true, false, false, props);
    19. return queue;
    20. }
    21. @Bean
    22. public Queue queueDlx() {
    23. Queue queue = new Queue("q.go.dlx", true, false, false);
    24. return queue;
    25. }
    26. @Bean
    27. public Exchange exchange() {
    28. DirectExchange exchange = new DirectExchange("ex.go", true, false, null);
    29. return exchange;
    30. }
    31. /**
    32. * 死信交换器
    33. *
    34. * @return
    35. */
    36. @Bean
    37. public Exchange exchangeDlx() {
    38. DirectExchange exchange = new DirectExchange("ex.go.dlx", true, false, null);
    39. return exchange;
    40. }
    41. @Bean
    42. public Binding binding() {
    43. return BindingBuilder.bind(queue()).to(exchange()).with("go").noargs();
    44. }
    45. /**
    46. * 死信交换器绑定死信队列
    47. *
    48. * @return
    49. */
    50. @Bean
    51. public Binding bindingDlx() {
    52. return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("go.dlx").noargs();
    53. }
    54. }

    2.5、GoController类:

    1. package com.lagou.rabbitmq.demo.controller;
    2. import org.springframework.amqp.core.AmqpTemplate;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class GoController {
    8. @Autowired
    9. private AmqpTemplate rabbitTemplate;
    10. @RequestMapping("/go")
    11. public String distributeGo() {
    12. rabbitTemplate.convertAndSend("ex.go", "go", "送单到石景山x小区,请在10秒内接受任务");
    13. return "任务已经下发,等待送单。。。";
    14. }
    15. @RequestMapping("/notgo")
    16. public String getAccumulatedTask() {
    17. String notGo = (String) rabbitTemplate.receiveAndConvert("q.go.dlx");
    18. return notGo;
    19. }
    20. }
  • 相关阅读:
    8. 过滤器的作用, 如何实现一个过滤器?
    1.nginx学习
    (Open Shortest Path First,OSPF)实验4
    VM-Linux基础操作命令
    ARM开发初级-STM32时钟系统以及如何正确使用HAL_Delay-学习笔记08
    你用 Excel 做的最酷的事情是什么?
    数据分析实战│时间序列预测
    01-Node中的系统模块:fs文件模块、path路径模块、正则、http模块
    PCL 4PCS点云粗配准
    计算机操作系统 (第四版 汤小丹)(持续学习中)
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/128130097