zookeeper提供了單機、分佈式和僞分佈式三種模式。java
#zookeeper的默認配置文件zoo.cfg:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
條件有限,以僞分佈式爲例,將zookeeper文件夾複製三份,基本配置以下:apache
#第一個
tickTime=2000
initLimit=5
syncLimit=2
#我本機的目錄
dataDir=D:/zookeeper/zookeeper-3.4.9-1/data
dataLogDir=D:/zookeeper/zookeeper-3.4.9-1/logs
clientPort=2181
server.1=localhost:7888:8888
server.2=localhost:7889:8889
server.3=localhost:7889:8890
#第二個
tickTime=2000
initLimit=5
syncLimit=2
#我本機的目錄
dataDir=D:/zookeeper/zookeeper-3.4.9-2/data
dataLogDir=D:/zookeeper/zookeeper-3.4.9-2/logs
clientPort=2182
server.1=localhost:7888:8888
server.2=localhost:7889:8889
server.3=localhost:7889:8890
#第三個
tickTime=2000
initLimit=5
syncLimit=2
#我本機的目錄
dataDir=D:/zookeeper/zookeeper-3.4.9-3/data
dataLogDir=D:/zookeeper/zookeeper-3.4.9-3/logs
clientPort=2183
server.1=localhost:7888:8888
server.2=localhost:7889:8889
server.3=localhost:7889:8890
僞分佈式和分佈式配置的區別在於,僞分佈式配置IP地址相同,端口號不一樣。windows
windows系統使用%zk_home%/bin目錄下的zkServer.cmd啓動zookeeper。因爲在僞分佈式下部署了三個節點,因此依次進入每一個文件夾中,啓動zookeeper。啓動的時候可能會報錯。socket
1 2017-02-09 14:21:38,871 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid confi 2 g, exiting abnormally 3 org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error proce 4 ssing D:\zookeeper\zookeeper-3.4.9-1\bin\..\conf\zoo.cfg 5 at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerC 6 onfig.java:144) 7 at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(Qu 8 orumPeerMain.java:101) 9 at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain 10 .java:78) 11 Caused by: java.lang.IllegalArgumentException: D:\zookeeper\zookeeper-3.4.9-1\da 12 ta\myid file is missing 13 at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(Q 14 uorumPeerConfig.java:362) 15 at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerC 16 onfig.java:140) 17 ... 2 more 18 Invalid config, exiting abnormally
錯誤緣由是沒有找到myid這個文件(如十一、12行),須要在配置文件dataDir指定的目錄下面(個人是:D:\zookeeper\zookeeper-3.4.9-1\data)創建一個myid文件,內容爲1。依次在剩下的兩個文件夾中也創建一個myid的文件,內容分別爲2和3。分佈式
保存後,再次啓動,發現又出現異常了,異常比較多,僅截取部分:spa
1 2017-02-09 14:32:32,467 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:218 2 1:QuorumCnxManager@400] - Cannot open channel to 3 at election address localhost 3 /127.0.0.1:8890 4 java.net.ConnectException: Connection refused: connect 5 at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) 6 at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketI 7 mpl.java:85) 8 at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.ja 9 va:350) 10 at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocket 11 Impl.java:206) 12 at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java 13 :188) 14 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 15 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 16 at java.net.Socket.connect(Socket.java:589) 17 at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(Quorum 18 CnxManager.java:381) 19 at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(Quorum 20 CnxManager.java:426) 21 at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(F 22 astLeaderElection.java:843) 23 at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:822 24 ) 25 2017-02-09 14:32:32,623 [myid:1] - INFO [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:218 26 1:QuorumPeer$QuorumServer@149] - Resolved hostname: localhost to address: localh 27 ost/127.0.0.1 28 2017-02-09 14:32:32,623 [myid:1] - INFO [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:218 29 1:FastLeaderElection@852] - Notification time out: 12800 30 2017-02-09 14:32:46,468 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:218 31 1:QuorumCnxManager@400] - Cannot open channel to 2 at election address localhost 32 /127.0.0.1:8889
這個異常其實能夠忽略,仔細看二、三、3一、32行打印出的信息就明白了。出現這個異常的緣由是由於,如今只啓動了一個zk節點,而在配置文件中卻寫了三個zk的地址,第一個zk節點還沒法與剩下的兩個節點進行通訊,因此只要把三個zk節點都啓動就沒問題了。.net