当前位置:网站首页>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
- B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
- 九齐NY8B062D MCU规格书/datasheet
- Pytoch learning (4)
- Jiuqi ny8b062d MCU specification /datasheet
- Practice examples to understand JS strong cache negotiation cache
- Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
- 漫谈客户端存储技术之Cookie篇
- 凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
- 实战模拟│JWT 登录认证
猜你喜欢

Detailed explanation of Audi EDI invoice message

NetCore3.1 Json web token 中间件

精选综述 | 用于白内障分级/分类的机器学习技术

AP8022开关电源小家电ACDC芯片离线式开关电源IC
Practice examples to understand JS strong cache negotiation cache

强化学习-学习笔记2 | 价值学习

C server log module

Win11无法将值写入注册表项如何解决?

So this is the BGP agreement

QT writing the Internet of things management platform 38- multiple database support
随机推荐
Pointnet / pointnet++ point cloud data set processing and training
原来这才是 BGP 协议
Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
Detailed explanation of Audi EDI invoice message
Six stones programming: about code, there are six triumphs
最长的可整合子数组的长度
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
Pytoch learning (4)
What does the neural network Internet of things mean? Popular explanation
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
Template_ Large integer subtraction_ Regardless of size
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
The problem of the maximum difference between the left and right maxima
哈希(Hash)竞猜游戏系统开发功能分析及源码
九齐单片机NY8B062D单按键控制4种LED状态
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)