安装与部署
- 配置过程相当简单。集群模式部署:
-
wget http://www-us.apache.org/dist/zookeeper/stable/zookeeper-3.4.10.tar.gz
-
tar -xzf zookeeper-3.4.10.tar.gz
-
cp zoo_sample.cfg zoo.cfg
- 修改zoo.cfg如下
- mkdir `dataDir`
- 在dataDir下建立文件myid。里面对应的是该host的myid。
-
- 配置文件 zoo.cfg
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. 该目录下需要myid文件 dataDir=/root/wttttt/zookeeper/zkDataDir # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 server.1=host98:2888:3888 server.2=host99:2888:2888 server.3=host101:2888:3888
- 启动成功后,验证三台机器的情况:
- zk的bin目录下有一些可执行脚本
- zkCleanup: 清理zk历史数据,包括事务日志文件和快照数据文件
- zkCli: zk的一个简易客户端
- zkEnv: 设置zk的环境变量
- zkServer: zk服务器的启动、停止和重启脚本
客户端脚本
- 启动客户端sh zkCli.sh。看到输出: (sh zkCli.sh -server ip:port)
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
则说明client已经成功连接上本地zk server(启动时没有指定zk地址则默认连接本地zk server)了。 - 创建:create [-s] [-e] path data acl [-s 持久节点] [-e 临时节点]
如下是在根目录下创建了一个叫做/zk-test的临时节点,节点的数据内容是“123”,默认不做任何权限控制。
[zk: localhost:2181(CONNECTED) 0] create -e /zk-test 123 Created /zk-test
- 读取:
- ls命令,可以列出zk指定节点下的所有子节点 ls path [watch]
[zk: localhost:2181(CONNECTED) 2] ls / [zk-test, zookeeper] [zk: localhost:2181(CONNECTED) 3] ls /zk-test []
- get命令,可以获取zk指定节点的数据内容和属性信息。 get path [watch]
[zk: localhost:2181(CONNECTED) 4] get / cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2 [zk: localhost:2181(CONNECTED) 5] get /zk-test 123 cZxid = 0x100000002 ctime = Sun Sep 24 15:29:48 CST 2017 mZxid = 0x100000002 mtime = Sun Sep 24 15:29:48 CST 2017 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x25eb2c360850000 dataLength = 3 numChildren = 0
- ls命令,可以列出zk指定节点下的所有子节点 ls path [watch]
- 更新:set命令,可以更新指定节点的数据内容 set path data [version]
[zk: localhost:2181(CONNECTED) 6] set /zk-test 1234 cZxid = 0x100000002 ctime = Sun Sep 24 15:29:48 CST 2017 mZxid = 0x100000003 mtime = Sun Sep 24 15:35:45 CST 2017 pZxid = 0x100000002 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x25eb2c360850000 dataLength = 4 numChildren = 0
可以看到更新之后,节点的mZxid加一了。dataVersion也从0变成1了。
- 删除:delete命令,可以删除zk上指定的节点。 delete path [version]
Java客户端API的使用
- TBD...