当前位置:网站首页>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
边栏推荐
- Flet教程之 06 TextButton基础入门(教程含源码)
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
- 六石编程学:关于代码,有六个得意
- jekins初始化密码没有或找不到
- idea恢复默认快捷键
- Template_ Large integer subtraction_ Regardless of size
- 长城证券开户安全吗 股票开户流程网上开户
- Why is the maximum speed the speed of light
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
猜你喜欢
So this is the BGP agreement
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Common verification rules of form components -1 (continuously updating ~)
How does the computer save web pages to the desktop for use
Win11无法将值写入注册表项如何解决?
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
MySQL中的日期时间类型与格式化方式
阿里测试师用UI自动化测试实现元素定位
随机推荐
Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
Informatics Olympiad 1336: [example 3-1] find roots and children
idea大小写快捷键
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
BFC面试简述
Practice examples to understand JS strong cache negotiation cache
记录线上bug解决list(未完待续7/4)
泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
Alibaba testers use UI automated testing to achieve element positioning
Redis分布式锁的实现
word中插入图片后,图片上方有一空行,且删除后布局变乱
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Template_ Large integer subtraction_ Regardless of size
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
剑指 Offer II 80-100(持续更新)
九齐单片机NY8B062D单按键控制4种LED状态
LeetCode 871. Minimum refueling times
关于联邦学习和激励的相关概念(1)
记一次 .NET 某工控数据采集平台 线程数 爆高分析
《动手学深度学习》(三) -- 卷积神经网络 CNN