springboot集成redis实现简单秒杀系统
2020-02-13 11:32
731 查看
本文实例为大家分享了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并发访问了。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- SSM实战——秒杀系统之Service层接口设计与实现、Spring托管、声明式事务
- 基于SpringMVC+Spring+MyBatis实现秒杀系统【数据库接口】
- SpringMvc+Mybatis实现一个简单人事管理系统(一)
- 基于SpringMVC+Spring+MyBatis实现秒杀系统【概况】
- 简单的秒杀商品实现,SpringBoot整合redis和RabbitMQ学习笔记
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
- eclipse下用J2EE(hibernate,struts2,spring)实现的简单登录、注册系统
- 基于SpringMVC+Spring+MyBatis实现秒杀系统【客户端交互】
- spring boot+mvc+mybatis+netty-sokey.io+html+js实现简单即时通讯聊天系统
- springboot(ssm)+spring security+druid+layui+xadmin2.2实现简单权限管理系统之效果展示
- SpringBoot 简单实现仿CAS单点登录系统
- 【SpringBoot商城秒杀系统项目实战13】秒杀商品详情页+秒杀倒计时功能实现
- spring+ActiveMQ+JMS+线程池实现简单的分布式,多线程,多任务的异步任务处理系统
- 自己实现一个简单的网购秒杀系统
- springmvc+mybatis实现简单的图书管理系统
- SpringMvc+Mybatis实现一个简单人事管理系统(三)
- SpringBoot 集成 rabbitmq 简单实现通过队列进行,订单系统与库存系统,物流系统之间的信息交互
- 基于SpringMVC+Spring+MyBatis实现秒杀系统【业务逻辑】
- springboot(ssm)+spring security+druid+layui+xadmin2.2实现简单权限管理系统之问题总结和解决方案
- SpringMvc+Mybatis实现一个简单人事管理系统(二)