当前位置:网站首页>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
边栏推荐
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
- What is involution?
- idea配置标准注释
- mysql语句执行详解
- 同事的接口文档我每次看着就头大,毛病多多。。。
- Taishan Office Technology Lecture: about the order of background (shading and highlighting)
- Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
- NetCore3.1 Json web token 中间件
猜你喜欢
看腾讯大老如何做接口自动化测试
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
How does the computer save web pages to the desktop for use
NetCore3.1 Json web token 中间件
Qt编写物联网管理平台38-多种数据库支持
托管式服务网络:云原生时代的应用体系架构进化
How to solve the problem that win11 cannot write the value to the registry key?
C server log module
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
黄金k线图中的三角形有几种?
随机推荐
What financial products can you buy with a deposit of 100000 yuan?
Detailed explanation of Audi EDI invoice message
Dynamic memory management
关于联邦学习和激励的相关概念(1)
What should I do if my computer sharing printer refuses access
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Stack: how to realize the judgment of valid brackets?
Selected review | machine learning technology for Cataract Classification / classification
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
Template_ Judging prime_ Square root / six prime method
电脑怎么保存网页到桌面上使用
九齐NY8B062D MCU规格书/datasheet
Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
GVM使用
Is it necessary to apply for code signing certificate for software client digital signature?
tcp为啥是三次握手和四次挥手
repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
What is involution?
How to solve the problem that win11 cannot write the value to the registry key?
卷积神经网络在深度学习中新发展的5篇论文推荐