当前位置:网站首页>Redis分布式锁的实现
Redis分布式锁的实现
2022-07-04 19:03:00 【大丈夫在世当日食一鲲】
1导入jedis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>随便填版本</version>
</dependency>
2 加锁&&解锁
@Component
class RedisTool {
private static final String LOCK_SUCCESS = “OK”;
private static final String SET_IF_NOT_EXIST = “NX”;
private static final String SET_WITH_EXPIRE_TIME = “PX”;
/**
* 尝试获取分布式锁
* @param jedis Redis客户端
* @param lockKey 锁
* @param requestId 请求标识
* @param expireTime 超期时间
* @return 是否获取成功
*/
public static boolean tryGetDistributedLock(Jedis jedis, String lockKey, String requestId, int expireTime) {
String result = jedis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
if (LOCK_SUCCESS.equals(result)) {
return true;
}
return false;
}
/**
* 释放分布式锁
* @param jedis Redis客户端
* @param lockKey 锁
* @param requestId 请求标识
* @return 是否释放成功
*/
public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
if (RELEASE_SUCCESS.equals(result)) {
return true;
}
return false;
}
}
3 这个比较简单,官方建议使用reddsion建立分布式锁
4 这里存在一些问题 后续再补充,先抛出来,并提供一些方案,如有不足,欢迎开喷
1.此处设置了过期时间,但是如果两个服务先后发起了一个请求,a服务卡住了,锁到期了,B服务启动了,但是B服务开的锁被A服务关了,B服务处于无锁状态,A服务关的不是自己的锁,所以。。。。
对于这种问题 上面用了requestId 来解决
2 假设因业务需要,不需要设定过期时间的情况下,服务挂了,重启 这个锁,锁了寂寞。此处可以利用redis的哨兵模式了
3 假设redis挂了,锁失效,服务接近于裸奔,数据也不知道是不是脏了,针对业务点,适当使用事务回滚进行操作
边栏推荐
- Talking about cookies of client storage technology
- Development and construction of DFI ecological NFT mobile mining system
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- Common verification rules of form components -1 (continuously updating ~)
- 六石编程学:关于代码,有六个得意
- go语言笔记(4)go常用管理命令
- Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
- Key rendering paths for performance optimization
- Chrome development tool: what the hell is vmxxx file
- Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
猜你喜欢
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
MySQL中的日期时间类型与格式化方式
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
What is the application technology of neural network and Internet of things
Key rendering paths for performance optimization
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
NetCore3.1 Json web token 中间件
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Small hair cat Internet of things platform construction and application model
Neural network IOT platform construction (IOT platform construction practical tutorial)
随机推荐
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
PHP pseudo original API docking method
实践示例理解js强缓存协商缓存
软件客户端数字签名一定要申请代码签名证书吗?
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
Practice examples to understand JS strong cache negotiation cache
The problem of the maximum difference between the left and right maxima
Selected review | machine learning technology for Cataract Classification / classification
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
jekins初始化密码没有或找不到
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Detailed explanation of Audi EDI invoice message
idea大小写快捷键
MySQL中的日期时间类型与格式化方式
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏