当前位置:网站首页>Implementation of redis distributed lock
Implementation of redis distributed lock
2022-07-04 20:44:00 【The great man ate a Kun on the day he was alive】
redis Distributed locks are easy to implement
1 Import jedis rely on
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version> Just fill in the version </version>
</dependency>
2 Lock && Unlock
@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”;
/**
* Try to get distributed lock
* @param jedis Redis client
* @param lockKey lock
* @param requestId The request id
* @param expireTime Beyond the time
* @return Success or failure
*/
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;
}
/**
* Release distributed lock
* @param jedis Redis client
* @param lockKey lock
* @param requestId The request id
* @return Whether to release successfully
*/
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 This is simpler , Official recommended use reddsion Establish distributed locks
4 There are some problems I'll add later , Throw it out first , And provide some solutions , If there is any deficiency , Welcome to spray
1. The expiration time is set here , But if two services make a request one after the other ,a The service is stuck , The lock has expired ,B Service started , however B The lock opened by the service is A The service is off ,B The service is unlocked ,A The service doesn't close its own lock , therefore ....
For this kind of problem It uses requestId To solve
2 Suppose that due to business needs , Without setting the expiration time , Service to hang , restart This lock , Lock the loneliness . You can use redis It's sentinel mode
3 hypothesis redis Hang up , Lock failure , The service is close to streaking , I don't know if the data is dirty , For sites , Use transaction rollback appropriately
边栏推荐
- 【深度学习】一文看尽Pytorch之十九种损失函数
- go笔记(3)Go语言fmt包的用法
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Jiuqi ny8b062d MCU specification /datasheet
- Related concepts of federal learning and motivation (1)
- 字节测试工程师十年经验直击UI 自动化测试痛点
- 原来这才是 BGP 协议
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- 黄金k线图中的三角形有几种?
- repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
猜你喜欢
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
剑指 Offer II 80-100(持续更新)
实操自动生成接口自动化测试用例
【深度学习】一文看尽Pytorch之十九种损失函数
How to adapt your games to different sizes of mobile screen
看腾讯大老如何做接口自动化测试
Practice examples to understand JS strong cache negotiation cache
AP8022开关电源小家电ACDC芯片离线式开关电源IC
随机推荐
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
C # better operation mongodb database
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Alibaba testers use UI automated testing to achieve element positioning
记一次 .NET 某工控数据采集平台 线程数 爆高分析
idea配置标准注释
Win11无法将值写入注册表项如何解决?
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
关于联邦学习和激励的相关概念(1)
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
卷积神经网络在深度学习中新发展的5篇论文推荐
Hash哈希竞猜游戏系统开发如何开发丨哈希竞猜游戏系统开发(多套案例)
LeetCode 871. 最低加油次数
强化学习-学习笔记2 | 价值学习
Informatics Olympiad 1336: [example 3-1] find roots and children
Reinforcement learning - learning notes 2 | value learning
Why is the maximum speed the speed of light
word中插入图片后,图片上方有一空行,且删除后布局变乱
Redis分布式锁的实现