并发扣减库存操作

商城里创建订单对库存的脚校验,怎么防止多卖超卖?
这里我分别使用了synchronized,rabbitmq,和数据库判断,数据库锁,

首先说rabbitMq吧!先看代码首先我声明了一个名为myQueue3的队列

//声明队列
 @Bean
    public Queue queue1(){
        return new Queue("myQueue3");
    }
客户端看到声明成功

建立消费者和生产者
getcort1 模拟了创建订单操作 为消息的提供者

    @RequestMapping(value = "/getcort1")
    public String send() {
        String context = "order_ok " + new Date();
        System.out.println("Sender : " + context);
// 调用 发送消息的方法
        //convertAndSend 发送消息返回为空
        //convertSendAndReceive发送消息 可接收回复
        Object a = rabbitTemplate.convertSendAndReceive("myQueue3", context);
        System.out.println("收到"+a);
        return "创建订单成功";
    }

test为消费者,并且为生产者反馈了扣减库存的结果

@RabbitListener(queues ="myQueue3")
    @RabbitHandler
    @ApiOperation(value = "压力测试", notes = "压力测试")
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public  String test(String message) throws Exception {
        System.out.println("收到了消息:"+message);
//查询库存
        HotelProduct hotelProduct = hotelProductMapper.selectByPrimaryKey(1);
//库存充足扣减,不足返回
        if (hotelProduct.getQuantity()>0){

            hotelProduct.setQuantity(hotelProduct.getQuantity() - 1);
            System.out.println("剩余库存:"+hotelProductMapper.selectByPrimaryKey(1).getQuantity());
            hotelProductMapper.updateByPrimaryKeySelective(hotelProduct);
            return "ok";
        } else {
            System.out.println("库存不足");
            return "no";
        }

    }

测试一下结果

数据库-将库存设为100

将发送1000个请求,20的并发,相当于20个客户端同时发送请求,每个客户端发送50个请求

可以看到1000个请求全部成功,

测试结果

是没有出现超卖的情况的

打印结果

消息队列扣减库存比平时还是有一个好处的,我们将数据库库存删掉,制造一个空指针

数据库

虽然扣减库存报错但是不影响订单的创建[图片上传中...(捕获.PNG-bcae0a-1610510811626-0)]

swagger
报错

将库存补上以后,扣减库存

扣减成功

synchronized关键字

@ApiOperation(value = "压力测试", notes = "压力测试")
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public synchronized String test() throws Exception {
        System.out.println("创建订单");
        HotelProduct hotelProduct = hotelProductMapper.selectByPrimaryKey(1);
        if (hotelProduct.getQuantity()>0){
            hotelProduct.setQuantity(hotelProduct.getQuantity() - 1);
            System.out.println("剩余库存:"+(hotelProductMapper.selectByPrimaryKey(1).getQuantity()));
            hotelProductMapper.updateByPrimaryKeySelective(hotelProduct);
            return "ok";
        } else {
            System.out.println("库存不足");
            return "no";
        }
}

相同条件测试一下

数据库逻辑操作 sql

<update id="quantity">
        UPDATE hotel_product
SET quantity = quantity - 1
WHERE
    id = 1
    AND quantity - 1 &gt;= 0
    </update>
@ApiOperation(value = "压力测试", notes = "压力测试")
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public String test() throws Exception {
        System.out.println("创建订单");
        if (productService.quantity() > 0) {
            System.out.println("成功,剩余库存:" + hotelProductMapper.selectByPrimaryKey(1).getQuantity());
            return "ok";
        } else {
            System.out.println("库存不足");
            return "no";
        }

    }
捕获.PNG

数据库悲观锁

select * from hotel_product where id=1 for update;

补充一下,面试的时候受到前辈指点,后面去了解一下,大家都知道redis本来就是单线程的,所以我们可以考虑把“检查库存-扣减库存-返回结果”的动作写成lua脚本去调用,一个调用就能原子化地操作库存了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先来一下小概念 CAP定理 任何一个分布式系统都无法同时满足一致性(Consistency)、可用性(Availa...
    Bill_Li_GB阅读 3,581评论 0 3
  • 面试的问题都是大同小异的,每轮面试结束后最好对问题做一个复盘的记录总结,不断迭代出最全面最佳的回答。 一定得自信,...
    技术灭霸阅读 1,267评论 1 2
  • 背景 在电商系统中买商品过程,先加入购物车,然后选中商品,点击结算,即会进入待支付状态,后续支付。过程需要检验库存...
    地藏Kelvin阅读 3,623评论 4 14
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,752评论 0 5
  • 昨天,在回家的路上,坐在车里悠哉悠哉地看着三毛的《撒哈拉沙漠的故事》,我被里面的内容深深吸引住了,尽管上学时...
    夜阑晓语阅读 3,831评论 2 9