1、修改配置文件
cp zoo_sample.cfg zoo.cfgvim 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# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/tmp/zookeeper/datadataLogDir=/tmp/zookeeper/log# 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=1server.1=192.100.3.79:2888:3888server.2=192.100.3.80:2888:3888server.3=192.100.3.81:2888:3888
并在dataDir(/tmp/zookeeper/data)下创建myid,值为server.1中的1.2.3
server.A=192.100.3.79:2888:3888server.B=192.100.3.80:2888:3888server.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]# jps27227 Jps27199 QuorumPeerMain
脚本:
免密登录
#将100机器的公钥复制到100 101 102 机器上的authorized_keys中ssh-copy-id 192.168.189.100ssh-copy-id 192.168.189.101ssh-copy-id 192.168.189.102
配置免密登录后,启动集群脚本
#!/bin/bashserver="192.168.189.100 192.168.189.101 192.168.189.102"for ip in $serverdossh 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 statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.9/bin/../conf/zoo.cfgMode: follower
3.2、客户端连接
bin/zkCli.sh -server node-2:2181# 默认连接本地bin/zkCli.sh
3.3、创建节点
# 永久节点[zk: node-2:2181(CONNECTED) 2] create /hello worldCreated /hello# 临时节点[zk: node-2:2181(CONNECTED) 3] create -e /tmp worldCreated /tmp# 有序永久节点[zk: node-2:2181(CONNECTED) 1] create -s /hello2 worleCreated /hello20000000002# 有序临时节点[zk: node-2:2181(CONNECTED) 2] create -s -e /tmp worleCreated /tmp0000000003# 临时节点当会话结束时 会删除
3.4、获取/设置数据
# 设置数据[zk: node-2:2181(CONNECTED) 4] set /hello datacZxid = 0x100000006ctime = Wed Jun 30 18:02:00 CST 2021mZxid = 0x10000000emtime = Wed Jun 30 18:06:08 CST 2021pZxid = 0x100000006cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 0# 获取数据[zk: node-2:2181(CONNECTED) 5] get /hellodatacZxid = 0x100000006ctime = Wed Jun 30 18:02:00 CST 2021mZxid = 0x10000000emtime = Wed Jun 30 18:06:08 CST 2021pZxid = 0x100000006cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 0# 子节点获取[zk: node-2:2181(CONNECTED) 6] create /hello/aaa 123Created /hello/aaa[zk: node-2:2181(CONNECTED) 7] get /hello/aaa 123123cZxid = 0x10000000fctime = Wed Jun 30 18:07:23 CST 2021mZxid = 0x10000000fmtime = Wed Jun 30 18:07:23 CST 2021pZxid = 0x10000000fcversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 3numChildren = 0
3.5、添加监听
[zk: node-2:2181(CONNECTED) 11] stat /hello watchcZxid = 0x100000006ctime = Wed Jun 30 18:02:00 CST 2021mZxid = 0x10000000emtime = Wed Jun 30 18:06:08 CST 2021pZxid = 0x10000000fcversion = 1dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 1[zk: node-2:2181(CONNECTED) 12]WATCHER::WatchedEvent state:SyncConnected type:NodeDataChanged path:/hello
