zookeeper本地叢集節點配置
阿新 • • 發佈:2018-11-18
1.下載zookeeper
2.直接解壓
3.配置本地叢集檔案
分別在zookeeper的data目錄下新建三個資料夾z1、z2、z3代表zookeeper三個節點:
- 每個z1、z2、z3資料夾下新建對應的zX.cfg配置檔案,內容如下:
tickTime = 2000
dataDir = /Users/huleiwind/Documents/soft/zookeeper-3.4.12/data/z1/data/
clientPort = 2181
initLimit = 5
syncLimit = 2
server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
每個目錄唯一的區別是clientPort設定不同,可以分別設定為2181、2182、2183,目錄如下圖:
- 每個z1、z2、z3資料夾下分別建立data資料夾,子目錄下只有一個檔案myid,其中內容就是一個數字,
當伺服器啟動時,伺服器通過配置檔案中的data目錄引數來查詢data 目錄的配置。它通過myId獲得伺服器ID,之後使用配置檔案中server.n 對應的項來設定埠並監聽。目錄結構如下圖:
4.分別啟動各節點
分別執行指令:
# huleiwind @ localhost in ~/Documents/soft/zookeeper-3.4.12/bin [15:19:50]
$ ./zkServer.sh start ../data/z1/z1.cfg
# huleiwind @ localhost in ~/Documents/soft/zookeeper-3.4.12/bin [15:19:50]
$ ./zkServer.sh start ../data/z1/z2.cfg
# huleiwind @ localhost in ~/Documents/soft/zookeeper-3.4.12/bin [15:19:50]
$ ./zkServer.sh start ../data/z1/z3.cfg
輸出日誌在:
/Users/huleiwind/Documents/soft/zookeeper-3.4.12/bin/zookeeper.out
它這個日誌是會覆蓋的,也就是當你自動z2時,z1的啟動日誌就沒了….
當啟動z1時,會發現報:
2018-05-23 15:20:41,252 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 2 at election address /127.0.0.1:3334
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:558)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:610)
at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:838)
at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:957)
2018-05-23 15:20:41,252 [myid:1] - INFO [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumPeer$QuorumServer@184] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1
2018-05-23 15:20:41,253 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 3 at election address /127.0.0.1:4445
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:558)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectAll(QuorumCnxManager.java:610)
at org.apache.zookeeper.server.quorum.FastLeaderElection.lookForLeader(FastLeaderElection.java:838)
at org.apache.zookeeper.server.quorum.QuorumPeer.run(QuorumPeer.java:957)
不停的連結節點2、3,說明z1節點配置ok,再繼續執行z1、z2的啟動就行了。
5.使用zkCli.sh連線叢集
我們使用zkCli.sh來訪問叢集:
# huleiwind @ localhost in ~/Documents/soft/zookeeper-3.4.12/bin [15:56:23]
$ ./zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
日誌:
2018-05-23 16:08:23,931 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-05-23 16:08:23,997 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@878] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2018-05-23 16:08:24,005 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100012a2de50001, negotiated timeout = 30000
可以看到連線到叢集中的節點 127.0.0.1:2181,如果反覆執行,會在三個節點中跳來跳去,也說明了叢集的負載均衡能力。
除了連線串 外,客戶端不用關心ZooKeeper服務由多少個伺服器組成,這也是 ZooKeeper的優點之一。
到這裡單臺機器zookeeper的叢集配置以及連結就結束了。