Zookeeper入门
来源:互联网 发布:mysql外键约束怎么写 编辑:程序博客网 时间:2024/06/05 11:44
摘要: zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,由于产品升级拓展,pom等文件配置越来越复杂,因此zk派上用场
一.zookeeper简介
一款管理分布式应用程序的协调服务系统
二.zookeeper应用场景
网上也有很多介绍,可以参见 http://blog.csdn.net/xinguan1267/article/details/38422149
本文主要介绍基于java的客户端开发
三.基于JAVA客户端实战
3.1Client
// 创建一个与服务器的连接 需要(服务端的 ip+端口号)(session过期时间)(Watcher监听注册)
ZooKeeper zk = new ZooKeeper(“10.154.156.180:2181”,
3000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
System.out.println(“已经触发了” + event.getType() + “事件!”);
}
});
// 创建一个目录节点 /** * CreateMode: * PERSISTENT (持续的,相对于EPHEMERAL,不会随着client的断开而消失) * PERSISTENT_SEQUENTIAL(持久的且带顺序的) * EPHEMERAL (短暂的,生命周期依赖于client session) * EPHEMERAL_SEQUENTIAL (短暂的,带顺序的) */ zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); // 创建一个子目录节点 zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); System.out.println(new String(zk.getData("/testRootPath",false,null))); // 取出子目录节点列表 System.out.println(zk.getChildren("/testRootPath",true)); // 创建另外一个子目录节点 zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); System.out.println(zk.getChildren("/testRootPath",true)); // 修改子目录节点数据 zk.setData("/testRootPath/testChildPathOne","hahahahaha".getBytes(),-1); byte[] datas = zk.getData("/testRootPath/testChildPathOne", true, null); String str = new String(datas,"utf-8"); System.out.println(str); //删除整个子目录 -1代表version版本号,-1是删除所有版本 zk.delete("/testRootPath/testChildPathOne", -1); System.out.println(zk.getChildren("/testRootPath",true)); System.out.println(str);
部分例式 具体可参见官网api: http://zookeeper.apache.org/doc/r3.2.2/api/org/apache/zookeeper/ZooKeeper.html#create%28java.lang.String,%20byte%5B%5D,%20java.util.List,%20org.apache.zookeeper.CreateMode%29
3.2Curator
Curator框架提供了一套高级的API, 简化了ZooKeeper的操作。 它增加了很多使用ZooKeeper开发的特性,可以处理ZooKeeper集群复杂的连接管理和重试机制。 这些特性包括:
封装ZooKeeper client与ZooKeeper server之间的连接处理;
提供了一套Fluent风格的操作API;
提供ZooKeeper各种应用场景(recipe, 比如共享锁服务, 集群领导选举机制)的抽象封装.
关于Fluent风格可以查看我的一篇博客(建造者模式),在偶看es搜索源码时发现也使用了Fluent风格。
如下是使用Curator创建对象的方法
public static CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs) {
return CuratorFrameworkFactory.builder().connectString(connectionString) .retryPolicy(retryPolicy) .connectionTimeoutMs(connectionTimeoutMs) .sessionTimeoutMs(sessionTimeoutMs) .build();}
调用代码如下:
client = createWithOptions(“10.154.156.180:2181”, new ExponentialBackoffRetry(1000, 3), 1000, 1000);
client.start();
如果需要创建新目录节点 依然是Fluent风格
client.create().forPath(“/curator”, new byte[0]);
client.create().withMode(CreateMode.PERSISTENT).forPath(“/curator/childOne”, new byte[0]);
当然创建zk也可以不使用Fluent风格
public static CuratorFramework createSimple(String connectionString) {
ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
return CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
}
其他方法说明:
create(): 发起一个create操作. 可以组合其他方法 (比如mode 或background) 最后以forPath()方法结尾
delete(): 发起一个删除操作. 可以组合其他方法(version 或background) 最后以forPath()方法结尾
checkExists(): 发起一个检查ZNode 是否存在的操作. 可以组合其他方法(watch 或background) 最后以forPath()方法结尾
getData(): 发起一个获取ZNode数据的操作. 可以组合其他方法(watch, background 或get stat) 最后以forPath()方法结尾
setData(): 发起一个设置ZNode数据的操作. 可以组合其他方法(version 或background) 最后以forPath()方法结尾
getChildren(): 发起一个获取ZNode子节点的操作. 可以组合其他方法(watch, background 或get stat) 最后以forPath()方法结尾
inTransaction(): 发起一个ZooKeeper事务. 可以组合create, setData, check, 和/或delete 为一个操作, 然后commit() 提交
- zookeeper 入门
- zookeeper 入门
- Zookeeper入门
- Zookeeper入门
- Zookeeper入门
- zookeeper入门
- ZooKeeper入门
- zookeeper入门
- ZooKeeper入门
- Zookeeper入门
- zookeeper入门
- zookeeper入门
- zookeeper入门
- Zookeeper-入门
- zookeeper入门
- zookeeper 入门
- zookeeper 入门
- Zookeeper入门
- Conn java版本
- 求知过程之json
- wiershark 过滤规则
- DateUtil java 版本
- 单例模式的内存释放
- Zookeeper入门
- 支付同步和异步接口,你还有疑惑么?
- 四元数的形象理解
- C++实验四-输出星状图、三色球问题
- StringUtils.isEmpty和StringUtils.isBlank用法和区别
- 5款最好的安卓界面设计工具推荐
- 数据库三范式总结
- mybatis中 #{}个${}问题
- ubuntu 14.04配置google拼音输入法