如何使用Spring Boot整合Redis构建高效秒杀系统?

2026-05-28 06:551阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计784个文字,预计阅读时间需要4分钟。

如何使用Spring Boot整合Redis构建高效秒杀系统?

原文示例:本文字例为大家分享了springboot集成redis实现简单秒杀系统的整体代码,供大家参考,具体内容如下:

项目是有地址的,我会放到文章的最后面。

1. 直接Service接口,我们会介绍两种秒杀模式:PU

改写后:

本例展示了如何使用Spring Boot集成Redis实现一个简单的秒杀系统,整体代码如下:

项目地址将在文章末尾提供。

1. 直接访问Service接口,我们将介绍两种秒杀模式:PU

如何使用Spring Boot整合Redis构建高效秒杀系统?

本文实例为大家分享了springboot集成redis实现简单秒杀系统的具体代码,供大家参考,具体内容如下

项目是有地址的,我会放到文章的最后面

1. 直接service,我们会介绍两种秒杀模式

public interface GoodsService { /** * 通过lua脚本实现的秒杀 * @param skuCode 商品编码 * @param buyNum 购买数量 * @return 购买数量 */ Long flashSellByLuaScript(String skuCode,int buyNum); /** * 通过redis 事务 实现的秒杀 * @param skuCode 商品编码 * @param buyNum 购买数量 * @return 购买数量 */ Long flashSellByRedisWatch(String skuCode,int buyNum); }

2. service实现类

import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.SessionCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Collections; import java.util.List; @Service public class GoodsServiceImpl implements GoodsService { @Resource private StringRedisTemplate stringRedisTemplate; @Override public Long flashSellByLuaScript(String skuCode,int num) { //下面是lua脚本 String luaScript ="local buyNum = ARGV[1]\n" + "local goodsKey = KEYS[1] \n" + "local goodsNum = redis.call('get',goodsKey) \n" + "if goodsNum >= buyNum \n" + "then redis.call('decrby',goodsKey,buyNum) \n" + "return buyNum \n" + "else \n" + "return '0'\n" + "end\n" + "\n" ; DefaultRedisScript<String> re = new DefaultRedisScript<String>(); //设置脚本 re.setScriptText(luaScript); //定义返回值类型,注意,如果没有这个定义,Spring不会返回结果 re.setResultType(String.class); RedisSerializer<String> stringRedisSerializer = stringRedisTemplate.getStringSerializer(); //执行LUA脚本 String result = (String) stringRedisTemplate.execute(re, stringRedisSerializer, stringRedisSerializer, null); return Long.valueOf(result); } @Override public Long flashSellByRedisWatch(String skuCode,int num){ SessionCallback<Long> sessionCallback = new SessionCallback<Long>() { @Override public Long execute(RedisOperations operations) throws DataAccessException { int result = num; //redis 乐观锁 //我们观察商品编码是否发生改变 operations.watch(skuCode); ValueOperations<String, String> valueOperations = operations.opsForValue(); String goodsNumStr = valueOperations.get(skuCode); Integer goodsNum = Integer.valueOf(goodsNumStr); //标记一个事务块的开始。 //事务块内的多条命令会按照先后顺序被放进一个队列当中, //最后由 EXEC 命令原子性(atomic)地执行。 operations.multi(); if (goodsNum >= num) { valueOperations.increment(skuCode, 0 - num); } else { result = 0; } //多条命令执行的结果集合 List exec = operations.exec(); if(exec.size()>0){ System.out.println(exec); } return (long) result; } }; return stringRedisTemplate.execute(sessionCallback); } //省略 其他的方法 }

3. controller

但是首先要向你的redis里面仍一个数据,key='xiaomi',value='100'

@ApiOperation(value = "用事务秒杀测试接口", notes = "用事务秒杀测试接口") @RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET) @ResponseBody public Long miaoTransaction() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); return res; } @ApiOperation(value = " 秒杀Lua测试接口", notes = "秒杀Lua测试接口") @RequestMapping(value = "/miaoLua", method = RequestMethod.GET) @ResponseBody public Long miaoLua() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); System.out.println(res.toString()); return res; }

然后就可以用jemeter并发访问了。

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

本文共计784个文字,预计阅读时间需要4分钟。

如何使用Spring Boot整合Redis构建高效秒杀系统?

原文示例:本文字例为大家分享了springboot集成redis实现简单秒杀系统的整体代码,供大家参考,具体内容如下:

项目是有地址的,我会放到文章的最后面。

1. 直接Service接口,我们会介绍两种秒杀模式:PU

改写后:

本例展示了如何使用Spring Boot集成Redis实现一个简单的秒杀系统,整体代码如下:

项目地址将在文章末尾提供。

1. 直接访问Service接口,我们将介绍两种秒杀模式:PU

如何使用Spring Boot整合Redis构建高效秒杀系统?

本文实例为大家分享了springboot集成redis实现简单秒杀系统的具体代码,供大家参考,具体内容如下

项目是有地址的,我会放到文章的最后面

1. 直接service,我们会介绍两种秒杀模式

public interface GoodsService { /** * 通过lua脚本实现的秒杀 * @param skuCode 商品编码 * @param buyNum 购买数量 * @return 购买数量 */ Long flashSellByLuaScript(String skuCode,int buyNum); /** * 通过redis 事务 实现的秒杀 * @param skuCode 商品编码 * @param buyNum 购买数量 * @return 购买数量 */ Long flashSellByRedisWatch(String skuCode,int buyNum); }

2. service实现类

import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.SessionCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Collections; import java.util.List; @Service public class GoodsServiceImpl implements GoodsService { @Resource private StringRedisTemplate stringRedisTemplate; @Override public Long flashSellByLuaScript(String skuCode,int num) { //下面是lua脚本 String luaScript ="local buyNum = ARGV[1]\n" + "local goodsKey = KEYS[1] \n" + "local goodsNum = redis.call('get',goodsKey) \n" + "if goodsNum >= buyNum \n" + "then redis.call('decrby',goodsKey,buyNum) \n" + "return buyNum \n" + "else \n" + "return '0'\n" + "end\n" + "\n" ; DefaultRedisScript<String> re = new DefaultRedisScript<String>(); //设置脚本 re.setScriptText(luaScript); //定义返回值类型,注意,如果没有这个定义,Spring不会返回结果 re.setResultType(String.class); RedisSerializer<String> stringRedisSerializer = stringRedisTemplate.getStringSerializer(); //执行LUA脚本 String result = (String) stringRedisTemplate.execute(re, stringRedisSerializer, stringRedisSerializer, null); return Long.valueOf(result); } @Override public Long flashSellByRedisWatch(String skuCode,int num){ SessionCallback<Long> sessionCallback = new SessionCallback<Long>() { @Override public Long execute(RedisOperations operations) throws DataAccessException { int result = num; //redis 乐观锁 //我们观察商品编码是否发生改变 operations.watch(skuCode); ValueOperations<String, String> valueOperations = operations.opsForValue(); String goodsNumStr = valueOperations.get(skuCode); Integer goodsNum = Integer.valueOf(goodsNumStr); //标记一个事务块的开始。 //事务块内的多条命令会按照先后顺序被放进一个队列当中, //最后由 EXEC 命令原子性(atomic)地执行。 operations.multi(); if (goodsNum >= num) { valueOperations.increment(skuCode, 0 - num); } else { result = 0; } //多条命令执行的结果集合 List exec = operations.exec(); if(exec.size()>0){ System.out.println(exec); } return (long) result; } }; return stringRedisTemplate.execute(sessionCallback); } //省略 其他的方法 }

3. controller

但是首先要向你的redis里面仍一个数据,key='xiaomi',value='100'

@ApiOperation(value = "用事务秒杀测试接口", notes = "用事务秒杀测试接口") @RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET) @ResponseBody public Long miaoTransaction() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); return res; } @ApiOperation(value = " 秒杀Lua测试接口", notes = "秒杀Lua测试接口") @RequestMapping(value = "/miaoLua", method = RequestMethod.GET) @ResponseBody public Long miaoLua() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); System.out.println(res.toString()); return res; }

然后就可以用jemeter并发访问了。

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