秒杀接口优化

0.秒杀优化瓶颈

优化前:
1.判断库存数量访问mysql
2.判断是否下单访问redis
3.减库存 访问MySQL
4.下单 访问两次MySQL

减少数据库的访问
1.系统初始化,把商品库存数量加载到Redis
2.收到请求,redis 预减少库存,库存不足,直接返回 (将大量请求拒之门外),库存足够进行第三步
3.请求入队,立即返回结果排队中
4.请求出队,生成订单,减少库存
5.客户端轮询,通过判断订单是否存在确定是否是否秒杀成功。
此时状态有:秒杀成功返回订单,秒杀请求还未处理,秒杀失败(秒杀商品已被购空 redis 中标记该商品)

注意问题:收到请求时候,redis 数据库会继续减库存操作,即使商品已经售空,这次这里需要进行redis 标记。
在系统启动时后进一步向 hashmap生成每一个商品对应是否结束的key:value [goodID,false]
进行秒杀时候获取该商品value 值,只有值为false 时候继续访问redis,进行减库存,redis 库存值<0,设置value :true;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java后端实现秒杀接口的示例代码: 1. 定义一个秒杀商品的实体类: ```java public class SeckillGoods { private Long id; // 商品ID private String name; // 商品名称 private Integer stock; // 商品库存 private Date startTime; // 秒杀开始时间 private Date endTime; // 秒杀结束时间 // 省略getter和setter方法 } ``` 2. 定义一个秒杀服务类: ```java public interface SeckillService { /** * 查询所有秒杀商品 */ List<SeckillGoods> listAll(); /** * 根据ID查询秒杀商品 */ SeckillGoods getById(Long id); /** * 开始秒杀 * @param id 秒杀商品ID * @param userId 用户ID */ void startSeckill(Long id, Long userId); } ``` 3. 实现秒杀服务类: ```java @Service public class SeckillServiceImpl implements SeckillService { @Autowired private SeckillGoodsMapper seckillGoodsMapper; @Autowired private SeckillOrderMapper seckillOrderMapper; @Autowired private RedisTemplate redisTemplate; @Override public List<SeckillGoods> listAll() { return seckillGoodsMapper.selectAll(); } @Override public SeckillGoods getById(Long id) { return seckillGoodsMapper.selectByPrimaryKey(id); } @Override public void startSeckill(Long id, Long userId) { // 1. 判断库存是否足够 SeckillGoods seckillGoods = getById(id); if (seckillGoods == null) { throw new RuntimeException("秒杀商品不存在"); } if (seckillGoods.getStock() <= 0) { throw new RuntimeException("秒杀商品已售罄"); } // 2. 判断用户是否已经秒杀过该商品 String key = "seckill:user:" + userId + ":goods:" + id; Boolean isExist = redisTemplate.hasKey(key); if (isExist) { throw new RuntimeException("您已经秒杀过该商品"); } // 3. 减库存 seckillGoods.setStock(seckillGoods.getStock() - 1); seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods); // 4. 创建秒杀订单 SeckillOrder seckillOrder = new SeckillOrder(); seckillOrder.setUserId(userId); seckillOrder.setGoodsId(id); seckillOrder.setCreateTime(new Date()); seckillOrder.setAmount(BigDecimal.valueOf(0)); // 省略计算金额 seckillOrderMapper.insertSelective(seckillOrder); // 5. 记录用户秒杀商品记录到Redis中 redisTemplate.opsForValue().set(key, true); } } ``` 4. 控制器中使用秒杀服务类: ```java @RestController @RequestMapping("/seckill") public class SeckillController { @Autowired private SeckillService seckillService; @GetMapping("/list") public List<SeckillGoods> listAll() { return seckillService.listAll(); } @GetMapping("/{id}") public SeckillGoods getById(@PathVariable Long id) { return seckillService.getById(id); } @PostMapping("/{id}") public void startSeckill(@PathVariable Long id, Long userId) { seckillService.startSeckill(id, userId); } } ``` 这样就完成了一个简单的Java后端实现秒杀接口的示例代码。需要注意的是,这只是一个示例,实际应用中还需要针对性能、安全等方面进行优化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值