一. 下载解压文件
安装地址: http://www.apache.org/dyn/closer.cgi/zookeeper/
• Windows 采用 winzip
或者 winwar
都行
• Linux采用 tar –zxvf zookeeper-3.4.6.tar.gz
解压
配置境变量
• ZOOKEEPER_HOME=/home/zookeeper-3.5.3-beta
• PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin
待下载后安装在指定的目录下
比如我安装在 windows 目录F:\ZooKeeper\zookeeper-3.5.3-beta
下
二.针对几个目录进行说明
bin
- .sh 为 linux 环境
- .cmd 为 windows 环境
conf
- ample.cfg 为样例配置文件,需要修改自己的名称,一般为 zoo.cfg
- Log4j.properties 为日志配置文件
Contrib
Contrib
为一些常用于操作 zk 的工具包
lib
zk 依赖的某些包
Recipes
zk 某些用法的代码实例
Dist-maven
maven 编译后的发布目录
三.配置启动 zk
修改配置文件名
把conf
目录下的zoo_sample.cfg
修改为 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.
dataDir=F:\\ENV\\Distribution\\ZooKeeper\\zookeeper-3.5.3-beta\\data
dataLogDir=F:\\ENV\\Distribution\\ZooKeeper\\zookeeper-3.5.3-beta\\log
# 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
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
简要介绍 zoo.cfg 文件
tickTime
:默认3000ms,作一种基本单元,用它的倍数来表示系统内部的时间间隔配置,比如
•2*tickTime
是客户端会话的超时时间
•1*tickTime
是客户端 zk 服器端的心跳时dataDir
:无默认配置,必配置;用于配置储快照文件的目录,如果没有配置dataLogDir
,那么事务日志也会储在目录clientPort
:zk 的运行端口,默认是2181
启动
Windows:直接双击 zkServer.cmd
Linux: zkServer.sh start
已经启动 zookeeper 的单机模式
四.zookeeper 的基础操作
现在为 zookeeper 单机模式下
客户端连接
1. 客户端命令行
- Linux:执行 bin/zkCli.sh
- Windows: 点击 bin/zkCli.cmd
- 不带任何参数默认连接到 localhost:2181
- zkCli.cmd – server ip:port 连接到指定的服器地址
2. 查看命令行帮助
- 行入help查看所有的命令列表及语法
- 实际上,输入任何字符回车都会提示班助信息
3.读取
ls path [watch]
+ Path 表示指定数据节点的节点路径
+ 列出指定节点的所有子节点
+ 能查看第一级的所有子节点
+ 刚安装时 ls /
有默认的 zookeeper 保留节点
+ Watch
表示监听 path 的子节点的变化
4.创建
create [-s] [-e] path data acl
+ 创建 zookeeper 节点
+ -s
或者 -e
表示建的是顺序或临时节点,不加默认创建的是持久节点
+ Path 为节点的全路径,zk 没有相对节点的表示方式
+ Data 当前节点内存储的数据
+ Acl 用来进行权限控制,缺省情况下不做任何权限控制
5.读取
get path [watch]
- 获取指定节点的数据内容和属性信息
- Path 表示指定数据节点的节点路径
下图中
- 123 为节点内的数据
cZxid
创建该节点事务 idMzxid
最后一次更新该节点的事务 idMime
为最后一次更新时间Cversion
子节点版本dataVersion
数据版本aclVersion
访问控制版本
6.更新
set path data [version]
- 更新指定节点的数据内容
- Path 表示被更新的节点路径
- data 为更新的数据
- Version 为指定被更新的数据版本,一般不指定,如果数据版本已经更新,则指定旧版本时会报错
7. 删除
delete path [version]
- 删除指定节点
- Path 表示被删除的节点
- Version 为指定被删除的数据版本,一般指定,如果 数据版本经更新,则指定旧版本时会报错