安裝配置JAVA開發環境html
zookeeper下載地址java
在下載的zookeeper目錄裏建立3個文件,zk1,zk2,zk3,用於存放每一個集羣的數據文件。數據庫
並在三個目錄下建立data和logs目錄apache
將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
三個文件的區別是這三個配置 ide
zoo1.cfg測試
dataDir= ../zk1/data
dataLogDir= ../zk1/logs clientPort=2181
zoo2.cfgui
dataDir= ../zk2/data
dataLogDir= ../zk2/logs clientPort=2182
zoo3.cfg this
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]
中止zk2節點工做
lgj@lgj-Lenovo-G470:~/java/zookeeper-3.4.13/bin$ ./zkServer.sh stop ../conf/zoo2.cfg ZooKeeper JMX enabled by default Using config: ../conf/zoo2.cfg Stopping zookeeper ... STOPPED
等待超時時間到,從新查看各個節點的狀態,能夠看到,zk2中止運行以後,在看zk3被集羣選舉爲leader.
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 Error contacting service. It is probably not running. 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: leader