zookeeper命令教学,5分钟快速入门Curator从此轻松搞定Zookeeper
zookeeper命令教学,5分钟快速入门Curator从此轻松搞定Zookeeper<groupId>org.apache.curator</groupId><dependency>现在大部分互联网公司所提供的服务都是基于分布式系统实现的,因为单台服务器的性能有瓶颈,只有分布式系统才能支撑得起这么高的用户量。而分布式系统必然涉及到多台服务器的协调和同步,这就需要用到Zookeeper。Zookeeper有很多客户端,例如官方的zkClient、Netflix公司开发且开源的Curator等。本文将要介绍Curator,它的抽象层次比zkClient更高,使用起来也比zkClient容易。假设已经搭建好zookeeper集群,使用curator需要在pom.xml中增加依赖:
5分钟快速入门Curator,轻松搞定Zookeeper
原创声明
这是本人署名原创文章,未经许可不支持转载且请勿抄袭。本公众号的所有文章均原创。为了容易理解和记忆,文章以图解为主、代码为辅。如果您感兴趣,欢迎关注!
文:吴潇/Java高级工程师
现在大部分互联网公司所提供的服务都是基于分布式系统实现的,因为单台服务器的性能有瓶颈,只有分布式系统才能支撑得起这么高的用户量。而分布式系统必然涉及到多台服务器的协调和同步,这就需要用到Zookeeper。
Zookeeper有很多客户端,例如官方的zkClient、Netflix公司开发且开源的Curator等。本文将要介绍Curator,它的抽象层次比zkClient更高,使用起来也比zkClient容易。
1 Curator分为以下几个模块- curator-recipes:在多数情况下用这个artifact,它包含一些分布式系统常用的功能(recipes),例如分布式锁、领导者选举等。curator-recipes自动依赖curator-framework和curator-client。
- curator-async:包含异步DSL和O/R modeling,迁移等功能。
- curator-framework:包含Curator框架的高层API,它自动依赖curator-client。
- curator-client:相当于官方zkClient中的Zookeeper类。
- curator-test:包含一些测试工具。
- curator-example:包含几个关于Curator的例子。
- curator-x-discovery:基于Curator实现的服务发现功能。
- curator-x-discovery-server:用于Curator服务发现功能的Restful服务器。
假设已经搭建好zookeeper集群,使用curator需要在pom.xml中增加依赖:
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.2.0</version>
</dependency>
或者你要使用一些分布式锁等curator-recipes包中的功能,那么需要
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
3. 简单使用Curator1)创建curator连接
String connectionString = "10.72.244.172:2181 10.72.217.83:2181 10.72.217.84:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString new ExponentialBackoffRetry(1000 3));
client.start();
2)在curator连接上执行操作
a. 创建ZNode(永久节点)
client.create()
.creatingParentContainersIfNeeded()
.forPath(path "hello".getBytes());
解释:create()返回一个builder,然后在这个builder上操作,最后forPath()完成整个操作。
b. 创建ephemeral node(临时节点)
client.create().withMode(CreateMode.EPHEMERAL).forPath(path "hello".getBytes());
c. 获取节点值
byte[] buf = client.getData().forPath(path);
System.out.println("get data path:" path " data:" new String(buf));
d. 设置节点值
client.setData().inBackground().forPath(path "data".getBytes());
e. 检查节点是否存在
Stat stat = client.checkExists().forPath(path);
if (stat==null) {
System.out.println("exec create path:" path);
} else {
System.out.println("exec getData");
}
f. 删除节点
client.delete().deletingChildrenIfNeeded().forPath("/yourpath");
3)操作完成后,关闭curator连接
client.close();
4. 深入一点的用法:监听ZNode以上几个操作只是对节点的简单操作,在实际工作中,我们需要更复杂一些的功能。例如,当某个ZNode变化后我们需要得到通知,并做一些后续处理。这个时候就要用到curator-recipes了.
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
curator-recipes包中的Path Cache功能就是用来监听ZNode的。当zookeeper有节点被创建、修改或删除的时候,Path Cache(对应的API是PathChildrenCache类)就会修改状态,从而得到子节点列表、子节点的数据和状态等。详细见官方文档https://curator.apache.org/curator-recipes/path-cache.html。recipe还有很多其他功能,例如Leader选举、分布式锁、分布式原子变量等,非常适合分布式系统编程,见https://curator.apache.org/curator-recipes/index.html。
示例代码
String path = "/your/path";
CuratorFramework client = …; // 前面已创建好
final PathChildrenCache pathChildrenCache =
new PathChildrenCache(client path true);
pathChildrenCache.getListenable().addListener(
new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework
PathChildrenCacheEvent event) throws Exception {
System.out.println("update event type:" event.getType()
" path:" event.getData().getPath()
" data:" new String(event.getData().getData()));
List<ChildData> childDataList = pathChildrenCache.getCurrentData();
if (childDataList != null && childDataList.size() > 0) {
for (ChildData childData : childDataList) {
System.out.println("path:" childData.getPath() " "
new String(childData.getData()));
}
}
}
});
调用start()之后才生效
pathChildrenCache.start();
调用完start()之后,这个节点及子节点上有任何变更,这里的listener都会被调用。
...
不再使用的话要调用close()
pathChildrenCache.close();
说明:本文运行环境JDK8 Zookeeper 3.5.5 Curator4.2.0 Maven 3.3.9。
参考资料:[1] Zookeeper: http://zookeeper.apache.org/
[2] Curator: https://curator.apache.org/index.html
[3] Getting Started: https://curator.apache.org/getting-started.html
[4] Recipes: https://curator.apache.org/curator-recipes/index.html
[5] Path Cache: https://curator.apache.org/curator-recipes/path-cache.html
以上我们讲解了如何通过Curator访问Zookeeper,从而实现分布式系统的控制功能。看完本文你应该学会Curator/Zookeeper最基本的操作了。希望对您的工作和面试都有帮助。如果对互联网编程技术、Java、Spring、Android、C/C 、Linux、个性化推荐、Community Detection、Machine Learning、Deep Learning、Data Mining、Gnuplot、LaTeX等技术感兴趣的话,欢迎关注本公众号。