当前位置:网站首页>分布式锁-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),重置超时时间
边栏推荐
- 2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
- Looting (leetcode-198)
- What happens from the input URL to the page load
- [STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
- CAS principle [concurrent programming]
- Boundless dialogue | participate in the live broadcast on July 25 and win the prize
- Exception: pyqtgraph requires Qt version >= 5.12 (your version is 5.9.5)
- CRC Coding in C language
- It's eleven again. Those jokes about nagging programmers going home for blind dates
- [C language] implementation of three versions of address book small project (including source code)
猜你喜欢

MySQL 数据库 JDBC编程

Do you really understand the concept of buffer? Take you to uncover the buffer zone~

Spark Learning: compile spark source code in win10

Spark Learning: using RDD API to implement inverted index
![[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus](/img/6b/b49b9e7fff62026a4818561d79fe3f.png)
[STM32 learning] (18) STM32 realizes LCD12864 display - parallel implementation of 8-bit bus

Where is the bitbucket clone address

The best time to buy and sell stocks Ⅲ (leetcode-123)
![Calculate CPU utilization [Prometheus]](/img/00/d9f297e3013cabbf3d41be58105fc7.png)
Calculate CPU utilization [Prometheus]

Dynamic programming -- a collection of stock problems
![Raspberry Pie: [failed] failed to start /etc/rc local Compatibility.](/img/c3/d648cea4e8eef20a221dc034ecfc1d.png)
Raspberry Pie: [failed] failed to start /etc/rc local Compatibility.
随机推荐
Looting (leetcode-198)
Wechat applet
Spark Learning: using RDD API to implement inverted index
cannot unpack non-iterable NoneType object
Countdownlatch and join [concurrent programming]
Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack
Spark Learning: compile spark source code in win10
PHP Basics - PHP types
2022, our small goal
What happens from the input URL to the page load
Home raiding II (leetcode-213)
高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力
SMTP automatic mail sending function code
unity中物体z旋转同步面板上的数值
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder
[STM32 learning] (7) use of serial port 2 (usart2)
多表查询之子查询_单行单列情况
CAS principle [concurrent programming]
Learn more about the synchronized lock upgrade process [concurrent programming]
Spark Learning: Spark implementation of distcp