[curator] Netflix Curator 使用
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:
- <dependency>
- <groupId>com.netflix.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>0.6.4</version>
- </dependency>
注意:在www.mvnrepository.com中认为0.32为最新版本,其实迄今为止最新版本为0.64,github trunk中的版本现在是0.65-SNAPSHOT
curator framework 使用
示例代码:
- String path = "/test_path";
- CuratorFramework client = CuratorFrameworkFactory.builder()
- .connectString("test:2181").namespace("/test1")
- .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
- .connectionTimeoutMs(5000).build();
- //create a node
- client.create().forPath("/head", new byte[0]);
- //delete a node in background
- client.delete().inBackground().forPath("/head");
- // create a EPHEMERAL_SEQUENTIAL
- client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/head/child", new byte[0]);
- // get the data
- client.getData().watched().inBackground().forPath("/test");
- // check the path exits
- client.checkExists().forPath(path);
curator framework使用builder模式和类似nio的chain api,代码非常简洁
curator recipes 使用
InterProcessMutex
用途:进程间互斥锁
示例代码:
- String lockName = "/lock1";
- InterProcessLock lock1 = new InterProcessMutex(this.curator, lockName);
- InterProcessLock lock2 = new InterProcessMutex(this.curator, lockName);
- lock1.acquire();
- boolean result = lock2.acquire(1, TimeUnit.SECONDS);
- assertFalse(result);
- lock1.release();
- result = lock2.acquire(1, TimeUnit.SECONDS);
- assertTrue(result);
原理:每次调用acquire在/lock1节点节点下使用CreateMode.EPHEMERAL_SEQUENTIAL 创建新的ephemeral节点,然后getChildren获取所有的children,判断刚刚创建的临时节点是否为第一个,如果是,则获取锁成功;如果不是,则删除刚刚创建的临时节点。
注意: 每次accquire操作,成功,则请求zk server 2次(一次写,一次getChildren);如果失败,则请求zk server 3次(一次写,一次getChildren,一次delete)
InterProcessReadWriteLock
示例代码:
- @Test
- public void testReadWriteLock() throws Exception{
- String readWriteLockPath = "/RWLock";
- InterProcessReadWriteLock readWriteLock1 = new InterProcessReadWriteLock(this.curator, readWriteLockPath);
- InterProcessMutex writeLock1 = readWriteLock1.writeLock();
- InterProcessMutex readLock1 = readWriteLock1.readLock();
- InterProcessReadWriteLock readWriteLock2 = new InterProcessReadWriteLock(this.curator, readWriteLockPath);
- InterProcessMutex writeLock2 = readWriteLock2.writeLock();
- InterProcessMutex readLock2 = readWriteLock2.readLock();
- writeLock1.acquire();
- // same with WriteLock, can read
- assertTrue(readLock1.acquire(1, TimeUnit.SECONDS));
- // different lock, can't read while writting
- assertFalse(readLock2.acquire(1, TimeUnit.SECONDS));
- // different write lock, can't write
- assertFalse(writeLock2.acquire(1, TimeUnit.SECONDS));
- // release the write lock
- writeLock1.release();
- //both read lock can read
- assertTrue(readLock1.acquire(1, TimeUnit.SECONDS));
- assertTrue(readLock2.acquire(1, TimeUnit.SECONDS));
- }
原理: 同InterProcessMutext,在ephemeral node的排序算法上做trick,write lock的排序在前。
注意: 同一个InterProcessReadWriteLock如果已经获取了write lock,则获取read lock也会成功
LeaderSelector
示例代码:
- @Test
- public void testLeader() throws Exception{
- LeaderSelectorListener listener = new LeaderSelectorListener(){
- @Override
- public void takeLeadership(CuratorFramework client)
- throws Exception {
- System.out.println("i'm leader");
- }
- @Override
- public void handleException(CuratorFramework client,
- Exception exception) {
- }
- @Override
- public void notifyClientClosing(CuratorFramework client) {
- }};
- String leaderPath = "/leader";
- LeaderSelector selector1 = new LeaderSelector(this.curator, leaderPath, listener);
- selector1.start();
- LeaderSelector selector2 = new LeaderSelector(this.curator, leaderPath, listener);
- selector2.start();
- assertFalse(selector2.hasLeadership());
- }
原理:内部基于InterProcessMutex实现,具体细节参见shared lock一节
总结
curator还提供了很多其他的实现,具体参见https://github.com/Netflix/curator/wiki/Recipes
[curator] Netflix Curator 使用的更多相关文章
- Zookeeper开源客户端框架Curator简介
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
- Zookeeper开源客户端框架Curator简介[转]
Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...
- 15. 使用Apache Curator管理ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- 15. 使用Apache Curator装饰ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- Zookeeper框架Curator使用
本文参考自https://blog.csdn.net/wo541075754/article/details/69138878?utm_source=gold_browser_extension ht ...
- 03.Curator深入使用
1.Apache Curator简介 Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...
- zookeeper(二): Curator vs zkClient
目录 zookeeper Curator zkClient 客户端对比 写在前面 1.1. zookeeper应用开发 1.1.1. ZkClient简介 1.1.2. Curator简介 写在最后 ...
- Zookeeper原理与Curator使用
近期打算实现一个基于Zookeeper的分布式的集群状态一致性控制, 对Zookeeper的原理不太了解, 正好学习一下, 网上找到了几篇文章, 先贴在这边, 等我熟读官方文档后, 再来补充自己的见解 ...
- 使用Apache Curator管理ZooKeeper(转)
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
随机推荐
- CSS样式覆盖顺序
有时候在写CSS的过程中,某些限制总是不起作用,这就涉及了CSS样式覆盖的问题,如下 Css代码 #navigator { height: 100%; width: 200; position: ...
- CF 346B. Lucky Common Subsequence(DP+KMP)
这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...
- 崩溃恢复(crash recovery)与 AUTORESTART参数
关于这个参数设置的影响,在生产系统中经历过两次: 第一次是有套不太重要的系统安装在虚拟机,这套系统所有应用(DB2 WAS IHS)都配置到/etc/rc.local中,每次启动机器会自 ...
- SQLSERVER远程备份、恢复(转)
SQLSERVER服务实例名称:192.168.0.2需要备份的数据库名称: a备份机器名称(Client端):192.168.0.3备份机用户:zf 密码:123备份机域名:domain备份机提供备 ...
- GO语言练习:实现最简单的http helloword 服务器
用Go语言实现一个最简单的http服务器端,主要用到了package io, log, net/http 这个3个库. 用到的函数包括: http.Handle() http.HandlerFunc( ...
- C/C++ 错误处理
has incomplete type and cannot be defined在头文件中添加该类型所在的文件eg:aggregate 'std::stringstream oss' has inc ...
- 【android tools】内存、网络、界面性能响应优化的工具
一.性能优化工具 性能分析,我理解有内存性能,IO性能, 界面性能,耗电等. 内存性能,用debuggable的app结合mat等专业工具可以分析.另外最近的Leakcanary很好用,但是要手动加入 ...
- [CareerCup] 15.6 Entity Relationship Diagram 实体关系图
15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (p ...
- WinForm 窗体属性
WinForm - C/S 客户端 B/S 网页端 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 ...
- Python的缩进
关于python的缩进:如果要确认一个函数包含哪些内容,java或php可以使用大括号将函数内容包含起来,但python里没有那样的大括号,python靠“缩进”(四个空格)来确定语句块的始末. 这是 ...