SpringBoot商城秒杀系统

  学习自:地址

1.主要流程

1.1数据库:

  

 

 

 1.2 环境

  window下:Zookeeper,Redis,rabbitmq-server。jdk1.8以上。

1.3 介绍

  这里只做秒杀部分功能,其他功能不会涉及。项目运行后可访问秒杀商品页面

 

 

 当用户没登陆,点击详情会跳转到登陆页面。

 

 

用户登陆后可以查看商品的详情并进行抢购。

 

 

 

 注意,用户对于一件商品只能抢购一次,进行第二次抢购时会被拒绝。当用户抢购成功时会异步发送一封邮件给用户。

 

 

 主要逻辑就是以上。接下来看代码

1.4 项目结构,api封装一些枚举和返回值,model主要是实体类和sql映射文件,service实现业务逻辑代码。

  

 

 

 

 

 

 

 

 

 

 

 

 

1.5 显示秒杀商品到页面以及用户的操作使用的还是MVC模式,不细讲。主要看如实现高并发下的秒杀。

要细述的话,东西太多,如果想深入了解,可点击上面的链接。

基本的秒杀逻辑如下,判断用户是否已经抢购过该商品,如果没有则查询待秒杀商品详情,判断该商品是否可以别秒杀,判断依据为库存是否足够

如果符合条件,则该商品库存减1,接着,再一次判断扣减是否成功,如果扣减成功则生成秒杀成功的订单,同时通知用户秒杀成功的信息。

 public Boolean killItem(Integer killId, Integer userId) throws Exception {
        Boolean result=false;

        //TODO:判断当前用户是否已经抢购过当前商品
        if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
            //TODO:查询待秒杀商品详情
            ItemKill itemKill=itemKillMapper.selectById(killId);

            //TODO:判断是否可以被秒杀canKill=1?
            if (itemKill!=null && 1==itemKill.getCanKill() ){
                //TODO:扣减库存-减一
                int res=itemKillMapper.updateKillItem(killId);

                //TODO:扣减是否成功?是-生成秒杀成功的订单,同时通知用户秒杀成功的消息
                if (res>0){
                    commonRecordKillSuccessInfo(itemKill,userId);

                    result=true;
                }
            }
        }else{
            throw new Exception("您已经抢购过该商品了!");
        }
        return result;
    }

 

代码优化1:使用redis的分布式锁,使用当前秒杀商品的id和当前用户的id组成一个key,使用StringBuffer拼接,使用雪花算法生成一个value,存进redis中。

Boolean cacheRes=valueOperations.setIfAbsent(key,value);当前锁唯一,一次只能放一个用户进行操作,操作结束后,释放该锁。
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 商品秒杀核心业务逻辑的处理-redis的分布式锁
     * @param killId
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean killItemV3(Integer killId, Integer userId) throws Exception {
        Boolean result=false;

        if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){

            //TODO:借助Redis的原子操作实现分布式锁-对共享操作-资源进行控制
            ValueOperations valueOperations=stringRedisTemplate.opsForValue();
            final String key=new StringBuffer().append(killId).append(userId).append("-RedisLock").toString();
            final String value=RandomUtil.generateOrderCode();
            Boolean cacheRes=valueOperations.setIfAbsent(key,value); //luna脚本提供“分布式锁服务”,就可以写在一起
            //TOOD:redis部署节点宕机了
            if (cacheRes){
                stringRedisTemplate.expire(key,30, TimeUnit.SECONDS);

                try {
                    ItemKill itemKill=itemKillMapper.selectByIdV2(killId);
                    if (itemKill!=null && 1==itemKill.getCanKill() && itemKill.getTotal()>0){
                        int res=itemKillMapper.updateKillItemV2(killId);
                        if (res>0){
                            commonRecordKillSuccessInfo(itemKill,userId);

                            result=true;
                        }
                    }
                }catch (Exception e){
                    throw new Exception("还没到抢购日期、已过了抢购时间或已被抢购完毕!");
                }finally {
                    if (value.equals(valueOperations.get(key).toString())){
                        stringRedisTemplate.delete(key);
                    }
                }
            }
        }else{
            throw new Exception("Redis-您已经抢购过该商品了!");
        }
        return result;
    }

 

代码优化2:将 Boolean cacheRes=lock.tryLock(30,10,TimeUnit.SECONDS); 每隔30秒判断当前用户是否超时写在了锁外面,不会因为一次卡顿而影响整个程序。

    @Autowired
    private RedissonClient redissonClient;

    /**
     * 商品秒杀核心业务逻辑的处理-redisson的分布式锁
     * @param killId
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean killItemV4(Integer killId, Integer userId) throws Exception {
        Boolean result=false;

        final String lockKey=new StringBuffer().append(killId).append(userId).append("-RedissonLock").toString();
        RLock lock=redissonClient.getLock(lockKey);

        try {
            Boolean cacheRes=lock.tryLock(30,10,TimeUnit.SECONDS);
            if (cacheRes){
                //TODO:核心业务逻辑的处理
                if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
                    ItemKill itemKill=itemKillMapper.selectByIdV2(killId);
                    if (itemKill!=null && 1==itemKill.getCanKill() && itemKill.getTotal()>0){
                        int res=itemKillMapper.updateKillItemV2(killId);
                        if (res>0){
                            commonRecordKillSuccessInfo(itemKill,userId);

                            result=true;
                        }
                    }
                }else{
                    throw new Exception("redisson-您已经抢购过该商品了!");
                }
            }
        }finally {
            lock.unlock();
            //lock.forceUnlock();
        }
        return result;
    }

 

代码优化3:

    @Autowired
    private CuratorFramework curatorFramework;

    private static final String pathPrefix="/kill/zkLock/";

    /**
     * 商品秒杀核心业务逻辑的处理-基于ZooKeeper的分布式锁
     * @param killId
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean killItemV5(Integer killId, Integer userId) throws Exception {
        Boolean result=false;

        InterProcessMutex mutex=new InterProcessMutex(curatorFramework,pathPrefix+killId+userId+"-lock");
        try {
            if (mutex.acquire(10L,TimeUnit.SECONDS)){

                //TODO:核心业务逻辑
                if (itemKillSuccessMapper.countByKillUserId(killId,userId) <= 0){
                    ItemKill itemKill=itemKillMapper.selectByIdV2(killId);
                    if (itemKill!=null && 1==itemKill.getCanKill() && itemKill.getTotal()>0){
                        int res=itemKillMapper.updateKillItemV2(killId);
                        if (res>0){
                            commonRecordKillSuccessInfo(itemKill,userId);
                            result=true;
                        }
                    }
                }else{
                    throw new Exception("zookeeper-您已经抢购过该商品了!");
                }
            }
        }catch (Exception e){
            throw new Exception("还没到抢购日期、已过了抢购时间或已被抢购完毕!");
        }finally {
            if (mutex!=null){
                mutex.release();
            }
        }
        return result;
    }

 

 

 

 

 

 

 

 

 

 

posted @ 2019-11-18 22:10  念_响  阅读(2058)  评论(0编辑  收藏  举报