Zookeeper分布式锁实现

本文介绍了一种基于ZooKeeper实现的分布式锁方案。详细展示了如何通过ZooKeeper客户端创建临时顺序节点来实现分布式环境下的锁机制。此外,还提供了一个具体的分布式锁实现示例,并演示了如何在代码中应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说,代码从我的iteye博客里贴过来的(https://washingtin.iteye.com/blog/2442081

一、引入zkclient包

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

二、直接上代码

1、定义分布式锁接口

package com.springboot.zookeeper.distribute;
import java.util.concurrent.TimeUnit;
/**
* Created by washingtin on 2019/6/24.
*/
public interface DistributedLock {
    /**
     * 获取锁,如果没有得到锁就一直等待
     *
     * @throws Exception
     */
     public void acquire() throws Exception;
     /**
     * 获取锁,如果没有得到锁就一直等待直到超时
     *
     * @param time 超时时间
     * @param unit time参数时间单位
     *
     * @return 是否获取到锁
     * @throws Exception
     */
     public boolean acquire(long time, TimeUnit unit) throws Exception;
     /**
     * 释放锁
     *
     * @throws Exception
     */
     public void release() throws Exception;
}

2、分布式锁的实现细节

获取分布式锁的重点逻辑在于BaseDistributedLock,实现了基于Zookeeper实现分布式锁的细节。

package com.springboot.zookeeper.distribute;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


/**
 * 实现zookeeper的分布式锁
 * Created by washingtin on 2019/6/24.
 **/
public class BaseDistributedLock {

    private final ZkClient client;
    /**
     *  用于保存Zookeeper中实现分布式锁的节点,例如/locker节点,该节点是个持久节点,
     *  在该节点下面创建临时顺序节点来实现分布式锁
     **/
    private final String basePath ;

    //同basePath变量一样
    private final String path ;
    /**
     *  锁名称前缀,/locker下创建的顺序节点,例如以lock-开头,这样便于过滤无关节点
     *
     **/
    private final String lockName;
    //最大重试次数
    private static final Integer MAX_RETRY_COUNT = 10;

    public BaseDistributedLock(ZkClient client, String path, String lockName) {
        this.client = client;
        this.basePath = path;
        this.path = path.concat("/").concat(lockName);
        this.lockName = lockName;
    }

    /**
     * 删除节点
     *
     * @param path
     * @throws Exception
     */
    private void deletePath(String path) throws Exception {
        client.delete(path);
    }

    /**
     * 创建临时顺序节点
     *
     * @param client Zookeeper客户端
     * @param path 节点路径
     * @return
     * @throws Exception
     */
    private String createEphemeralSequential( 
                   ZkClient client, String path ) throws Exception {

        return client.createEphemeralSequential(path, null);
    }

    /**
     * 获取锁的核心方法
     *
     * @param startMillis 当前系统时间
     * @param millisToWait 超时时间
     * @param path
     * @return
     * @throws Exception
     */
    private boolean waitToLock(
                        long startMillis, Long millisToWait, 
                        String path ) throws Exception {
        //获取锁标志
        boolean haveTheLock = false;
        //删除锁标志
        boolean doDelete = false;
        try {
            while (!haveTheLock) {
              // 获取/locker节点下的所有顺序节点,并且从小到大排序
              List<String> children = getSortedChildren();
              // 获取子节点,如:/locker/node_0000000003返回node_0000000003
              String sequenceNodeName = path.substring(basePath.length() + 1);
              /**
                * 计算刚才客户端创建的顺序节点在locker的所有子节点中排序位置,
                * 如果是排序为0,则表示获取到了锁
                */
              int ourIndex = children.indexOf(sequenceNodeName);
              /**
                * 如果在getSortedChildren中没有找到之前创建的[临时]顺序节点,
                * 这表示可能由于网络闪断而导致Zookeeper认为连接断开而删除了我们创建的节点,
                * 此时需要抛出异常,让上一级去处理上一级的做法是捕获该异常,
                * 并且执行重试指定的次数,见后面的 attemptLock方法
                */
              if (ourIndex < 0) {
                 throw new ZkNoNodeException("节点没有找到: " + sequenceNodeName);
              }

              /** 如果当前客户端创建的节点在locker子节点列表中位置大于0,表示其它
                *客户端已经获取了锁此时当前客户端需要等待其它客户端释放锁
                */
              boolean isGetTheLock = ourIndex == 0; //是否得到锁
              /** 如何判断其它客户端是否已经释放了锁?从子节点列表中获取到比
                *自己次小的那个节点,并对其建立监听
                *获取比自己次小的那个节点,如:node_0000000002
                */
              String pathToWatch = isGetTheLock? null : children.get(ourIndex - 1);
              if (isGetTheLock) {
                  haveTheLock = true;
              } else {
              /**
                * 如果次小的节点被删除了,则表示当前客户端的节点应该是最小的了,
                * 所以使用CountDownLatch来实现等待
                */
            String previousSequencePath = basePath.concat("/").concat(pathToWatch);
            final CountDownLatch latch = new CountDownLatch(1);
            final IZkDataListener previousListener = new IZkDataListener() {
            /**
              * 监听指定节点删除时触发该方法
              */
            public void handleDataDeleted(String dataPath) throws Exception {
                // 次小节点删除事件发生时,让countDownLatch结束等待
                // 此时还需要重新让程序回到while,重新判断一次!
                latch.countDown();
            }

            /**
              * 监听指定节点的数据发生变化触发该方法
              *
              */
            public void handleDataChange(String dataPath,Object data) 
                throws Exception {

                }

            };
            try {
               // 如果节点不存在会出现异常
               // 监听比自己次小的那个节点
                 client.subscribeDataChanges(previousSequencePath
                                                        , previousListener);
                 //发生超时需要删除节点
                 if (millisToWait != null) {
                      millisToWait -= (System.currentTimeMillis() - startMillis);
                      startMillis = System.currentTimeMillis();
                      if (millisToWait <= 0) {
                         doDelete = true; // timed out - delete our node
                         break;
                      }

                      latch.await(millisToWait, TimeUnit.MICROSECONDS);
                  } else {
                      latch.await();
                  }

                } catch (ZkNoNodeException e) {
                        // ignore
                } finally {
                    client.unsubscribeDataChanges(previousSequencePath
                                                        ,previousListener);
                }
             }
          }
       } catch (Exception e) {
        // 发生异常需要删除节点
        doDelete = true;
        throw e;
       } finally {
        // 如果需要删除节点
        if (doDelete) {
           deletePath(path);
        }
       }
       return haveTheLock;
    }

    private String getLockNodeNumber(String str, String lockName) {
        int index = str.lastIndexOf(lockName);
        if (index >= 0) {
            index += lockName.length();
            return index <= str.length() ? str.substring(index) : "";
        }
        return str;
    }

    /**
     * 获取parentPath节点下的所有顺序节点,并且从小到大排序
     *
     * @return
     * @throws Exception
     */
    private List<String> getSortedChildren() throws Exception {
        try {
            List<String> children = client.getChildren(basePath);
            Collections.sort(children, new Comparator<String>() {
                public int compare(String lhs, String rhs) {
                    return getLockNodeNumber(lhs, lockName).compareTo(
                            getLockNodeNumber(rhs, lockName));
                }
            });
            return children;
        } catch (ZkNoNodeException e) {
            client.createPersistent(basePath, true); //创建锁持久节点
            return getSortedChildren();
        }
    }

    /**
     * 释放锁
     *
     * @param lockPath
     * @throws Exception
     */
    protected void releaseLock(String lockPath) throws Exception {
        deletePath(lockPath);
    }

    /**
     * 尝试获取锁
     *
     * @param time
     * @param unit
     * @return
     * @throws Exception
     */
    protected String attemptLock(long time, TimeUnit unit) throws Exception {
        final long startMillis = System.currentTimeMillis();
        final Long millisToWait = (unit != null) ? unit.toMillis(time) : null;
        String ourPath = null;
        boolean hasTheLock = false; //获取锁标志
        boolean isDone = false; //是否完成得到锁
        int retryCount = 0; //重试次数
        // 网络闪断需要重试一试
        while (!isDone) {
            isDone = true;
            try {
                /**
                  * createLockNode用于在locker(basePath持久节点)
                  * 下创建客户端要获取锁的[临时]顺序节点
                  */
                ourPath = createEphemeralSequential(client, path);
                /**
                 * 该方法用于判断自己是否获取到了锁,即自己创建的顺序节点在locker的
                 * 所有子节点中是否最小
                 * 如果没有获取到锁,则等待其它客户端锁的释放,并且稍后重试直到获取到锁或者超时
                 */
                hasTheLock = waitToLock(startMillis, millisToWait, ourPath);
            } catch (ZkNoNodeException e) {
                if (retryCount++ < MAX_RETRY_COUNT) {
                    isDone = false;
                } else {
                    throw e;
                }
            }
        }

        System.out.println(ourPath + "锁获取" + (hasTheLock ? "成功" : "失败"));
        if (hasTheLock) {
            return ourPath;
        }

        return null;
    }
}

3.定义一个简单的互斥锁

package com.springboot.zookeeper.distribute;
import org.I0Itec.zkclient.ZkClient;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
 * Created by washingtin on 2019/6/24.
 */
public class SimpleDistributedLock extends BaseDistributedLock 
                                   implements DistributedLock {

    /*
     * 用于保存Zookeeper中实现分布式锁的节点,如名称为locker:/locker,
     * 该节点应该是持久节点,在该节点下面创建临时顺序节点来实现分布式锁
     */
    private final String basePath;
    /*
     * 锁名称前缀,locker下创建的顺序节点例如都以lock-开头,这样便于过滤无关节点
     * 这样创建后的节点类似:lock-00000001,lock-000000002
     */
    private static final String LOCK_NAME = "lock-";
    /*
     * 用于保存某个客户端在locker下面创建成功的顺序节点,用于后续相关操作使用(如判断)
     *
     */
    private String ourLockPath;
    /**
     * 传入Zookeeper客户端连接对象,和basePath
     *
     * @param client
     *            Zookeeper客户端连接对象
     * @param basePath
     *            basePath是一个持久节点
     */
    public SimpleDistributedLock(ZkClient client, String basePath) {
        /*
         * 调用父类的构造方法在Zookeeper中创建basePath节点,并且为basePath节点子节点设置前缀
         * 同时保存basePath的引用给当前类属性
         */
        super(client, basePath, LOCK_NAME);
        this.basePath = basePath;
    }

    /**
     * 用于获取锁资源,通过父类的获取锁方法来获取锁
     *
     * @param time 获取锁的超时时间
     * @param unit 超时时间单位
     *
     * @return 是否获取到锁
     * @throws Exception
     */
    private boolean internalLock(long time, TimeUnit unit) throws Exception {
        // 如果ourLockPath不为空则认为获取到了锁,具体实现细节见attemptLock的实现
        ourLockPath = attemptLock(time, unit);
        return ourLockPath != null;
    }

    /**
     * 获取锁,如果没有得到锁就一直等待
     *
     * @throws Exception
     */
    public void acquire() throws Exception {
        // -1表示不设置超时时间,超时由Zookeeper决定
        if (!internalLock(-1, null)) {
            throw new IOException("连接丢失!在路径:'" + basePath + "'下不能获取锁!");
        }
    }

    /**
     * 获取锁,如果没有得到锁就一直等待直到超时
     *
     * @param time 超时时间
     * @param unit time参数时间单位
     *
     * @return 是否获取到锁
     * @throws Exception
     */
    public boolean acquire(long time, TimeUnit unit) throws Exception {
        return internalLock(time, unit);
    }


    /**
     * 释放锁
     */
    public void release() throws Exception {
        releaseLock(ourLockPath);
        System.out.println(ourLockPath + "锁已释放...");
    }
}

4.Test类

package com.springboot.zookeeper;import com.springboot.zookeeper.distribute.SimpleDistributedLock;
import org.I0Itec.zkclient.ZkClient;
import org.junit.Test;
/**
 * Created by washingtin on 2019/6/24.
 */
public class DistributeLockTest {

    @Test
    public void distributeLockTest() throws Exception {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 3000);
        SimpleDistributedLock simple = new SimpleDistributedLock(zkClient, "/locker");
        for (int i = 0; i < 10; i++) {
            try {
                simple.acquire();
                System.out.println("正在进行运算操作:" + System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                simple.release();
                System.out.println("=================\r\n");
            }
        }
    }
}

这里不进行测试了,请朋友们自己运行看一下结果即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值