1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-16 18:10:13 +08:00

[docs add]分布式锁

This commit is contained in:
guide 2022-08-23 20:19:30 +08:00
parent c6a5d26e1d
commit b0cb31e2f6
4 changed files with 18 additions and 3 deletions

View File

@ -322,6 +322,10 @@ JVM 这部分内容主要参考 [JVM 虚拟机规范-Java8 ](https://docs.oracle
[为什么要分布式 id ?分布式 id 生成方案有哪些?](./docs/distributed-system/distributed-id.md)
### 分布式锁
[分布式锁](./docs/distributed-system/distributed-lock.md)
### 分布式事务
[分布式事务](./docs/distributed-system/distributed-transaction.md)

View File

@ -394,6 +394,7 @@ export const sidebarConfig = defineSidebarConfig({
},
"api-gateway",
"distributed-id",
"distributed-lock",
{
text: "RPC",
prefix: "rpc/",

View File

@ -1,3 +1,8 @@
---
title: 分布式锁
category: 分布式
---
## 什么是分布式锁?
对于单机多线程,在 Java 中,我们通常使用 `ReetrantLock` 这类 JDK 自带的 **本地锁** 来控制本地多个线程对本地共享资源的访问。对于分布式系统,我们通常使用 **分布式锁** 来控制多个服务对共享资源的访问。
@ -105,7 +110,8 @@ Redlock 算法的思想是让客户端向 Redis 集群中的多个独立的 Redi
Redlock 是直接操作 Redis 节点的,并不是通过 Redis 集群操作的,这样才可以避免 Redis 集群主从切换导致的锁丢失问题。
Redlock 实现比较复杂,性能也比较差。 《数据密集型应用系统设计》一书的作者曾经专门发文 diss 过Redlock。
Redlock 实现比较复杂,性能比较差,发生时钟变迁的情况下还存在安全性隐患。《数据密集型应用系统设计》一书的作者 Martin Kleppmann 曾经专门发文怼过 Redlock他认为这是一个很差的分布式锁实现。感兴趣的朋友可以看看[Redis 锁从面试连环炮聊到神仙打架](https://mp.weixin.qq.com/s?__biz=Mzg3NjU3NTkwMQ==&mid=2247505097&idx=1&sn=5c03cb769c4458350f4d4a321ad51f5a&source=41#wechat_redirect)这篇文章,有详细介绍到 antirez 和 Martin Kleppmann 关于 Redlock 的激烈辩论。
实际项目中不建议使用 Redlock 算法,成本和收益不成正比。
如果不是非要实现绝对可靠的分布式锁的话,其实单机版 Redis 就完全够了,实现简单,性能也非常高。如果你必须要实现一个绝对可靠的分布式锁的话,可以基于 Zookeeper 来做,只是性能会差一些。

View File

@ -325,6 +325,10 @@ JVM 这部分内容主要参考 [JVM 虚拟机规范-Java8 ](https://docs.oracle
[为什么要分布式 id ?分布式 id 生成方案有哪些?](./distributed-system/distributed-id.md)
### 分布式锁
[分布式锁](./distributed-system/distributed-lock.md)
### 分布式事务
[分布式事务](./distributed-system/distributed-transaction.md)