✅P293_商城业务-订单服务-延时队列定时关单模拟

gong_yz大约 2 分钟谷粒商城

基于事件模型的交换机设计

设计规范建议:

  • 交换机命名:业务+exchange;交换机为Topic
  • 路由键:事件.需要感知的业务(可以不写)
  • 队列命名:事件+想要监听服务名+queue
  • 绑定关系:事件.感知的业务(#)

创建队列、交换机,绑定关系

参照下图创建队列、交换机,绑定关系 ,模拟下单成功1分钟后,收到关闭订单的消息

容器中的 Binding、Queue、Exchange 都会自动创建(RabbitMQ没有的情况)

Binding、Queue只要存在,即使@Bean声明的属性发生变化也不会覆盖,需要手动在RabbitMQ管理页面进行调整

创建Queue、Exchange、Binding

cfmall-order/src/main/java/com/gyz/cfmall/order/config/MyMQConfig.java

package com.gyz.cfmall.order.config;

import com.gyz.cfmall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: gongyuzhuo
 * @since: 2024-02-06 14:33
 * @description: 容器中的Queue、Exchange、Binding 会自动创建(在RabbitMQ)不存在的情况下
 */
@Configuration
public class MyMQConfig {

    /**
     * TopicExchange
     * @return
     */
    @Bean
    public Exchange orderExchange() {
        //交换机名字、是否持久化、是否自动删除
        Exchange exchange = new TopicExchange("order-event-exchange", true, false);
        return exchange;
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>(16);
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);//毫秒
        Queue orderDelayQueue = new Queue("order.delay.queue", true, false, false, arguments);
        return orderDelayQueue;
    }

    /**
     * 普通队列
     * @return
     */
    @Bean
    public Queue orderReleaseorderQueue() {
        Queue orderReleaseQueue = new Queue("order.release.order.queue", true, false, false);
        return orderReleaseQueue;
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * 交换机和队列绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
}

监听关单事件

cfmall-order/src/main/java/com/gyz/cfmall/order/config/MyMQConfig.java

@RabbitListener(queues = "order.release.order.queue")
public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
    System.out.println("接收到过期的订单信息,准备关闭订单:" + orderEntity.getOrderSn());
    channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}

模拟订单完成

cfmall-order/src/main/java/com/gyz/cfmall/order/web/WebController.java

@GetMapping("/testCreateOrder")
@ResponseBody
public String createOrderTest() {
    OrderEntity orderEntity = new OrderEntity();
    orderEntity.setOrderSn(UUID.randomUUID().toString());
    rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderEntity);
    return "ok";
}

测试

前提:首页已经进行用户登录

访问地址:https://order.cfmall.com/testCreateOrderopen in new window

进行消息发送

观察RabbitMQ管理页面数据变化