当前位置: 首页 > news >正文

Spring Boot + Vue 项目中使用 Redis 分布式锁案例

加锁使用命令:set  lock_key   unique_value  NX  PX  1000

NX:等同于SETNX ,只有键不存在时才能设置成功

PX:设置键的过期时间为10秒

unique_value:一个必须是唯一的随机值(UUID),通常由客户端生成。解决误删他人锁的关键。

这条命令是原子性的,要么一起成功,要么一起失败。

    解锁:Lua 脚本保证原子性

需要先判断当前锁的值是否是自己设置的unique_value,如果是,才能使用DEL删除,两个操作必须保证原子性,使用Lua脚本安全的释放锁;

// unlock.lua

if redis.call("get", KEYS[1]) == ARGV[1] then

     return redis.call("del", KEYS[1])

else

     return 0

end

下面是一个完整的基于 Spring Boot 和 Vue 的秒杀案例,使用 Redis 分布式锁防止超卖。

后端实现 (Spring Boot)

1. 添加依赖 (pom.xml)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>
</dependencies>

2. 应用配置 (application.yml)

spring:redis:host: localhostport: 6379password: database: 0lettuce:pool:max-active: 8max-wait: -1msmax-idle: 8min-idle: 0
server:port: 8080

3. Redis 分布式锁工具类

@Component
public class RedisDistributedLock {@Autowiredprivate StringRedisTemplate redisTemplate;// 锁的超时时间,防止死锁private static final long LOCK_EXPIRE = 30000L; // 30秒// 获取锁的等待时间private static final long LOCK_WAIT_TIME = 3000L; // 3秒// 锁的重试间隔private static final long SLEEP_TIME = 100L; // 100毫秒/*** 尝试获取分布式锁* @param lockKey 锁的key* @param requestId 请求标识(可以使用UUID)* @param expireTime 锁的超时时间(毫秒)* @return 是否获取成功*/public boolean tryLock(String lockKey, String requestId, long expireTime) {try {long startTime = System.currentTimeMillis();while (true) {// 使用SET命令代替SETNX,保证原子性Boolean result = redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, expireTime, TimeUnit.MILLISECONDS);if (Boolean.TRUE.equals(result)) {return true; // 获取锁成功}// 检查是否超时if (System.currentTimeMillis() - startTime > LOCK_WAIT_TIME) {return false; // 获取锁超时}// 等待一段时间后重试try {Thread.sleep(SLEEP_TIME);} catch (InterruptedException e) {Thread.currentThread().interrupt();return false;}}} catch (Exception e) {return false;}}/*** 释放分布式锁* @param lockKey 锁的key* @param requestId 请求标识* @return 是否释放成功*/public boolean releaseLock(String lockKey, String requestId) {// 使用Lua脚本保证原子性 ,先判断锁的键值是否等于requestId,等于才能进行删除String script = "if redis.call('get', KEYS[1]) == ARGV[1] then " +"return redis.call('del', KEYS[1]) " +"else " +"return 0 " +"end";DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();redisScript.setScriptText(script);redisScript.setResultType(Long.class);Long result = redisTemplate.execute(redisScript, Collections.singletonList(lockKey), requestId);return result != null && result == 1;}/*** 简化版获取锁(使用默认超时时间)*/public boolean tryLock(String lockKey, String requestId) {return tryLock(lockKey, requestId, LOCK_EXPIRE);}
}

4. 商品服务类

@Service
public class ProductService {@Autowiredprivate RedisDistributedLock redisDistributedLock;@Autowiredprivate StringRedisTemplate redisTemplate;private static final String PRODUCT_STOCK_PREFIX = "product:stock:";private static final String PRODUCT_LOCK_PREFIX = "product:lock:";/*** 初始化商品库存  //从数据库中查询出对应商品的库存数量*/public void initProductStock(Long productId, Integer stock) {redisTemplate.opsForValue().set(PRODUCT_STOCK_PREFIX + productId, stock.toString());}/*** 获取商品库存*/public Integer getProductStock(Long productId) {String stockStr = redisTemplate.opsForValue().get(PRODUCT_STOCK_PREFIX + productId);return stockStr != null ? Integer.parseInt(stockStr) : 0;}/*** 秒杀下单(使用分布式锁)*/public boolean seckillProduct(Long productId, String userId) {String lockKey = PRODUCT_LOCK_PREFIX + productId;String requestId = UUID.randomUUID().toString();try {// 尝试获取锁if (!redisDistributedLock.tryLock(lockKey, requestId)) {return false; // 获取锁失败}// 检查库存Integer stock = getProductStock(productId);if (stock <= 0) {return false; // 库存不足}// 模拟业务处理耗时  //修改商品的库存try {Thread.sleep(100);} catch (InterruptedException e) {Thread.currentThread().interrupt();}// 扣减库存redisTemplate.opsForValue().decrement(PRODUCT_STOCK_PREFIX + productId);// 记录订单(这里简化处理,实际应保存到数据库)System.out.println("用户 " + userId + " 成功秒杀商品 " + productId);return true;} finally {// 释放锁redisDistributedLock.releaseLock(lockKey, requestId);}}
}

前端实现 (Vue)

1. 安装依赖

npm install axios

2. 秒杀页面组件 (Seckill.vue)

<template><div class="seckill-container"><h1>商品秒杀</h1><div class="product-info"><h2>商品ID: {{ productId }}</h2><p>当前库存: {{ stock }}</p><button @click="initStock">初始化库存(100件)</button></div><div class="seckill-form"><input v-model="userId" placeholder="请输入用户ID" /><button @click="seckill" :disabled="isSeckilling">{{ isSeckilling ? '秒杀中...' : '立即秒杀' }}</button></div><div class="result"><h3>秒杀结果:</h3><p>{{ resultMessage }}</p></div><div class="logs"><h3>操作日志:</h3><ul><li v-for="(log, index) in logs" :key="index">{{ log }}</li></ul></div></div>
</template><script>
import axios from 'axios';export default {name: 'Seckill',data() {return {productId: 1001, // 商品IDstock: 0, // 当前库存userId: '', // 用户IDresultMessage: '', // 秒杀结果logs: [], // 操作日志isSeckilling: false // 是否正在秒杀};},mounted() {this.getStock();},methods: {// 获取商品库存async getStock() {try {const response = await axios.get(`http://localhost:8080/api/seckill/stock/${this.productId}`);this.stock = response.data;this.addLog(`获取库存成功: ${this.stock}`);} catch (error) {this.addLog('获取库存失败: ' + error.message);}},// 初始化库存async initStock() {try {await axios.post(`http://localhost:8080/api/seckill/init/${this.productId}/100`);this.addLog('初始化库存成功');this.getStock(); // 重新获取库存} catch (error) {this.addLog('初始化库存失败: ' + error.message);}},// 执行秒杀async seckill() {if (!this.userId) {this.resultMessage = '请输入用户ID';return;}this.isSeckilling = true;this.resultMessage = '秒杀中...';try {const response = await axios.post(`http://localhost:8080/api/seckill/${this.productId}?userId=${this.userId}`);this.resultMessage = response.data;this.addLog(`用户 ${this.userId} ${response.data}`);} catch (error) {this.resultMessage = '秒杀失败: ' + (error.response?.data || error.message);this.addLog(`用户 ${this.userId} 秒杀失败: ${error.response?.data || error.message}`);} finally {this.isSeckilling = false;this.getStock(); // 重新获取库存}},// 添加日志addLog(message) {const timestamp = new Date().toLocaleTimeString();this.logs.unshift(`[${timestamp}] ${message}`);// 只保留最近20条日志if (this.logs.length > 20) {this.logs.pop();}}}
};
</script><style scoped>
.seckill-container {max-width: 600px;margin: 0 auto;padding: 20px;
}.product-info, .seckill-form, .result, .logs {margin-bottom: 20px;padding: 15px;border: 1px solid #ddd;border-radius: 5px;
}input {padding: 8px;margin-right: 10px;width: 200px;
}button {padding: 8px 16px;background-color: #4CAF50;color: white;border: none;border-radius: 4px;cursor: pointer;
}button:disabled {background-color: #cccccc;cursor: not-allowed;
}button:hover:not(:disabled) {background-color: #45a049;
}ul {list-style-type: none;padding: 0;max-height: 300px;overflow-y: auto;
}li {padding: 5px 0;border-bottom: 1px solid #eee;
}
</style>

3. 主应用文件 (App.vue)

<template><div id="app"><Seckill /></div>
</template><script>
import Seckill from './components/Seckill.vue'export default {name: 'App',components: {Seckill}
}
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;color: #2c3e50;margin-top: 20px;
}
</style>
  1. 原子性加锁:使用 setIfAbsent 方法的原子性操作,避免非原子操作带来的竞态条件

  2. 唯一请求标识:使用 UUID 作为请求标识,确保只能释放自己加的锁

  3. 超时机制:设置锁的超时时间,防止死锁

  4. Lua脚本释放锁:使用 Lua 脚本保证判断锁归属和删除操作的原子性

  5. 重试机制:在获取锁失败后等待一段时间重试,避免立即失败


文章转载自:

http://3UHZ0pvb.zxqxx.cn
http://sjzysv0K.zxqxx.cn
http://QpMv3v0u.zxqxx.cn
http://Ekezm3WI.zxqxx.cn
http://FpKL8IX6.zxqxx.cn
http://bz0ViVhW.zxqxx.cn
http://Tm4HmUi6.zxqxx.cn
http://jLcd9JYt.zxqxx.cn
http://nali6PFG.zxqxx.cn
http://GoWrd8RJ.zxqxx.cn
http://eT3v0ZqU.zxqxx.cn
http://nCyMEWTp.zxqxx.cn
http://zVg6HZAr.zxqxx.cn
http://q2LZz2sH.zxqxx.cn
http://qjPo0h9N.zxqxx.cn
http://SeFXEPy7.zxqxx.cn
http://8xbM6e8I.zxqxx.cn
http://ToGjzqtb.zxqxx.cn
http://eME7tqAx.zxqxx.cn
http://CHkIeDMR.zxqxx.cn
http://h6NamZFn.zxqxx.cn
http://7CZdfS5b.zxqxx.cn
http://aqj524Vm.zxqxx.cn
http://awrbICRP.zxqxx.cn
http://9EbfDowC.zxqxx.cn
http://sdScp10X.zxqxx.cn
http://jNM52iWw.zxqxx.cn
http://UuGBC5lH.zxqxx.cn
http://ov9jhgwJ.zxqxx.cn
http://2nwtaFnn.zxqxx.cn
http://www.dtcms.com/a/377557.html

相关文章:

  • Unity(①基础)
  • 【测量】知识点
  • 开始 ComfyUI 的 AI 绘图之旅-ControlNet(六)
  • 楼宇自控系统监控建筑变配电系统:功效体现在安全与节能层面
  • 分布式存储:RustFS与MinIO全面对比
  • 【第24话:定位建图】 SLAM回环检测方法及原理详细介绍
  • Electron 核心模块速查表
  • SafeEar:浙大和清华联合推出的AI音频伪造检测框架,错误率低至2.02%
  • vue2+jessibuca播放h265视频
  • 智普科技推出 Claude 用户平滑迁移方案,GLM-4.5 模型全面开放
  • IIS 部署 asp.net core 项目时,出现500.19、500.31问题的解决方案
  • ASP.NET Core 中的简单授权
  • 可遇不可求的自动化运维工具 | 2 | 实施阶段一:基础准备
  • Golang安装笔记
  • 【记录】Docker|Docker内部访问LInux主机上的Ollama服务
  • MySQL 日期时间类型:从入门到精通的核心指南
  • git 同时推送两个不同平台的版本管理库
  • SoC日志管理
  • 微服务网关全解析:从入门到实践
  • 《sklearn机器学习——数据预处理》类别特征编码
  • #C语言——刷题攻略:牛客编程入门训练(十一):攻克 循环控制(三),轻松拿捏!
  • 深入剖析 Chrome PartitionAlloc 内存池源码原理与性能调优实践
  • Shell 脚本编程:函数
  • C++ STL 容器的一个重要成员函数——`emplace_back`
  • vue3:触发自动el-input输入框焦点
  • python range函数练习题
  • Q2(门座式)起重机司机的理论知识考试考哪些内容?
  • 企业微信消息推送
  • 顺序表:数据结构中的基础线性存储结构
  • 什么是X11转发?