赞
踩
基于Redisson实现Redis分布式锁 https://riemann.blog.csdn.net/article/details/104763755
@Controller public class RedisLockController { private static final Logger LOGGER = LoggerFactory.getLogger(RedisLockController.class); @Resource private RedissonClient redissonClient; @Resource private StringRedisTemplate stringRedisTemplate; @RequestMapping("/deduct_stock_2") @Transactional public ApiResult deductStock() { String lockKey = "lockKey"; RLock rLock = redissonClient.getLock(lockKey); try { rLock.lock(); int stockNum = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock")); if (stockNum > 0) { int realStockNum = stockNum - 1; stringRedisTemplate.opsForValue().set("stock", realStockNum + ""); LOGGER.info("扣减成功,剩余库存:{}", realStockNum); } else { LOGGER.info("扣减失败,库存不足"); } } finally { rLock.unlock(); } return new ApiResult(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。