栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

如何通过SpringBoot实现商城秒杀系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过SpringBoot实现商城秒杀系统

这篇文章主要介绍了如何通过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中。

@Autowired
  private StringRedisTemplate stringRedisTemplate;
  
  @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;

  
  @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/";

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136208.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号