在下载的zookeeper目录里创建3个文件,zk1,zk2,zk3,用意存放每个集群的数据文件。
并在三个目录下创建data和logs目录
将conf目录下的zoo-sample.cfg 更改为zoo1.cfg , zoo2.cfg ,zoo3.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. # 内存数据库保存的模糊快照的目录,如果某个服务器为集群中的一台,则id文件也保存在该目录下 # 快照异步写入,不会阻塞 dataDir= ../zk1/data dataLogDir= ../zk1/logs # the port at which the clients will connect # TCP监听端口 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.myid=IP:PORT1;PORT2 #PORT1:服务器与集群中的LEADER服务器交换信息的端口 #PORT2:万一集群中的LEADER挂了,需要一个端口来重新进行宣讲 选出新的LEADER server.1=127.0.0.1:2881:3881 server.2=127.0.0.1:2882:3882 server.3=127.0.0.1:2883:3883
三个文件的区别是这三个配置
zoo1.cfg
dataDir= ../zk1/data
dataLogDir= ../zk1/logs
clientPort=2181
zoo2.cfg
dataDir= ../zk2/data
dataLogDir= ../zk2/logs
clientPort=2182
zoo3.cfg
dataDir= ../zk3/data dataLogDir= ../zk3/logs clientPort=2183
在每个目录下的data目录里创建 myid文件,文件内容分别为1,2,3.
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh start ../conf/zoo1.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo1.cfg Starting zookeeper ... STARTED lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh start ../conf/zoo2.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo2.cfg Starting zookeeper ... STARTED lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh start ../conf/zoo3.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo3.cfg Starting zookeeper ... STARTED
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh status ../conf/zoo1.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo1.cfg Mode: follower
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh status ../conf/zoo2.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo2.cfg Mode: leader
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh status ../conf/zoo3.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo3.cfg Mode: follower
可以看到,ZK2选举成为了leader,另外两个为follower。
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkCli.sh -server 127.0.0.1:2181
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkCli.sh -server 127.0.0.1:2182
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkCli.sh -server 127.0.0.1:2183
分别连接三个集群节点
在任何一个集群节点进行操作,其他集群也会同步更新。
在zk2创建节点
[zk: 127.0.0.1:2182(CONNECTED) 1] create /test1 ""
Created /test1
在zk1查看节点
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper, test1]
原文:https://www.cnblogs.com/lgjlife/p/10546014.html