Curator教程(一)快速入门

Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端编程。

它包含以下几个组件:

Component description
Recipes Implementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework.
Framework The Curator Framework is a high-level API that greatly simplifies using ZooKeeper. It adds many features that build on ZooKeeper and handles the complexity of managing connections to the ZooKeeper cluster and retrying operations.
Utilities Various utilities that are useful when using ZooKeeper.
Client A replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities.
Errors How Curator deals with errors, connection issues, recoverable exceptions, etc.
Extensions The curator-recipes package implements the common recipes that are described in the ZooKeeper documentation. To avoid bloating that package, recipes/applications that have a vertical appeal will be put in separate “extension” packages using the naming convention curator-x-name.

环境配置

  • JDK 1.7
  • Zookeeper 3.4.8
  • Curator 2.11.1
  • Maven 3.3

快速入门

Maven依赖

<properties>
    <java.version>1.7java.version>
    <curator.version>2.11.1curator.version>
properties>

<dependencies>
    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <version>3.4.8version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-log4j12artifactId>
                <groupId>org.slf4jgroupId>
            exclusion>
            <exclusion>
                <artifactId>log4jartifactId>
                <groupId>log4jgroupId>
            exclusion>
        exclusions>
    dependency>

    <dependency>
        <groupId>org.apache.curatorgroupId>
        <artifactId>curator-frameworkartifactId>
        <version>${curator.version}version>
    dependency>
    <dependency>
        <groupId>org.apache.curatorgroupId>
        <artifactId>curator-clientartifactId>
        <version>${curator.version}version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guavagroupId>
                <artifactId>guavaartifactId>
            exclusion>
        exclusions>
    dependency>

    <dependency>
        <groupId>com.google.guavagroupId>
        <artifactId>guavaartifactId>
        <version>18.0version>
    dependency>
dependencies>

1.创建Curator 连接实例

String address = "localhost:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));

注意:一个Zookeeper集群只需要构造一个CuratorFramework 实例对象即可。

CuratorFramework 使用之前必须先调用

client.start();

完成一系列操作后,调用client.close();方法,可以使用try-finally语句:

CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
try{
    client.start();
    ...
}finally {
    if(client!=null)
        client.close();
}

2.创建节点

a. 创建永久性节点

client.create()
      .creatingParentContainersIfNeeded()
      .withMode(CreateMode.PERSISTENT)
      .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
      .forPath(path, "hello, zk".getBytes());

b. 创建临时节点

client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes());  

3.获取节点值

byte[] buf = client.getData().forPath(path);
System.out.println("get data path:"+path+", data:"+new String(buf));

4.设置节点值

client.setData().inBackground().forPath(path, "ricky".getBytes());

5.checkExists

Stat stat = client.checkExists().forPath(path);
if(stat==null){
    System.out.println("exec create path:"+path);
}else {
    System.out.println("exec getData");
}

6.删除节点

client.delete().deletingChildrenIfNeeded().forPath("/pandora");


点此下载完整代码:https://github.com/TiFG/zookeeper-samples

参考

Getting Started:http://curator.apache.org/getting-started.html

你可能感兴趣的:(Zookeeper)