JAVA|Zookeeper分布式锁

【JAVA|Zookeeper分布式锁】
Zookeeper分布式锁

    • 相关枚举与类解释
    • 框架分布式锁

相关枚举与类解释 CreateMode部分
//持久化结点 PERSISTENT(0, false, false, false, false), //顺序自动编码持久化结点 PERSISTENT_SEQUENTIAL(2, false, true, false, false), //临时结点 EPHEMERAL(1, true, false, false, false), //顺序自动编码临时结点 EPHEMERAL_SEQUENTIAL(3, true, true, false, false);

Zookeeper ZooDefs.Ids
OPEN_ACL_UNSAFE : 完全开放的ACL,任何连接的客户端都可以操作该属性znode
CREATOR_ALL_ACL : 只有创建者才有ACL权限
READ_ACL_UNSAFE:只能读取ACL
框架分布式锁
import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessLock; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.apache.curator.retry.ExponentialBackoffRetry; /** * Created with IntelliJ IDEA. * Description: * * @Author: 苏力德 * DateTime: 2022-04-05 17:47 */ public class CuratorLockTest { private String rootNode = "/locks"; private String connectString = "zkDistributed:2181"; // connection 超时时间 private int connectionTimeout = 2000; // session 超时时间 private int sessionTimeout = 2000; public static void main(String[] args) { new CuratorLockTest().test(); }//测试 private void test() { // 创建分布式锁 1 final InterProcessLock lock1 = new InterProcessMutex(getCuratorFramework(), rootNode); // 创建分布式锁 2 final InterProcessLock lock2 = new InterProcessMutex(getCuratorFramework(), rootNode); 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(); }// 分布式锁初始化 public CuratorFramework getCuratorFramework (){ //重试策略,初试时间 3 秒,重试 3 次 RetryPolicy policy = new ExponentialBackoffRetry(3000, 3); //通过工厂创建 Curator CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(connectString) .connectionTimeoutMs(connectionTimeout) .sessionTimeoutMs(sessionTimeout) .retryPolicy(policy).build(); //开启连接 client.start(); System.out.println("zookeeper 初始化完成..."); return client; }}

    推荐阅读