当前位置:网站首页>分布式锁-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),重置超时时间
边栏推荐
- Get the historical quotation data of all stocks
- Dynamic programming -- a collection of stock problems
- CRC Coding in C language
- 2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
- 757. Set the intersection size to at least 2: greedy application question
- LiteOS_ a - SYS_ The run() function is missing a header file.
- Learn more about the synchronized lock upgrade process [concurrent programming]
- Jenkins deploys the project and prompts that the module package defined by him cannot be found
- Web page opening speed is very slow, how to solve it?
- Spark Learning: using RDD API to implement inverted index
猜你喜欢
![[STM32 learning] (6) use of serial port 1 (usart1)](/img/b1/430d3501a99e46958c066f7fd7eee9.png)
[STM32 learning] (6) use of serial port 1 (usart1)

Ask you to build a small program server

Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?

What is the cloud native mid platform business architecture?
![[STM32 learning] (22) STM32 realizes 360 degree rotary encoder](/img/8e/fb036296ec3aff5e60acee5018943c.png)
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder

Dynamic planning: robbing families and houses

Spark Learning: Spark implementation of distcp

Home raiding III (leetcode-337)

Raspberry Pie:: no space left on device

Compilation and linking of programs
随机推荐
Compilation and linking of programs
Arduino drive Lora module node
Reading makes people improve my list
Uniapp calendar component
[STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
LiteOS_ a - SYS_ The run() function is missing a header file.
Homologous policy solutions
Redis configuration serialization
MySQL query database capacity size
Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)
Trie tree template 2
Spark Learning: a form of association in a distributed environment?
CAS principle [concurrent programming]
Get the historical quotation data of all stocks
Spark Learning: implement compact table command
Use of jstack "JVM common commands"
Color recognition of regions of interest in pictures and videos based on OpenCV
2022, our small goal
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices