建设网站条件,线上广告宣传方式有哪些,郑州做商城网站,网站开发工具 知乎SpringCloud 大型系列课程正在制作中#xff0c;欢迎大家关注与提意见。 程序员每天的CV 与 板砖#xff0c;也要知其所以然#xff0c;本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发 1 项目准备
SpringBoot 雪花算法生成商品订单… 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_exchange2 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/*** 定义队列**/Beanpublic Queue directQueue(){return new Queue(DIRECT_QUEUE,true);}/*** 定义延迟交换机* args:根据该参数进行灵活路由设置为“direct”意味着该插件具有与直连交换机具有相同的路由行为* 交换机类型为 x-delayed-message**/Beanpublic CustomExchange delayedExchange(){MapString, Object args new HashMapString, Object();args.put(x-delayed-type, direct);return new CustomExchange(DELAYED_EXCHANGE, x-delayed-message, true, false, args);}/*** 队列和延迟交换机绑定**/Beanpublic 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 {Autowiredprivate 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 {RabbitHandlerpublic void process(String message){log.info(DelayedMQReciever接收到的消息是 message);}
}
2.4 测试
Api(tags 订单测试模块)
RestController()
RequestMapping(/test/orders)
Slf4j
public class OrderTestController {AutowiredTestDelayedMQSender 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】 文章转载自: http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.sknbb.cn.gov.cn.sknbb.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.rlpmy.cn.gov.cn.rlpmy.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.frmmp.cn.gov.cn.frmmp.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn