当前位置:网站首页>分布式锁-Redission 原理分析
分布式锁-Redission 原理分析
2022-07-24 10:06:00 【正在学习编程的小学生】
1.基于setnx实现的分布式锁存在下面的问题
- 不可重入 : 同一个线程无法多次获取同一把
- 不可重试 : 获取锁只尝试一次就返回 false,没有重试机制
- 超时释放 :锁超时释放虽然可以避免死锁,但如果是业务执行耗时较长,也会导致锁释放,存在安全隐患
- 主从一致性 : 如果Redis提供了主从集群,主从同步存在延迟,当主 宕机时,如果从并同步主中的锁数据,则会出现锁实现
Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布 式的Java常用对象,还提供了许多分布式服务,其中就包含了各种分布式锁的实现。使用Redission可以解决上面提到的4个问题。
2.代码实现
2.1导入依赖

2.2配置Redisson客户端

2.3使用Redisson的分布式锁

3.Redisson可重入锁原理
3.1结构
通过hash结构:
当value是0的时候说明可以被删除
3.2流程图

源码
加锁源码:
源码解析:
释放锁源码:

源码解析:
Redisson分布式锁的原理

原理总结:
- 可重入:利用hash结构记录线程id和重入次数
- 可重试(有剩余时间就去重试,也就是发布订阅的方式来实现锁的重试):利用信号量和PubSub功能实现等待、唤醒、获取锁失败的重试机制
- 超时续约(通过定时任务不断续约):利用watchDog,每隔一段时间(releaseTime/3),重置超时时间
边栏推荐
- Synchronized scope "concurrent programming"
- Raspberry Pie: [failed] failed to start /etc/rc local Compatibility.
- PHP Basics - PHP magic method
- MySQL query database capacity size
- [STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
- 2022: I feel like sleeping for the first time during the day
- Wechat applet
- TypeError: Object of type ‘int32‘ is not JSON serializable
- The best time to buy and sell stocks Ⅲ (leetcode-123)
- Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
猜你喜欢

Ribbon's loadbalancerclient, zoneawareloadbalancer and zoneavoidancerule are three musketeers by default

Server load and CPU performance tuning

MySQL query database capacity size

Implementation principle of acid in MySQL

Analysis of Kube proxy IPVS mode

Dark king | analysis of zego low illumination image enhancement technology

Deployment and analysis of coredns

Do you really understand the concept of buffer? Take you to uncover the buffer zone~
![[C language] implementation of three versions of address book small project (including source code)](/img/3b/926001332ec05378de4c35dc28ed55.png)
[C language] implementation of three versions of address book small project (including source code)

Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)
随机推荐
Sub query of multi table query_ Single row and single column
How does ribbon get the default zoneawareloadbalancer?
cannot unpack non-iterable NoneType object
2022, enterprise informatization construction based on Unified Process Platform refers to thubierv0.1
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
JS 84*148=b6a8 how many decimal places can you make both sides equal
Mysql database JDBC programming
Get the historical quotation data of all stocks
Financial digital transformation
PHP Basics - PHP magic constants
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
PHP debugging tool - how to install and use firephp
Raspberry Pie:: no space left on device
LiteOS_ a - SYS_ The run() function is missing a header file.
2022, enterprise unified process platform design and integration specifications refer to thubierv0.1
SMTP automatic mail sending function code
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
Ask you to build a small program server
It's eleven again. Those jokes about nagging programmers going home for blind dates