Curator教程(一)快速入门

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

它包含以下几个组件:

Componentdescription
RecipesImplementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework.
FrameworkThe 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.
UtilitiesVarious utilities that are useful when using ZooKeeper.
ClientA replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities.
ErrorsHow Curator deals with errors, connection issues, recoverable exceptions, etc.
ExtensionsThe 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.7</java.version>
    <curator.version>2.11.1</curator.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-log4j12</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>log4j</artifactId>
                <groupId>log4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>${curator.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-client</artifactId>
        <version>${curator.version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值