curator简介

Netflix curator 是Netflix公司开源的一个Zookeeper client library,用于简化zookeeper客户端编程,包含一下几个模块:

  • curator-client - zookeeper client封装,用于取代原生的zookeeper客户端,提供一些非常有用的客户端特性
  • curator-framework - zookeeper api的高层封装,大大简化zookeeper客户端编程,添加了例如zookeeper连接管理、重试机制等
  • curator-recipes - zookeeper recipes 基于curator-framework的实现(除2PC以外)

maven dependency:

  1. <dependency>
  2. <groupId>com.netflix.curator</groupId>
  3. <artifactId>curator-recipes</artifactId>
  4. <version>0.6.4</version>
  5. </dependency>

注意:在www.mvnrepository.com中认为0.32为最新版本,其实迄今为止最新版本为0.64,github trunk中的版本现在是0.65-SNAPSHOT

curator framework 使用

示例代码:

  1. String path = "/test_path";
  2. CuratorFramework client = CuratorFrameworkFactory.builder()
  3. .connectString("test:2181").namespace("/test1")
  4. .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
  5. .connectionTimeoutMs(5000).build();
  6. //create a node
  7. client.create().forPath("/head", new byte[0]);
  8. //delete a node in background
  9. client.delete().inBackground().forPath("/head");
  10. // create a EPHEMERAL_SEQUENTIAL
  11. client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/head/child", new byte[0]);
  12. // get the data
  13. client.getData().watched().inBackground().forPath("/test");
  14. // check the path exits
  15. client.checkExists().forPath(path);

curator framework使用builder模式和类似nio的chain api,代码非常简洁

curator recipes 使用

InterProcessMutex

用途:进程间互斥锁

示例代码:

  1. String lockName = "/lock1";
  2. InterProcessLock lock1 = new InterProcessMutex(this.curator, lockName);
  3. InterProcessLock lock2 = new InterProcessMutex(this.curator, lockName);
  4. lock1.acquire();
  5. boolean result = lock2.acquire(1, TimeUnit.SECONDS);
  6. assertFalse(result);
  7. lock1.release();
  8. result = lock2.acquire(1, TimeUnit.SECONDS);
  9. assertTrue(result);

原理:每次调用acquire在/lock1节点节点下使用CreateMode.EPHEMERAL_SEQUENTIAL 创建新的ephemeral节点,然后getChildren获取所有的children,判断刚刚创建的临时节点是否为第一个,如果是,则获取锁成功;如果不是,则删除刚刚创建的临时节点。

注意: 每次accquire操作,成功,则请求zk server 2次(一次写,一次getChildren);如果失败,则请求zk server 3次(一次写,一次getChildren,一次delete)

InterProcessReadWriteLock

示例代码:

  1. @Test
  2. public void testReadWriteLock() throws Exception{
  3. String readWriteLockPath = "/RWLock";
  4. InterProcessReadWriteLock readWriteLock1 = new InterProcessReadWriteLock(this.curator, readWriteLockPath);
  5. InterProcessMutex writeLock1 = readWriteLock1.writeLock();
  6. InterProcessMutex readLock1 = readWriteLock1.readLock();
  7. InterProcessReadWriteLock readWriteLock2 = new InterProcessReadWriteLock(this.curator, readWriteLockPath);
  8. InterProcessMutex writeLock2 = readWriteLock2.writeLock();
  9. InterProcessMutex readLock2 = readWriteLock2.readLock();
  10. writeLock1.acquire();
  11. // same with WriteLock, can read
  12. assertTrue(readLock1.acquire(1, TimeUnit.SECONDS));
  13. // different lock, can't read while writting
  14. assertFalse(readLock2.acquire(1, TimeUnit.SECONDS));
  15. // different write lock, can't write
  16. assertFalse(writeLock2.acquire(1, TimeUnit.SECONDS));
  17. // release the write lock
  18. writeLock1.release();
  19. //both read lock can read
  20. assertTrue(readLock1.acquire(1, TimeUnit.SECONDS));
  21. assertTrue(readLock2.acquire(1, TimeUnit.SECONDS));
  22. }

原理: 同InterProcessMutext,在ephemeral node的排序算法上做trick,write lock的排序在前。

注意: 同一个InterProcessReadWriteLock如果已经获取了write lock,则获取read lock也会成功

LeaderSelector

示例代码:

  1. @Test
  2. public void testLeader() throws Exception{
  3. LeaderSelectorListener listener = new LeaderSelectorListener(){
  4. @Override
  5. public void takeLeadership(CuratorFramework client)
  6. throws Exception {
  7. System.out.println("i'm leader");
  8. }
  9. @Override
  10. public void handleException(CuratorFramework client,
  11. Exception exception) {
  12. }
  13. @Override
  14. public void notifyClientClosing(CuratorFramework client) {
  15. }};
  16. String leaderPath = "/leader";
  17. LeaderSelector selector1 = new LeaderSelector(this.curator, leaderPath, listener);
  18. selector1.start();
  19. LeaderSelector selector2 = new LeaderSelector(this.curator, leaderPath, listener);
  20. selector2.start();
  21. assertFalse(selector2.hasLeadership());
  22. }

原理:内部基于InterProcessMutex实现,具体细节参见shared lock一节

总结

curator还提供了很多其他的实现,具体参见https://github.com/Netflix/curator/wiki/Recipes

[curator] Netflix Curator 使用的更多相关文章

  1. Zookeeper开源客户端框架Curator简介

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

  2. Zookeeper开源客户端框架Curator简介[转]

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

  3. 15. 使用Apache Curator管理ZooKeeper

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  4. 15. 使用Apache Curator装饰ZooKeeper

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

  5. Zookeeper框架Curator使用

    本文参考自https://blog.csdn.net/wo541075754/article/details/69138878?utm_source=gold_browser_extension ht ...

  6. 03.Curator深入使用

    1.Apache Curator简介     Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...

  7. zookeeper(二): Curator vs zkClient

    目录 zookeeper Curator zkClient 客户端对比 写在前面 1.1. zookeeper应用开发 1.1.1. ZkClient简介 1.1.2. Curator简介 写在最后 ...

  8. Zookeeper原理与Curator使用

    近期打算实现一个基于Zookeeper的分布式的集群状态一致性控制, 对Zookeeper的原理不太了解, 正好学习一下, 网上找到了几篇文章, 先贴在这边, 等我熟读官方文档后, 再来补充自己的见解 ...

  9. 使用Apache Curator管理ZooKeeper(转)

    Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...

随机推荐

  1. js函数中参数的传递

    数据类型 在 javascript 中数据类型可以分为两类: 基本类型值 primitive type,比如Undefined,Null,Boolean,Number,String. 引用类型值,也就 ...

  2. BZOJ 2882 & 后缀数组的傻逼实现

    题意: 一个字符环,求一个开头使字典序最小. SOL: 后缀数组打起来...然后居然卡过...10sec的实现我10936ms...居然卡过??? rank倒三...啦啦啦啦啦.... 改个离散化会不 ...

  3. CORS(跨域资源共享)简介

    前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...

  4. ACM: 继续畅通工程-并查集-最小生成树-解题报告

    继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...

  5. spring Thymeleaf 中文乱码

    前言: 项目用springmvc+Thymeleaf ,但是发现html中的所有中文都乱码. 但是自己的html的编码确定已经是UTF-8,在web.xml文件里面也添加了CharacterEncod ...

  6. 使用C语言在windows下一口气打开一批网页

    作者:郝峰波 mail : fengbohello@qq.com 本博客地址:http://www.cnblogs.com/fengbohello/p/4374450.html 1.核心函数说明 核心 ...

  7. 初识WebSocket

    众所周知,Http协议是无状态的,并且是基于Request/Response的方式与服务器进行交互,也就是我们常说的单工模式.但是随着互联 网的发展,浏览器与服务端进行双向通信需求的增加,长轮询向服务 ...

  8. 关于Repeater嵌套绑定的问题

    前台代码: <div id="firstpane" class="menu_list">                <asp:Repeat ...

  9. css sprites介绍

    一.CSS Sprites简介 通常被意译为“CSS图像拼合”或“CSS贴图定位”.CSS Sprites并不是一门新技术,目前它已经在网页开发中发展得较为成熟,CSS Sprites并不是什么金科玉 ...

  10. Android studio使用增量更新进行版本升级

    今天将Android Studio更新了一下,特此记录一下升级过程,以后可能还会用得着. 首先通过菜单栏进入 Help --> Check for update 查看下当前版本是否需要更新.事实 ...