Index of /apache/zookeeper
分别在zookeeper的data目录下新建三个文件夹z1、z2、z3代表zookeeper三个节点:
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,目录如下图:
分别执行指令:
# 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的启动就行了。
我们使用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的集群配置以及链接就结束了。