单机模式
下载zookeeper的包
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
解压、分解归档包
gzip -d zookeeper-3.4.9.tar.gz
tar -xf zookeeper-3.4.9.tar
修改配置文件
查看下默认的配置文件,基本符合本次测试要求。将zoo_sample.cfg
拷贝为zoo.cfg
:
cp zoo_sample.cfg zoo.cfg
zoo.cfg
配置的简单解释:
[root@localhost bin]# cat /usr/local/zookeeper-3.4.9/conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000 # zk最小的单位时间,其他很多时间参数是此单位时间的n倍
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 # Leader服务器等待Follow服务器启动,完成数据同步的时间。这里配置为10表示单位时间的10倍
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 # Leader服务器与Follow服务器心跳检测的最大无响应时间
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/var/zookeeper # 快照文件存储目录
# 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
启动
[root@localhost bin]# sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看启动日志
[root@localhost bin]# less zookeeper.out
测试是否成功
[root@localhost bin]# telnet 127.0.0.1 2181
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stat
Zookeeper version: 3.4.9-1757313, built on 08/23/2016 06:50 GMT
Clients:
/127.0.0.1:42587[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: standalone
Node count: 4
Connection closed by foreign host.
关闭
[root@localhost local]# sh /usr/local/zookeeper-3.4.9/bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
单机模式(Windows)
如果我们在Windows下开发,最方便地是用Windows版本的ZooKeeper,安装起来非常方便。
下载
下载的版本与Linux版本一致的,并没有就不同的平台下载不同的包。
下载后就解压吧。
修改配置
在ZooKeeper的conf目录下,拷贝一份zoo_sample.cfg
文件,命名为zoo.cfg
,修改dataDir
的值为Windows的路径格式,比如dataDir=d:/tmp/zookeeper
。
启动
在ZooKeeper的bin目录下,运行zkServer.cmd
即运行ZooKeeper了,默认端口为2181。
集群模式
集群模式,最少需要3个节点。
修改配置
我们沿用基础配置,在zoo.cfg后面追加各节点的地址:
server.1=192.168.1.101:2888:3888
server.2=192.168.1.102:2889:3889
server.3=192.168.1.103:2890:3890
这里的端口我都分离开来了,因为我的其中两个虚拟系统安装在同一物理机器中,为了避免冲突。
拷贝文件到其他节点
将修改好的完整的zookeeper远程拷贝到另外两个节点:
scp -r /usr/local/zookeeper-3.4.9-cluster/ root@192.168.1.102:/usr/local/
scp -r /usr/local/zookeeper-3.4.9-cluster/ root@192.168.1.103:/usr/local/
创建myid标识节点
需要在各个节点的数据目录中用一个叫myid的文件标识自己的节点ID,myid的内容只有一个ID,对照zoo.cfg的配置,这里应该分别是1、2、3。
各个节点分别执行以下3条指令(一个节点一条):echo "1" > /var/zookeeper/myid
、echo "2" > /var/zookeeper/myid
、echo "3" > /var/zookeeper/myid
。
启动
sh /usr/local/zookeeper-3.4.9-cluster/bin/zkServer.sh start
测试是否成功
[root@localhost local]# telnet 127.0.0.1 2181
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stat
Zookeeper version: 3.4.9-1757313, built on 08/23/2016 06:50 GMT
Clients:
/127.0.0.1:46009[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 1
Sent: 0
Connections: 1
Outstanding: 0
Zxid: 0x300000000
Mode: leader
Node count: 4
Connection closed by foreign host.
可能遇到的问题
- 端口的冲突
- 防火墙拦截了
- myid没配置或配置不正确
客户端命令
客户端连接命令
连接命令:/opt/zookeeper-3.4.9/bin/zkCli.sh -server 127.0.0.1:2181
进入客户端命令行模式。
查看指定路径下的节点
[zk: 127.0.0.1:2181(CONNECTED) 0] ls /
[my-elastic-job-nn, zookeeper]
其中/zookeeper
是保留的节点,而my-elastic-job-nn
是我创建的节点。
查看节点的信息
[zk: 127.0.0.1:2181(CONNECTED) 7] get /my-elastic-job-nn/mySimpleJob/config
{"jobName":"mySimpleJob","jobClass":"com.nicchagil.MySimpleJob","jobType":"SIMPLE","cron":"0/5 * * * * ?","shardingTotalCount":2,"shardingItemParameters":"0\u003dX,1\u003dY","jobParameter":"myParameter\u003d123","failover":false,"misfire":true,"description":"","jobProperties":{"job_exception_handler":"com.dangdang.ddframe.job.executor.handler.impl.DefaultJobExceptionHandler","executor_service_handler":"com.dangdang.ddframe.job.executor.handler.impl.DefaultExecutorServiceHandler"},"monitorExecution":true,"maxTimeDiffSeconds":-1,"monitorPort":9999,"jobShardingStrategyClass":"","reconcileIntervalMinutes":-1,"disabled":false,"overwrite":true}
cZxid = 0x5
ctime = Thu Aug 03 08:25:46 PDT 2017
mZxid = 0x1a2b
mtime = Sat Aug 05 09:47:41 PDT 2017
pZxid = 0x5
cversion = 0
dataVersion = 20
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 646
numChildren = 0
输入结果的JSON是节点的内容,后面分别是节点的信息:
- cZxid,创建节点的事务ID
- ctime,创建节点的时间
- mZxid,最后更新节点的事务ID
- mtime,节点的最后更新时间
- pZxid,最后更新
当前节点子节点列表
的事务ID - cversion,TODO
- dataVersion,TODO
- aclVersion,节点的ACL变更版本
- ephemeralOwner,TODO
- dataLength,节点内容的长度
- numChildren,子节点的个数
创建节点
创建一个持久节点
[zk: 127.0.0.1:2181(CONNECTED) 7] create /my-data 123456
Created /my-data
创建一个临时节点:
[zk: 127.0.0.1:2181(CONNECTED) 0] create -e /my-extemporaneous-node 123456
Created /my-extemporaneous-node
[zk: 127.0.0.1:2181(CONNECTED) 1] get /my-extemporaneous-node
123456
cZxid = 0x1a58
ctime = Sat Aug 05 10:34:35 PDT 2017
mZxid = 0x1a58
mtime = Sat Aug 05 10:34:35 PDT 2017
pZxid = 0x1a58
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x15da922d510000b
dataLength = 6
numChildren = 0
创建顺序的节点:
[zk: 127.0.0.1:2181(CONNECTED) 2] create -s /my-sequential-node 123456
Created /my-sequential-node0000000005
设置节点的值
[zk: 127.0.0.1:2181(CONNECTED) 7] set /my-data 789
cZxid = 0x1a5c
ctime = Sat Aug 05 10:37:27 PDT 2017
mZxid = 0x1a5d
mtime = Sat Aug 05 10:37:51 PDT 2017
pZxid = 0x1a5c
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
删除节点
[zk: 127.0.0.1:2181(CONNECTED) 4] delete /my-data
级联删除节点
如果节点有子节点,通过delete无法删除,可通过rmr删除
[zk: 127.0.0.1:2181(CONNECTED) 4] rmr /my-data