SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。 程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发

1 项目准备

SpringBoot 雪花算法生成商品订单号【SpringBoot系列13】本文章 基于这个项目来开发

本文章是系列文章 ,每节文章都有对应的代码,每节的源码都是在上一节的基础上配置而来,对应的视频讲解课程正在火速录制中。

订单系统,用户下单,即要保存即时性,也要保证流畅性,同时还要防止超卖,本文章是基于 RabbitMQ 消息队列 + Redis 实现的下单,当然后续还会的秒杀系统设计 以及后续的微服务以及熔断控制等等

如下图所示是本项目实现的一个下单流程的主要过程:

本文章实现的是 当用户下单成功后10分钟内没有支付时,使用 RabbitMQ 延时消息队列取消订单,并恢复商品的库存。

1 RabbitMQ 延时插件的加载

插件地址:

https://www.rabbitmq.com/community-plugins.html

下载对应的版本到电脑本地,打开终端 将下载的压缩包 移动到plugins目录下

docker cp /Users/androidlongs/Downloads/rabbitmq_delayed_message_exchange-3.9.0.ez rabbitmq:/plugins

然后进入容器,我这里使用容器名字 也可以用容器id进入

docker exec -it rabbitmq /bin/bash

移动到plugins目录下

cd plugins

查看是否上传成功

ls

也可以使用

rabbitmq-plugins list

然后启用延时插件

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

2 SpringBoot RabbitMQ 延时消息

2.1 创建消息队列

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 TestDelayedMessageConfig {

public static final String DIRECT_QUEUE = "test.queue.direct";//队列

public static final String DELAYED_EXCHANGE = "test.exchange.delayed";//延迟交换机

public static final String ROUTING_KEY = "test.routingkey.bind";//绑定的routing-key

/**

* 定义队列

**/

@Bean

public Queue directQueue(){

return new Queue(DIRECT_QUEUE,true);

}

/**

* 定义延迟交换机

* args:根据该参数进行灵活路由,设置为“direct”,意味着该插件具有与直连交换机具有相同的路由行为

* 交换机类型为 x-delayed-message

**/

@Bean

public CustomExchange delayedExchange(){

Map args = new HashMap();

args.put("x-delayed-type", "direct");

return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);

}

/**

* 队列和延迟交换机绑定

**/

@Bean

public Binding orderBinding() {

return BindingBuilder.bind(directQueue()).to(delayedExchange()).with(ROUTING_KEY).noargs();

}

}

CustomExchange 是自定义交换机,一般是配合插件来使用的。

2.2 创建延时消息 生产者

import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

@Slf4j

public class TestDelayedMQSender {

@Autowired

private RabbitTemplate rabbitTemplate;

public void testSsend(String msg, Integer delayTime) {

log.info("测试发送延时消息 {} : {}", delayTime, msg);

//将消息携带路由键值

rabbitTemplate.convertAndSend(

TestDelayedMessageConfig.DELAYED_EXCHANGE,//交换机名称

TestDelayedMessageConfig.ROUTING_KEY,//路由 key

msg,

message -> {

//设置延时的时间 单位毫秒

message.getMessageProperties().setDelay(delayTime);

return message;

});

}

}

2.3 创建延时消息 消费者

import lombok.extern.log4j.Log4j;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;

import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Component;

@Component

@Log4j

@RabbitListener(queues = TestDelayedMessageConfig.DIRECT_QUEUE)//监听队列名称

public class TestDelayedMQReciever {

@RabbitHandler

public void process(String message){

log.info("DelayedMQReciever接收到的消息是:"+ message);

}

}

2.4 测试

@Api(tags = "订单测试模块")

@RestController()

@RequestMapping("/test/orders")

@Slf4j

public class OrderTestController {

@Autowired

TestDelayedMQSender delayedMQSender;

@GetMapping("/send/delay")

public R createPreOrder(@RequestParam(required = false,defaultValue = "10000") Integer delayTime) {

delayedMQSender.testSsend("测试延时消息了",delayTime);

return R.ok();

}

}

3 延时取消订单

实现思路是用户下单,创建订单成功后,发一个带有订单信息的延时消息,然后当到达指定时间后,判断一下订单是否未支付。

如果已支付 就不做任何处理,如果未支付,就取消订单,取消订单后

更新redis 缓存更新 ES 订单信息发送取消订单的通知恢复商品库存…

项目源码在这里 :https://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-11-snow_flake 有兴趣可以关注一下公众号:biglead

创建SpringBoot基础项目SpringBoot项目集成mybatisSpringBoot 集成 Druid 数据源【SpringBoot系列3】SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】SpringBoot MyBatis-Plus 集成 【SpringBoot系列5】SpringBoot mybatis-plus-generator 代码生成器 【SpringBoot系列6】SpringBoot MyBatis-Plus 分页查询 【SpringBoot系列7】SpringBoot 集成Redis缓存 以及实现基本的数据缓存【SpringBoot系列8】SpringBoot 整合 Spring Security 实现安全认证【SpringBoot系列9】SpringBoot Security认证 Redis缓存用户信息【SpringBoot系列10】SpringBoot 整合 RabbitMQ 消息队列【SpringBoot系列11】SpringBoot 结合RabbitMQ与Redis实现商品的并发下单【SpringBoot系列12】SpringBoot 雪花算法生成商品订单号【SpringBoot系列13】

精彩链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。