一、创建会话

1. 创建会话

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry; /**
* 使用Curator创建会话
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(
"192.168.1.109:2181", // 服务器列表
5000, // 会话超时时间,单位毫秒
3000, // 连接创建超时时间,单位毫秒
new ExponentialBackoffRetry(1000, 3) // 重试策略
);
client.start(); client.close();
}
}

2. 使用链式风格的API接口创建会话

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry; /**
* 使用链式风格的API接口创建会话
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.close();
}
}

二、创建节点

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; /**
* 使用Curator创建节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/zk-huey/cnode", "hello".getBytes()); client.close();
}
}

三、删除节点

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; /**
* 使用Curator删除节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/zk-huey/cnode", "hello".getBytes()); client.delete()
.guaranteed()
.deletingChildrenIfNeeded()
.withVersion(-1)
.forPath("/zk-huey"); client.close();
}
}

四、读取节点数据

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat; /**
* 使用Curator读取节点数据
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/zk-huey/cnode", "hello".getBytes()); Stat stat = new Stat();
byte[] nodeData = client.getData()
.storingStatIn(stat)
.forPath("/zk-huey/cnode");
System.out.println("NodeData: " + new String(nodeData));
System.out.println("Stat: " + stat); client.close();
}
}

五、更新节点数据

package com.huey.dream.demo;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat; /**
* 使用Curator更新节点数据
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/zk-huey/cnode", "hello".getBytes()); client.setData()
.withVersion(-1)
.forPath("/zk-huey/cnode", "world".getBytes()); client.close();
}
}

六、 获取子节点列表

package com.huey.dream.demo;

import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode; /**
* 使用Curator
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class CarutorDemo { public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.1.109:2181")
.sessionTimeoutMs(5000)
.connectionTimeoutMs(3000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start(); client.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath("/zk-huey/cnode", "hello".getBytes()); List<String> children = client.getChildren().forPath("/zk-huey");
System.out.println("Children: " + children); client.close();
}
}

ZooKeeper(3.4.5) - 开源客户端 Curator(2.7.0) 的简单示例的更多相关文章

  1. Zookeeper开源客户端Curator之创建会话

    前面Zookeeper的链接使用的都是其提供的原生代码,实际开发过程中非常底层的细节开发工作如连接重连,反复注册等耗费开发人员大量的工作精力并且重复工作.而开源客户端Curator的出现解决了该类问题 ...

  2. 八:Zookeeper开源客户端Curator的api测试

    curator是Netflix公司开源的一套ZooKeeper客户端,Curator解决了很多ZooKeeper客户端非常底层的细节开发工作.包括连接重连,反复注册Watcher等.实现了Fluent ...

  3. Zookeeper开源客户端Curator的使用

    开源zk客户端-Curator 创建会话: RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3); CuratorFramewor ...

  4. zookeeper开源客户端curator

    zookeeper的原生api相对来说比较繁琐,比如:对节点添加监听事件,当监听触发后,我们需要再次手动添加监听,否则监听只生效一次:再比如,断线重连也需要我们手动代码来判断处理等等.对于curato ...

  5. Zookeeper开源客户端Curator之事件监听详解

    Curator对Zookeeper典型场景之事件监听进行封装,提供了使用参考.这篇博文笔者带领大家了解一下Curator的实现方式. 引入依赖 对于Curator封装Zookeeper的典型场景使用都 ...

  6. zookeeper生产最广泛使用java客户端curator介绍及其它客户端比较

    关于zookeeper的原理解析,可以参见zookeeper核心原理详解,本文所述大多数实践基于对zookeeper原理的首先理解. Curator是Netflix公司开源的一个Zookeeper客户 ...

  7. 【分布式】Zookeeper使用--开源客户端

    一.前言 上一篇博客已经介绍了如何使用Zookeeper提供的原生态Java API进行操作,本篇博文主要讲解如何通过开源客户端来进行操作. 二.ZkClient ZkClient是在Zookeepe ...

  8. Zookeeper客户端Curator使用详解

    Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBoot.Curator.Bootstrap写了一个可视化的Web应用: zookeep ...

  9. 7.5 zookeeper客户端curator的基本使用 + zkui

    使用zookeeper原生API实现一些复杂的东西比较麻烦.所以,出现了两款比较好的开源客户端,对zookeeper的原生API进行了包装:zkClient和curator.后者是Netflix出版的 ...

随机推荐

  1. JEECMS-自定义标签[list]

    1.自定义标签类 import static cn.com.yhxl.common.web.freemarker.DirectiveUtils.OUT_LIST; import static free ...

  2. 乐观锁--CAS

    悲观锁与乐观锁的区别 悲观锁会把整个对象加锁占为已有后才去做操作,Java中的Synchronized属于悲观锁.悲观锁有一个明显的缺点就是:它不管数据存不存在竞争都加锁,随着并发量增加,且如果锁的时 ...

  3. 单个SWF文件loading加载详解(转)

    通过带宽查看器,可以看到SWF中每帧所占带宽状况.另外,我们还可以在Flash发布设置中,选择生成体积报告. 勾选这一项之后,发布flash时,会自动在fla目录中生成一个名为”文件名 Report. ...

  4. cocos2d(3.0)一些基础的东西

    1.创建项目后环境配置: 附加文件夹:加入 $(EngineRoot) $(EngineRoot)cocos $(EngineRoot)cocos\editor-support $(EngineRoo ...

  5. Android学习笔记之百度地图

    步行路线搜索及RouteOverlay 方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walki ...

  6. 赵雅智_Fragment生命周期

    官网帮助文档链接:  http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...

  7. qt 获取系统磁盘空间大小

    quint64 getDiskFreeSpace(QString driver) { LPCWSTR lpcwstrDriver=(LPCWSTR)driver.utf16(); ULARGE_INT ...

  8. java_ExecutorService, CompletionService - 有返回值并行工作方式

    package com.demo.test3; import java.util.ArrayList; import java.util.List; import java.util.Random; ...

  9. 基础知识 - Rabin-Karp 算法

    Rabin-Karp 算法(字符串快速查找) Go 语言的 strings 包(strings.go)中用到了 Rabin-Karp 算法.Rabin-Karp 算法是基于这样的思路:即把字符串看作是 ...

  10. 1、netlink 连接器 通信机制

    使用netlink之前,先参考一下资料:http://www.ibm.com/developerworks/cn/linux/l-connector/ netlink通信机制介绍:资料来源 linux ...