Java 实现秒杀-mq消息队列

消息队列(MQ)本身的作用、如下图:
在这里插入图片描述
在这里插入图片描述
秒杀系统中,消息队列(MQ)的主要作用是削峰、解耦、异步处理、增强系统的并发处理能力

**解耦:**系统组件之间通过消息队列进行解耦,我们只需要依赖于mq,避免了各个子系统间的强依赖问题,提高系统的可扩展性和可维护性。
在这里插入图片描述
**异步处理:**消息队列可以实现异步处理,用户提交订单后,可以直接返回结果,无需等待秒杀后续流程完成。这样能避免总耗时比较长,从而提高用户体验。
在这里插入图片描述
**削峰:**突然出现的请求峰值,导致系统不稳定的问题。使用mq后,由于消费者的消费能力有限,会按照自己的节奏来消费消息,多的请求不处理,保留在mq的队列中,不会对系统的稳定性造成影响,能够起到消峰的作用。
在这里插入图片描述

MQ消息队列存在的主要问题包括消息丢失、‌消息顺序问题、‌消息堆积、‌重复消费、‌延时及过期失效问题。‌

消息丢失:‌
消息在生产、‌传输、‌消费过程中可能会因为各种原因丢失
1、生产者发送失败(网络原因)
2、MQ服务器处理失败(mq服务器持久化时,磁盘出现异常)

java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";//WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult execute(@PathVariable("seckillId")Long seckillId,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值