Distributed Atomic Long - 尝试原子增加的计数器首先它尝试乐观锁.如果失败,可选的InterProcessMutex会被采用.
不管是optimistic 还是 mutex, 重试机制都被用来尝试增加值.
Caches
Path Cache - Path Cache用来监控ZNode. Whenever a child is added,
updated or removed, the Path Cache will change its state to contain the current set of children, the children’s data and the children’s state. Path caches in the Curator Framework are provided by the PathChildrenCache class. Changes to the path are passed
to registered PathChildrenCacheListener instances.
Node Cache - A utility that attempts to keep the data from
a node locally cached. This class will watch the node, respond to update/create/delete events, pull down the data, etc. You can register a listener that will get notified when changes occur.
Nodes
Persistent Ephemeral Node - An ephemeral
node that attempts to stay present in ZooKeeper, even through connection and session interruptions..
Queues
Distributed Queue - 分布式的ZK队列. Items put into the
queue are guaranteed to be ordered (by means of ZK’s PERSISTENTSEQUENTIAL node). If a single consumer takes items out of the queue, they will be ordered FIFO. If ordering is important, use a LeaderSelector to nominate a single consumer.
Distributed Id Queue - A version of DistributedQueue
that allows IDs to be associated with queue items. Items can then be removed from the queue if needed.
Distributed Priority Queue - An implementation
of the Distributed Priority Queue ZK recipe.
Distributed Delay Queue - An implementation
of a Distributed Delay Queue.
Simple Distributed Queue - A drop-in replacement
for the DistributedQueue that comes with the ZK distribution.
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");
方法
方法名
描述
create()
开始一create操作. 可以调用额外的方法(mode or background),最后调用forPath()
delete()
开始一个delete操作. 调用额外的方法(version or background) , 最好调用forPath()
checkExists()
开始一个检查ZNode是否存在的操作. 调用额外的方法 (watch or background), 最后调用forPath()
getData()
开始一个获取ZNode节点数据的操作. 调用额外的方法(watch, background or get stat), 最后调用forPath()
setData()
开始一个设置ZNode节点数据的操作. 调用额外的方法(version or background), 最后调用forPath()
getChildren()
开始一个获取ZNode的子节点列表的操作.调用额外的方法(watch, background or get stat), 最后调用forPath()
inTransaction()
开始一个原子的ZooKeeper事务. 可以包含 create, setData, check, and/or delete 操作的组合, 然后commit() 作为一个原子操作.
通知 Notifications
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.
EnsurePath ensurePath = new EnsurePath(aFullPathToEnsure);...String nodePath = aFullPathToEnsure + "/foo";ensurePath.ensure(zk); // first time syncs and creates if neededzk.create(nodePath, ...);...ensurePath.ensure(zk); // subsequent times are NOPszk.create(nodePath, ...);
RetryLoop retryLoop = client.newRetryLoop();while ( retryLoop.shouldContinue() ){ try { // perform your work ... // it's important to re\-get the ZK instance as there may have been an error and the instance was re\-created ZooKeeper zk = client.getZookeeper(); retryLoop.markComplete(); } catch ( Exception e ) { retryLoop.takeException(e); }}
RetryLoop.callWithRetry(client, new Callable<Void>(){ @Override public Void call() throws Exception { // do your work here - it will get retried if needed return null; }});