分布式锁的概念:比如说"进程
1"
在使用该资源的时候,会先去获得锁,
"
进程
1"
获得锁以后会对该资源
保持独占,这样其他进程就无法访问该资源,"
进程
1"
用完该资源以后就将锁释放掉,让其 他进程来获得锁,那么通过这个锁机制,我们就能保证了分布式系统中多个进程能够有序的访问该临界资源。那么我们把这个分布式环境下的这个锁叫作分布式锁。
?1.原生 Zookeeper 实现分布式锁案例
1)分布式锁实现
CountDownLatch类的内部维护一个初始值,该值称为计数器,主线程执行await()方法时,如果计数器值大于0,主线程将会被阻塞,当另外的线程完成任务后,使用CountDownLatch的coutDown()方法使用计数器值减1
public class DistributedLock {
private final String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private final int sessionTimeout = 2000;
private final ZooKeeper zk;
private CountDownLatch connectLatch = new CountDownLatch(1);
private CountDownLatch waitLatch = new CountDownLatch(1);
private String waitPath;
private String currentNode;
public DistributedLock() throws IOException, InterruptedException, KeeperException {
//获取连接
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
//connectLatch 如果连接上zk,可以释放
//注意Event导的包是Zookeeper那个
if (watchedEvent.getState() == Event.KeeperState.SyncConnected){
connectLatch.countDown();
}
//waitLatch 需要释放
//如果发生删除节点的事件并且删除的路径是被监听的节点的路径则释放
if (watchedEvent.getType() == Event.EventType.NodeDeleted && watchedEvent.getPath().equals(waitPath)){
waitLatch.countDown();
}
}
});
//等待zk正常连接后,往下走程序
connectLatch.await();
//判断根节点/locks是否存在
Stat stat = zk.exists("/locks", false);
if(stat == null){
//创建根节点
zk.create("/locks", "locks".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
//对zk加锁
public void zklock(){
//创建对应的临时带序号节点
try {
//create方法返回的是节点路径,这里是当前节点
currentNode = zk.create("/locks/" + "seq-", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//判断创建的节点是否是最小的序号节点,如果是获取到锁;如果不是,监听它序号前一个节点
List<String> children = zk.getChildren("/locks", false);
//如果children只有一个节点,直接获取值,如果有多个节点,需要判断谁序号最小
if(children.size() == 1){
return;
}else{
//按序号进行排序
Collections.sort(children);
//获取节点名称 seq-00000000
//把/locks/截掉,剩下的为名称
String thisNode = currentNode.substring("/locks/".length());
//通过seq-00000000获取该节点在children集合的位置
int index = children.indexOf(thisNode);
//判断数据是否有问题
if(index == -1){
System.out.println("数据异常");
}else if(index == 0){
//第一个节点,可以获取锁了
return;
}else{
//需要监听它前一个节点变化
//waitPath是当前节点前一个路径
waitPath = "/locks/" + children.get(index - 1);
zk.getData(waitPath, true, null);
//等待监听
waitLatch.await();
return;
}
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//解锁
public void unZklock(){
//删除节点
try {
zk.delete(currentNode, -1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}
}
2)分布式锁测试
(1)创建两个线程
public class DistributedLockTest {
public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
final DistributedLock lock1 = new DistributedLock();
final DistributedLock lock2 = new DistributedLock();
new Thread(new Runnable() {
@Override
public void run() {
try {
lock1.zklock();
System.out.println("线程 1 获取锁");
Thread.sleep(5 * 1000);
lock1.unZklock();
System.out.println("线程 1 释放锁");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
lock2.zklock();
System.out.println("线程 2 获取锁");
Thread.sleep(5 * 1000);
lock2.unZklock();
System.out.println("线程 2 释放锁");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
(2)观察控制台变化
2.Curator 框架实现分布式锁案例
1)原生的 Java API 开发存在的问题
(
1
)会话连接是异步的,需要自己去处理。比如使用
CountDownLatch
(2)
Watch
需要重复注册,不然就不能生效
(3)开发的复杂性还是比较高的
(4)不支持多节点删除和创建。需要自己去递归
2)Curator 是一个专门解决分布式锁的框架,解决了原生 JavaAPI 开发分布式遇到的问题。
详情请查看官方文档:
https://curator.apache.org/index.html
3)Curator 案例实操
(
1
)添加依赖
在pom.xml里加入以下内容:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.3.0</version>
</dependency>
pom.xml完整代码:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>zookeeper</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
</project>
(2)代码实现
public class CuratorLockTest {
public static void main(String[] args) {
//创建分布式锁1
InterProcessMutex lock1 = new InterProcessMutex(getCuratorFramework(), "/locks");
//创建分布式锁2
InterProcessMutex lock2 = new InterProcessMutex(getCuratorFramework(), "/locks");
new Thread((new Runnable() {
@Override
public void run() {
try {
//这里同一线程获取两次为了验证可以重复进入同一个线程
lock1.acquire();
System.out.println("线程 1 获取锁");
lock1.acquire();
System.out.println("线程 1 再次获取锁");
Thread.sleep(5 * 1000);
lock1.release();
System.out.println("线程 1 释放锁");
lock1.release();
System.out.println("线程 1 再次释放锁");
} catch (Exception e) {
e.printStackTrace();
}
}
})).start();
new Thread((new Runnable() {
@Override
public void run() {
try {
lock2.acquire();
System.out.println("线程 2 获取锁");
lock2.acquire();
System.out.println("线程 2 再次获取锁");
Thread.sleep(5 * 1000);
lock2.release();
System.out.println("线程 2 释放锁");
lock2.release();
System.out.println("线程 2 再次释放锁");
} catch (Exception e) {
e.printStackTrace();
}
}
})).start();
}
// 分布式锁初始化
private static CuratorFramework getCuratorFramework() {
//重试策略,初试时间 3 秒,重试 3 次
ExponentialBackoffRetry policy = new ExponentialBackoffRetry(3000, 3);
CuratorFramework client = CuratorFrameworkFactory.builder().connectString("hadoop102:2181,hadoop103:2181,hadoop104:2181")
.connectionTimeoutMs(2000)
.sessionTimeoutMs(2000)
.retryPolicy(policy).build();
//启动客户端
client.start();
System.out.println("zookeeper 启动成功");
return client;
}
}
(3)控制台显示
|