Zookeeper 入门手册Word文档格式.docx

上传人:b****3 文档编号:7960626 上传时间:2023-05-09 格式:DOCX 页数:27 大小:189.34KB
下载 相关 举报
Zookeeper 入门手册Word文档格式.docx_第1页
第1页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第2页
第2页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第3页
第3页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第4页
第4页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第5页
第5页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第6页
第6页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第7页
第7页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第8页
第8页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第9页
第9页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第10页
第10页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第11页
第11页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第12页
第12页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第13页
第13页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第14页
第14页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第15页
第15页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第16页
第16页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第17页
第17页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第18页
第18页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第19页
第19页 / 共27页
Zookeeper 入门手册Word文档格式.docx_第20页
第20页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Zookeeper 入门手册Word文档格式.docx

《Zookeeper 入门手册Word文档格式.docx》由会员分享,可在线阅读,更多相关《Zookeeper 入门手册Word文档格式.docx(27页珍藏版)》请在冰点文库上搜索。

Zookeeper 入门手册Word文档格式.docx

通过简单的例子程序循序渐进地学习ZOOKEEPER的开发应用。

本文档由产品组维护,供各项目组使用。

目录

编写说明2

目录3

1.1.本文目的4

1.2.阅读建议4

2.起步4

2.1.DEMO00搭建ZOOKEEPER集群服务器4

2.1.1.功能概述4

2.1.2.开发过程4

2.1.3.执行效果4

2.2.DEMO01ZOOKEEPER基本操作5

2.2.1.功能概述5

2.2.2.开发过程5

2.2.3.执行效果16

2.3.DEMO002Zookeeper临时节点16

2.3.1.功能描述16

2.3.2.开发过程16

2.3.3.执行效果18

2.4.DEMO003监控Zookeeper节点18

2.4.1.功能描述18

2.4.2.开发过程18

2.4.3.执行效果23

前言

1.1.本文目的

本文是Zookeeper开发的入门手册,将会在一个最简单的例子入手,为读者逐渐深入地介绍Zookeeper上的应用开发。

1.2.阅读建议

本文假定读者是一个Unix系统下的开发人员,有一定的应用编程经验,熟悉J2EE的应用开发。

请参考提供的对应DEMO包,对应下面的章节,一步一步对照开发。

2.起步

本章的目的在于,通过循序渐进的练习,让初学者了解ZOOKEEPER的基本概念,掌握开发方法,具备实际应用的能力。

2.1.DEMO00搭建ZOOKEEPER集群服务器

2.1.1.功能概述

在一台机器上搭建一个由3个Server组成的ZOOKEEPER伪集群服务器,可以进行集群服务的启动、停止。

2.1.2.开发过程

1、参考《ZOOKEEPER介绍v1.0.doc》,搭建一个伪集群服务器。

2.1.3.执行效果

在ZOOKEEPER集群的每个服务器的../zookeeper/bin目录下,运行shzkServer.shstart,当出现以下界面说明ZOOKEEPER启动成功。

zookeeper/bin目录下,运行zkCli.sh–server172.16.4.224:

2181(IP、端口与实际配置一致)。

当出现以下界面说明已经成功连接上ZOOKEEPER客户端。

2.2.DEMO01ZOOKEEPER基本操作

2.2.1.功能概述

熟悉Zookeeper的API,练习Zookeeper的增、删、改、查操作。

2.2.2.开发过程

1、Eclipse新建一个Java工程,导入Zookeeper依赖的jar包

2、编写增、删、改、查的代码。

packagezookeeper;

importjava.io.IOException;

importjava.util.List;

importorg.apache.zookeeper.KeeperException;

/**

*@类名ZkServer.java

*@描述:

*zookeeper的操作封装接口,实现了常用的操作

*创建、销毁、写入、修改、查询等。

*@作者梅光弦

*@创建时间2012-12-4下午02:

49:

58

*@版本v1.0

*/

publicinterfaceZkServer{

/**

*

*@paramaddress

*@paramserverName

*@throwsIOException

publicvoidinit(Stringaddress)throwsIOException;

*@throwsInterruptedException

publicvoiddestroy()throwsInterruptedException;

*@parampath

*@return

*@throwsKeeperException

publicList<

String>

getChilds(Stringpath)throwsKeeperException,InterruptedException;

publicStringgetData(Stringpath)throwsKeeperException,InterruptedException;

*@paramdata

publicvoidchangeData(Stringpath,Stringdata)throwsKeeperException,InterruptedException;

publicvoidappendTempNode(Stringpath,Stringdata)throwsKeeperException,InterruptedException;

publicvoidappendPresistentNode(Stringpath,Stringdata)throwsKeeperException,InterruptedException;

publicvoiddelNode(Stringpath)throwsKeeperException,InterruptedException;

publicbooleanexist(Stringpath)throwsKeeperException,InterruptedException;

}

importjava.util.HashMap;

importorg.apache.zookeeper.CreateMode;

importorg.apache.zookeeper.WatchedEvent;

importorg.apache.zookeeper.Watcher;

importorg.apache.zookeeper.ZooKeeper;

importorg.apache.zookeeper.ZooDefs.Ids;

*@类名ZkServerImpl.java

*TODO

53:

47

publicclassZkServerImplimplementsZkServer,Watcher{

privateZooKeeperzk=null;

publicvoidappendPresistentNode(Stringpath,Stringdata)

throwsKeeperException,InterruptedException{

if(zk!

=null){

//创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失)

/**

*创建一个给定的目录节点path,并给它设置数据,

*CreateMode标识有四种形式的目录节点,分别是

*PERSISTENT:

持久化目录节点,这个目录节点存储的数据不会丢失;

*PERSISTENT_SEQUENTIAL:

顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加1,然后返回给客户端已经成功创建的目录节点名;

*EPHEMERAL:

临时目录节点,一旦创建这个节点的客户端与服务器端口也就是session超时,这种节点会被自动删除;

*EPHEMERAL_SEQUENTIAL:

临时自动编号节点

*/

zk.create(path,data.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

}

}

publicvoidappendTempNode(Stringpath,Stringdata)

*/

zk.create(path,data.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);

}

publicvoidchangeData(Stringpath,Stringdata)throwsKeeperException,

InterruptedException{

//修改节点下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉

zk.setData(path,data.getBytes(),-1);

publicvoiddelNode(Stringpath)throwsKeeperException,

if(zk!

=null){

zk.delete(path,-1);

}

publicvoiddestroy()throwsInterruptedException{

zk.close();

publicbooleanexist(Stringpath)throwsKeeperException,

returnzk.exists(path,true)!

=null;

returnfalse;

getChilds(Stringpath)throwsKeeperException,

returnzk.getChildren(path,true);

returnnull;

publicStringgetData(Stringpath)throwsKeeperException,

byte[]b=zk.getData(path,true,null);

returnnewString(b);

returnnull;

publicvoidinit(Stringaddress)throwsIOException{

zk=newZooKeeper(address,50000,this);

publicvoidprocess(WatchedEventarg0){

packagetest;

importzookeeper.ZkServer;

importzookeeper.ZkServerImpl;

*@类名ZkTest.java

*@创建时间2012-12-4下午03:

05:

02

publicclassZkTest{

privatestaticfinalStringhosts="

172.16.5.224:

2181,172.16.5.224:

2182,172.16.5.224:

2183"

;

*创建永恒节点

publicvoidtestCreatePersistentNodes(){

ZkServerzkServer=newZkServerImpl();

try{

zkServer.init(hosts);

Stringpath="

App1"

if(!

zkServer.exist(path)){

zkServer.appendPresistentNode(path,"

);

}

}catch(IOExceptione){

e.printStackTrace();

}catch(KeeperExceptione){

}catch(InterruptedExceptione){

*获取节点的数据

publicvoidtestGetNodeData(){

Stringtemp=zkServer.getData(path);

System.out.println(temp);

*修改节点的数据

publicvoidtestUpdateNodes(){

zkServer.changeData(path,"

App_changed"

*删除节点

publicvoidtestDelNodes(){

zkServer.delNode(path);

importjunit.framework.TestCase;

publicclassZkTestextendsTestCase{

/App1"

NodeApp1'

sDataisApp1"

if(zkServer.exist(path)){

sDataisApp1Changed"

}catch(KeeperExcept

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2