Zookeeper学习————应用场景


应用场景:

  1. 配置中心
  2. 分布式id
  3. 分布式锁
Curator连接zookeeper

请查看:https://blog.csdn.net/qq_42513284/article/details/107500316

1.配置中心

  1. 将配置信息配置到zookeeper
  2. 读取配置信息,存入本地变量,注册watcher监听器
  3. 当配置信息发生改变时,通过watcher的回调事件捕获事件类型
  4. 重新读取配置信息和更新本地变量,重新注册watcher监听器

2.分布式id

  1. 创建临时有序节点
  2. 获取路径,截取定义的前缀之后的数字
/**
 * 分布式id
 */
public String getUniqueId() {
    String id = "";
    try {
        String prefix = "/uniqueId";
        String s = curatorFramework
            .create()
            //支持递归创建节点:如果父节点不存在,就创建父节点
            .creatingParentsIfNeeded()
            .withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
            .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
            .forPath(prefix, "123".getBytes());
        if (null != s){
            id = s.substring(9);
        }
    } catch (Exception e) {
        return id;
    }
    return id;
}

3.分布式锁

在这里插入图片描述

1.1.可重入排它锁
 /**
  * 分布式锁: interProcessMutex——可重入排它锁
  */
public void lock1() {
    InterProcessMutex interProcessMutex = null;
    boolean acquire = false;
    try {
        interProcessMutex = new InterProcessMutex(curatorFramework, "");
        //@param1:5000 超时时间
        //@param2:时间单位
        acquire = interProcessMutex.acquire(5000, TimeUnit.MILLISECONDS);
        if (acquire) {
            //已获取锁
        }
    } catch (Exception e) {

    } finally {
        if (null!=interProcessMutex && acquire){
            try {
                interProcessMutex.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
1.2.不可重入排它锁
 /**
  * 分布式锁: interProcessSemaphoreMutex——不可重入排它锁
  */
public void lock2() {
    InterProcessSemaphoreMutex interProcessSemaphoreMutex = null;
    boolean acquire = false;
    try {
        interProcessSemaphoreMutex = new InterProcessSemaphoreMutex(curatorFramework, "");
        //@param1:5000 超时时间
        //@param2:时间单位
        acquire = interProcessSemaphoreMutex.acquire(5000, TimeUnit.MILLISECONDS);
        if (acquire) {
            //已获取锁
        }
    } catch (Exception e) {

    } finally {
        if (null!=interProcessSemaphoreMutex && acquire){
            try {
                interProcessSemaphoreMutex.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
1.3.多个对象加锁
 /**
  * 分布式锁: interProcessMultiLock——对多个对象加锁
  */
public void lock3() {
    InterProcessMultiLock interProcessMultiLock = null;
    boolean acquire = false;
    try {
        List<String> paths=new ArrayList<>();
        interProcessMultiLock = new InterProcessMultiLock(curatorFramework, paths);
        //@param1:5000 超时时间
        //@param2:时间单位
        acquire = interProcessMultiLock.acquire(5000, TimeUnit.MILLISECONDS);
        if (acquire) {
            //已获取锁
        }
    } catch (Exception e) {

    } finally {
        if (null!=interProcessMultiLock && acquire){
            try {
                interProcessMultiLock.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
1.4.读写锁
1.4.1.读锁
/**
 * 分布式锁: readWriteLock——读锁(readLock)
 */
public void readLock() {
    InterProcessMutex interProcessMutex = null;
    boolean acquire = false;
    try {
        InterProcessReadWriteLock readLock = new InterProcessReadWriteLock(curatorFramework, "/path");
        //@param1:5000 超时时间
        //@param2:时间单位
        interProcessMutex = readLock.readLock();
        interProcessMutex.acquire(5000, TimeUnit.MILLISECONDS);
        if (acquire) {
            //已获取读锁
        }
    } catch (Exception e) {

    } finally {
        if (null!=interProcessMutex && acquire){
            try {
                interProcessMutex.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}   
1.4.2.写锁
/**
 * 分布式锁: readWriteLock——写锁(writeLock)
 */
public void writeLock() {
    InterProcessMutex interProcessMutex = null;
    boolean acquire = false;
    try {
        InterProcessReadWriteLock writeLock = new InterProcessReadWriteLock(curatorFramework, "/path");
        //@param1:5000 超时时间
        //@param2:时间单位
        interProcessMutex = writeLock.writeLock();
        interProcessMutex.acquire(5000, TimeUnit.MILLISECONDS);
        if (acquire) {
            //已获取写锁
        }
    } catch (Exception e) {

    } finally {
        if (null!=interProcessMutex && acquire){
            try {
                interProcessMutex.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值