当前位置:网站首页>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挂了,锁失效,服务接近于裸奔,数据也不知道是不是脏了,针对业务点,适当使用事务回滚进行操作
边栏推荐
- Neural network IOT platform construction (IOT platform construction practical tutorial)
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- [in-depth learning] review pytoch's 19 loss functions
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
- How to adapt your games to different sizes of mobile screen
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- 托管式服务网络:云原生时代的应用体系架构进化
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- 剑指 Offer II 80-100(持续更新)
- Common verification rules of form components -1 (continuously updating ~)
猜你喜欢
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
C server log module
电脑共享打印机拒绝访问要怎么办
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
随机推荐
Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
go笔记(3)Go语言fmt包的用法
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
奏响青春的乐章
最长的可整合子数组的长度
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
关于联邦学习和激励的相关概念(1)
repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
如何让你的小游戏适配不同尺寸的手机屏幕
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
电脑共享打印机拒绝访问要怎么办
Informatics Olympiad 1336: [example 3-1] find roots and children
The problem of the maximum difference between the left and right maxima
凌云出海记 | 一零跃动&华为云:共助非洲普惠金融服务