zoo.cfg
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# 存储快照位置和事务日志,一般分开存储# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/tmp/zookeeper# the port at which the clients will connectclientPort=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
基本命令
# 查看结点[zk: 127.0.0.1:2181(CONNECTED) 0] ls /[zookeeper]# 创建一个结点[zk: 127.0.0.1:2181(CONNECTED) 1] create /test "content"[zookeeper, test]# 获取结点内容[zk: 127.0.0.1:2181(CONNECTED) 2] get /testtestcZxid = 0x4ctime = Tue Feb 18 11:28:15 CST 2020mZxid = 0x4mtime = Tue Feb 18 11:28:15 CST 2020pZxid = 0x4cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 0# 删除结点[zk: 127.0.0.1:2181(CONNECTED) 3] delete /test
集群配置
如果是三台 zookeeper 组成的集群
1、配置三个 zoo.cfg
**
指定不同的 clientPort 端口号,指定每台机器不同的 dataDir 位置
# zoo1.cfgtickTime=2000initLimit=10syncLimit=5dataDir=/home/eric/zookeeper-3.4.14/data/data_1clientPort=2181server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889
# zoo2.cfgtickTime=2000initLimit=10syncLimit=5dataDir=/home/eric/zookeeper-3.4.14/data/data_2clientPort=2182server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889
# zoo3.cfgtickTime=2000initLimit=10syncLimit=5dataDir=/home/eric/zookeeper-3.4.14/data/data_3clientPort=2183server.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889
2、建立 myid 文件
分别在不同的 dataDir 下建立 myid 文件,并指定不同的值
$ echo "1" > data_1/myid$ echo "2" > data_2/myid$ echo "3" > data_3/myid
3、分别启动每个服务
由于是 3 台机器组成的集群,起始只要有两台启动成功了,客户端就可以进行连接了
$ zkServer.sh start ~/zookeeper-3.4.14/conf/zoo1.cfg$ zkServer.sh start ~/zookeeper-3.4.14/conf/zoo2.cfg$ zkServer.sh start ~/zookeeper-3.4.14/conf/zoo3.cfg
由于是依次启动 zoo1.cfg,zoo2.cfg,zoo3.cfg 所以 leader 就是 zoo2 这台服务器了
4、添加 observer
observer 配置
**
# zoo-observer.cfgtickTime=2000initLimit=10syncLimit=5dataDir=/home/eric/zookeeper-3.4.14/data/data_obclientPort=2184peerType=observerserver.1=localhost:2887:3887server.2=localhost:2888:3888server.3=localhost:2889:3889server.4=localhost:2890:3890:observer
# 写入标识符到 myidecho 4 > /home/eric/zookeeper-3.4.14/data/data_ob/myid
follower 配置(修改)
# zoo1.cfg 添加server.4=localhost:2890:3890:observer
# zoo2.cfg 添加server.4=localhost:2890:3890:observer
# zoo3.cfg 添加server.4=localhost:2890:3890:observer
启动 observer
$ zkServer.sh status ~/zookeeper-3.4.14/conf/zoo-observer.cfgZooKeeper JMX enabled by defaultUsing config: /home/eric/zookeeper-3.4.14/conf/zoo-observer.cfgMode: observer
