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

网友投稿 594 2023-07-10

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

如何通过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;

/**

* 商品秒杀核心业务逻辑的处理-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.getCanKilhttp://l() && 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=ihttp://temKillMapper.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;

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring中的事务管理如何配置
下一篇:Spring实战之注入集合值操作示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~