1、修改配置文件
cp zoo_sample.cfg zoo.cfg
vim 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=/tmp/zookeeper/data
dataLogDir=/tmp/zookeeper/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
server.1=192.100.3.79:2888:3888
server.2=192.100.3.80:2888:3888
server.3=192.100.3.81:2888:3888
并在dataDir(
/tmp/zookeeper/data)下创建myid,值为server.1中的1.2.3
server.A=192.100.3.79:2888:3888
server.B=192.100.3.80:2888:3888
server.C=192.100.3.81:2888:3888
ABC为集群的id,保存在myid中,2888集群的leader的选举端口,3888:服务器之间通信端口
注意:端口需要放开,或者直接关闭防火墙
2、启动
# 启动
bin/zkServer.sh start
# 查看
jps
[root@node-1 zookeeper-3.4.9]# jps
27227 Jps
27199 QuorumPeerMain
脚本:
免密登录
#将100机器的公钥复制到100 101 102 机器上的authorized_keys中
ssh-copy-id 192.168.189.100
ssh-copy-id 192.168.189.101
ssh-copy-id 192.168.189.102
配置免密登录后,启动集群脚本
#!/bin/bash
server="192.168.189.100 192.168.189.101 192.168.189.102"
for ip in $server
do
ssh root@$ip "/usr/local/zookeeper-3.4.12/bin/zkServer.sh start"
done
3、相关命令
3.1、查看集群状态
[root@node-1 zookeeper-3.4.9]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: follower
3.2、客户端连接
bin/zkCli.sh -server node-2:2181
# 默认连接本地
bin/zkCli.sh
3.3、创建节点
# 永久节点
[zk: node-2:2181(CONNECTED) 2] create /hello world
Created /hello
# 临时节点
[zk: node-2:2181(CONNECTED) 3] create -e /tmp world
Created /tmp
# 有序永久节点
[zk: node-2:2181(CONNECTED) 1] create -s /hello2 worle
Created /hello20000000002
# 有序临时节点
[zk: node-2:2181(CONNECTED) 2] create -s -e /tmp worle
Created /tmp0000000003
# 临时节点当会话结束时 会删除
3.4、获取/设置数据
# 设置数据
[zk: node-2:2181(CONNECTED) 4] set /hello data
cZxid = 0x100000006
ctime = Wed Jun 30 18:02:00 CST 2021
mZxid = 0x10000000e
mtime = Wed Jun 30 18:06:08 CST 2021
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
# 获取数据
[zk: node-2:2181(CONNECTED) 5] get /hello
data
cZxid = 0x100000006
ctime = Wed Jun 30 18:02:00 CST 2021
mZxid = 0x10000000e
mtime = Wed Jun 30 18:06:08 CST 2021
pZxid = 0x100000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
# 子节点获取
[zk: node-2:2181(CONNECTED) 6] create /hello/aaa 123
Created /hello/aaa
[zk: node-2:2181(CONNECTED) 7] get /hello/aaa 123
123
cZxid = 0x10000000f
ctime = Wed Jun 30 18:07:23 CST 2021
mZxid = 0x10000000f
mtime = Wed Jun 30 18:07:23 CST 2021
pZxid = 0x10000000f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
3.5、添加监听
[zk: node-2:2181(CONNECTED) 11] stat /hello watch
cZxid = 0x100000006
ctime = Wed Jun 30 18:02:00 CST 2021
mZxid = 0x10000000e
mtime = Wed Jun 30 18:06:08 CST 2021
pZxid = 0x10000000f
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
[zk: node-2:2181(CONNECTED) 12]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/hello