资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

这篇文章将为大家详细讲解有关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中怎么实现一个商城秒杀系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:SpringBoot中怎么实现一个商城秒杀系统
文章地址:http://cdkjz.cn/article/jphjep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220