下载 tar包 解压

1、修改配置文件

cp zoo_sample.cfg zoo.cfg
vim zoo.cfg

  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/tmp/zookeeper/data
  13. dataLogDir=/tmp/zookeeper/log
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.1=192.100.3.79:2888:3888
  31. server.2=192.100.3.80:2888:3888
  32. server.3=192.100.3.81:2888:3888

并在dataDir(/tmp/zookeeper/data)下创建myid,值为server.1中的1.2.3

  1. server.A=192.100.3.79:2888:3888
  2. server.B=192.100.3.80:2888:3888
  3. server.C=192.100.3.81:2888:3888

ABC为集群的id,保存在myid中,2888集群的leader的选举端口,3888:服务器之间通信端口
注意:端口需要放开,或者直接关闭防火墙

2、启动

  1. # 启动
  2. bin/zkServer.sh start
  3. # 查看
  4. jps
  5. [root@node-1 zookeeper-3.4.9]# jps
  6. 27227 Jps
  7. 27199 QuorumPeerMain

脚本:
免密登录

  1. #将100机器的公钥复制到100 101 102 机器上的authorized_keys中
  2. ssh-copy-id 192.168.189.100
  3. ssh-copy-id 192.168.189.101
  4. ssh-copy-id 192.168.189.102

配置免密登录后,启动集群脚本

  1. #!/bin/bash
  2. server="192.168.189.100 192.168.189.101 192.168.189.102"
  3. for ip in $server
  4. do
  5. ssh root@$ip "/usr/local/zookeeper-3.4.12/bin/zkServer.sh start"
  6. done

3、相关命令

3.1、查看集群状态

  1. [root@node-1 zookeeper-3.4.9]# bin/zkServer.sh status
  2. ZooKeeper JMX enabled by default
  3. Using config: /usr/local/zookeeper-3.4.9/bin/../conf/zoo.cfg
  4. Mode: follower

3.2、客户端连接

  1. bin/zkCli.sh -server node-2:2181
  2. # 默认连接本地
  3. bin/zkCli.sh

3.3、创建节点

  1. # 永久节点
  2. [zk: node-2:2181(CONNECTED) 2] create /hello world
  3. Created /hello
  4. # 临时节点
  5. [zk: node-2:2181(CONNECTED) 3] create -e /tmp world
  6. Created /tmp
  7. # 有序永久节点
  8. [zk: node-2:2181(CONNECTED) 1] create -s /hello2 worle
  9. Created /hello20000000002
  10. # 有序临时节点
  11. [zk: node-2:2181(CONNECTED) 2] create -s -e /tmp worle
  12. Created /tmp0000000003
  13. # 临时节点当会话结束时 会删除

3.4、获取/设置数据

  1. # 设置数据
  2. [zk: node-2:2181(CONNECTED) 4] set /hello data
  3. cZxid = 0x100000006
  4. ctime = Wed Jun 30 18:02:00 CST 2021
  5. mZxid = 0x10000000e
  6. mtime = Wed Jun 30 18:06:08 CST 2021
  7. pZxid = 0x100000006
  8. cversion = 0
  9. dataVersion = 1
  10. aclVersion = 0
  11. ephemeralOwner = 0x0
  12. dataLength = 4
  13. numChildren = 0
  14. # 获取数据
  15. [zk: node-2:2181(CONNECTED) 5] get /hello
  16. data
  17. cZxid = 0x100000006
  18. ctime = Wed Jun 30 18:02:00 CST 2021
  19. mZxid = 0x10000000e
  20. mtime = Wed Jun 30 18:06:08 CST 2021
  21. pZxid = 0x100000006
  22. cversion = 0
  23. dataVersion = 1
  24. aclVersion = 0
  25. ephemeralOwner = 0x0
  26. dataLength = 4
  27. numChildren = 0
  28. # 子节点获取
  29. [zk: node-2:2181(CONNECTED) 6] create /hello/aaa 123
  30. Created /hello/aaa
  31. [zk: node-2:2181(CONNECTED) 7] get /hello/aaa 123
  32. 123
  33. cZxid = 0x10000000f
  34. ctime = Wed Jun 30 18:07:23 CST 2021
  35. mZxid = 0x10000000f
  36. mtime = Wed Jun 30 18:07:23 CST 2021
  37. pZxid = 0x10000000f
  38. cversion = 0
  39. dataVersion = 0
  40. aclVersion = 0
  41. ephemeralOwner = 0x0
  42. dataLength = 3
  43. numChildren = 0

3.5、添加监听

  1. [zk: node-2:2181(CONNECTED) 11] stat /hello watch
  2. cZxid = 0x100000006
  3. ctime = Wed Jun 30 18:02:00 CST 2021
  4. mZxid = 0x10000000e
  5. mtime = Wed Jun 30 18:06:08 CST 2021
  6. pZxid = 0x10000000f
  7. cversion = 1
  8. dataVersion = 1
  9. aclVersion = 0
  10. ephemeralOwner = 0x0
  11. dataLength = 4
  12. numChildren = 1
  13. [zk: node-2:2181(CONNECTED) 12]
  14. WATCHER::
  15. WatchedEvent state:SyncConnected type:NodeDataChanged path:/hello