Using Curator

本文介绍Curator框架如何简化ZooKeeper的操作。它提供了自动连接管理和重试机制,具备更简洁的API和现代流畅接口。文章还介绍了Curator的一些特性实现,如领导选举、共享锁等。

Curator uses Fluent Style

If you haven’t used this before, it might seem odd so it’s suggested that you familiarize yourself with the style.


CuratorFramework are allocated from theCuratorFrameworkFactory

You only need one CuratorFramework object for each ZooKeeper cluster you are connecting to:

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181",retryPolicy);
client.start();


The Curator Client is a low-level API. 

It is strongly recommended that you use the CuratorFramework instead of directly using Curator Zookeeper Client.


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. 


Some of the features are:

  • Automatic connection management:

    • There are potential error cases that require ZooKeeper clients to recreate a connection and/or retry operations. Curator automatically and transparently (mostly) handles these cases.

  • Cleaner API:

    • simplifies the raw ZooKeeper methods, events, etc.

    • provides a modern, fluent interface

  • Recipe implementations:

    • Leader election

    • Shared lock

    • Path cache and watcher

    • Distributed Queue

    • Distributed Priority Queue




The CuratorFramework uses a Fluent-style interface. 

Operations are constructed using builders returned by the CuratorFramework instance. 

client.create().forPath("/head", new byte[0]);
client.delete().inBackground().forPath("/head");
client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
               .forPath("/head/child",new byte[0]);
client.getData().watched().inBackground().forPath("/test");


Background operations and watches are published via the ClientListener interface. 

Register listeners with the CuratorFramework instance using the addListener method.



Curator's  namespace to avoid ZK paths conflicting in ZK Cluster.

The CuratorFramework has a concept of a "namespace".

CuratorFramework will then prepend the namespace to all paths when one of its APIs is called. 

CuratorFramework client = CuratorFrameworkFactory.builder().namespace("MyApp") ... build();
 ...
client.create().forPath("/test", data);
// node was actually written to: "/MyApp/test"


Temporary CuratorFramework instances are meant for single requests to ZooKeeper ensembles over a failure prone network such as a WAN. 

The APIs available from CuratorTempFramework are limited. 

The connection will be closed after a period of inactivity.


CuratorTempFramework instances are created via the CuratorFrameworkFactory just like normal CuratorFramework instances. 

However, instead of calling the build() method, call buildTemp()

buildTemp() creates a CuratorTempFramework instance that closes itself after 3 minutes of inactivity. 

There is an alternate version of buildTemp() that allows you to specify the inactivity period.


转载于:https://my.oschina.net/darionyaphet/blog/285032

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值