温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中怎么实现一个商城秒杀系统

发布时间:2021-08-07 14:10:26 来源:亿速云 阅读:135 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关SpringBoot中怎么实现一个商城秒杀系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

 

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中。

@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;  }

关于SpringBoot中怎么实现一个商城秒杀系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI