Windows 下 Zookeeper 單機模式和偽分散式模式安裝
- 安裝jdk
- 安裝Zookeeper.
在官網http://mirrors.hust.edu.cn/apache/zookeeper/下載zookeeper.我下載的是zookeeper-3.4.10版本。 - 解壓zookeeper-3.4.10至D:\dev\zookeeper\zookeeper-3.4.10.
- 在D:\dev\zookeeper新建data及log目錄。
- ZooKeeper的安裝模式分為三種,分別為:單機模式(stand-alone)、叢集模式和叢集偽分佈模式。ZooKeeper
單機模式的安裝相對比較簡單,如果第一次接觸ZooKeeper的話,建議安裝ZooKeeper單機模式或者叢集偽分佈模式。
安裝單機模式。
至D:\dev\zookeeper\zookeeper-3.4.10\conf 複製 zoo_sample.cfg 並貼上到當前目錄下,命名zoo.cfg.
1、編輯zoo.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. dataDir=D:\\dev\\zookeeper\\data dataLogDir=D:\\dev\\zookeeper\\log # the port at which the clients will connect 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
2、cmd命令下進入D:\dev\zookeeper\zookeeper-3.4.10\bin目錄下執行zkserver.cmd.如下所示:
2018-11-21 17:32:36,043 [myid:] - INFO [main:[email protected]] - Server environment:java.compiler=<NA>
2018-11-21 17:32:36,045 [myid:] - INFO [main:[email protected]] - Server environment:os.name=Windows 10
2018-11-21 17:32:36,045 [myid:] - INFO [main: [email protected]] - Server environment:os.arch=amd64
2018-11-21 17:32:36,046 [myid:] - INFO [main:[email protected]] - Server environment:os.version=10.0
2018-11-21 17:32:36,046 [myid:] - INFO [main:[email protected]] - Server environment:user.name=admin
2018-11-21 17:32:36,047 [myid:] - INFO [main:[email protected]] - Server environment:user.home=C:\Users\admin
2018-11-21 17:32:36,047 [myid:] - INFO [main:[email protected]] - Server environment:user.dir=D:\dev\zookeeper\zookeeper-3.4.10\bin
2018-11-21 17:32:36,060 [myid:] - INFO [main:[email protected]] - tickTime set to 2000
2018-11-21 17:32:36,060 [myid:] - INFO [main:[email protected]] - minSessionTimeout set to -1
2018-11-21 17:32:36,061 [myid:] - INFO [main:[email protected]] - maxSessionTimeout set to -1
2018-11-21 17:32:36,560 [myid:] - INFO [main:[email protected]] - binding to port 0.0.0.0/0.0.0.0:2181
3、啟動完成後cmd命令下,netstat-ano檢視埠監聽服務。
4、cmd下進入D:\machine\zookeeper-3.4.6\bin目錄下執行zkcli.cmd.如下所示:
2018-11-21 17:34:18,097 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA>
2018-11-21 17:34:18,098 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Windows 10
2018-11-21 17:34:18,099 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64
2018-11-21 17:34:18,100 [myid:] - INFO [main:[email protected]] - Client environment:os.version=10.0
2018-11-21 17:34:18,102 [myid:] - INFO [main:[email protected]] - Client environment:user.name=admin
2018-11-21 17:34:18,106 [myid:] - INFO [main:[email protected]] - Client environment:user.home=C:\Users\admin
2018-11-21 17:34:18,107 [myid:] - INFO [main:[email protected]] - Client environment:user.dir=D:\dev\zookeeper\zookeeper-3.4.10\bin
2018-11-21 17:34:18,109 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 [email protected]
Welcome to ZooKeeper!
2018-11-21 17:34:18,809 [myid:] - INFO [main-SendThread(0:0:0:0:0:0:0:1:2181):[email protected]] - Opening socket connection to server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2018-11-21 17:34:18,813 [myid:] - INFO [main-SendThread(0:0:0:0:0:0:0:1:2181):[email protected]] - Socket connection established to 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, initiating session
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 2018-11-21 17:34:18,932 [myid:] - INFO [main-SendThread(0:0:0:0:0:0:0:1:2181):[email protected]] - Session establishment complete on server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, sessionid = 0x167359d3d6b0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
安裝叢集偽分佈模式。
1、複製 zoo.cfg 檔案為zoo1.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.
dataDir=D:\\dev\\zookeeper\\data\\d_1
dataLogDir=D:\\dev\\zookeeper\\log
# the port at which the clients will connect
clientPort=2181
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
# 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
2、複製 zoo1.cfg 檔案為zoo2.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.
dataDir=D:\\dev\\zookeeper\\data\\d_2
dataLogDir=D:\\dev\\zookeeper\\log
# the port at which the clients will connect
clientPort=2182
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
# 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
3、複製 zoo1.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.
dataDir=D:\\dev\\zookeeper\\data\\d_3
dataLogDir=D:\\dev\\zookeeper\\log
# the port at which the clients will connect
clientPort=2183
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
# 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
4、在D:\dev\zookeeper\data 新建d_1、d_2及d_3目錄
5、複製zkserver.cmd檔案為zkServer-1.cmd。部分內容如下:
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..\conf\zoo1.cfg
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
6、複製zkserver-1.cmd檔案為zkServer-2.cmd。部分內容如下:
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..\conf\zoo2.cfg
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
7、複製zkserver-1.cmd檔案為zkServer-3.cmd。部分內容如下:
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..\conf\zoo3.cfg
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
8、cmd下分別執行zkserver-1.cmd報錯如下:
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing ..\conf\zoo1.cfg
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:154)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
Caused by: java.lang.IllegalArgumentException: D:\dev\zookeeper\data\d_1\myid file is missing
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:406)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:150)
... 2 more
Invalid config, exiting abnormally
9、缺少myid 檔案在對應位置建立myid 檔案,檔案內隨便寫一個數字,重新啟動,報如下錯誤:
2018-11-21 17:47:14,624 [myid:1] - WARN [WorkerSender[myid=1]:[email protected]] - Cannot open channel to 2 at election address localhost/127.0.0.1:3888
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:562)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:538)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:452)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:433)
at java.lang.Thread.run(Thread.java:745)
2018-11-21 17:47:14,626 [myid:1] - INFO [WorkerSender[myid=1]:[email protected]] - Resolved hostname: localhost to address: localhost/127.0.0.1
2018-11-21 17:47:15,629 [myid:1] - WARN [WorkerSender[myid=1]:[email protected]] - Cannot open channel to 3 at election address localhost/127.0.0.1:3889
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:562)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:538)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:452)
at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:433)
at java.lang.Thread.run(Thread.java:745)
10、這錯是連線另外兩個節點失敗,啟動其他兩個節點即可。
11、cmd下netstar-ano檢視埠監聽情況。
12、cmd下執行zkcli.cmd -server:localhost:2181;zkcli.cmd ;-server:localhost:2182;zkcli.cmd -server:localhost:2183.