(3). Module3 分布式模式
由于ZooKeeper单机模式不支持单点失败保护,所以不推荐在生产环境下使用。
ZooKeeper有另外一种支持多台机器的模式,即真正的分布式模式,这多台包含在一个应用体内的集群机器被称为quorum,这些机器最小配置为3台,最佳配置为5台,其中包含1台Leader(领导者)机器,由5台机器内部选举产生,另外4台机器就立即成为Follower(跟随者)机器,一旦Leader宕机,剩余的Follower就会重新选举出Leader。
从配置文件内部的字段定义上来说,分布式模式的ZooKeeper与单机模式的ZooKeeper有一些差距,例如下面三个字段:
Ø initLimit:follower对于Leader的初始化连接timeout时间;
Ø syncLimit:follower对于Leader的同步timeout时间;
Ø timeout的计算公式是initLimit*tickTime,syncLimit*tickTime。
此外,我们需要把组成quorum的所有机器也都列在这个配置文件里面。假设我们有两个端口,第一个端口2889用于Follower和Leader之间的通信,通信方式是采用TCP方式,第二个端口3889是为选举Leader用的,用于quorum内部的Leader选举响应。那么我们配置文件如清单1-22所示。
清单1-22 分布式模式配置文件
server.1=node1:2889:3889
server.2=node2:2889:3889
server.3=node3:2889:3889
注意,分布式模式也需要设置myid,这个和伪分布式模式基本一样,只需要在每一台机器上实现一个myid,例如第一台机器是1,第二台机器上设置为2,第三台机器上设置为3,以此类推。
分布式模式的启动方式和单机唯一的差距是每一台机器上都需要启动ZooKeeper服务,即运行命令./zkServer.sh start。
ZooKeeper服务端运行后,我们可以通过在每台机器上运行./zkServer.sh status来查看选举结果,其中Follower节点的运行结果如清单所示,Leader节点的运行结果如清单1-23所示。
清单1-23 Follower节点的运行结果
[root@node3 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/lib/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
清单1-24 Leader节点的运行结果
[root@node2 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/lib/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
差距就在于Mode这一栏。接下来可以通过zkCli命令行访问ZooKeeper服务,假如我们访问node2节点,如清单1-25所示。
清单1-25 访问ZooKeeper服务及输出
[root@localhost bin]# ./zkCli.sh -server node2:2182
Connecting to node2:2182
2016-01-19 16:15:06,702 [myid:] - INFO [main:Environment@100] - Clientenvironment:zookeeper.version=3.4.7-1713338, built on 11/09/2015 04:32 GMT
2016-01-19 16:15:06,710 [myid:] - INFO [main:Environment@100] - Client environment:host.name=node2
2016-01-19 16:15:06,710 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79
2016-01-19 16:15:06,714 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=OracleCorporation
2016-01-19 16:15:06,714 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jdk1.7.0_79/jre
2016-01-19 16:15:06,715 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.class.path=/home/zhoumingyao/zookeeper-3.4.7/bin/../build/classes:/home/zhoumingyao/zookeeper-3.4.7/bin/../build/lib/*.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../lib/slf4j-api-1.6.1.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../lib/netty-3.7.0.Final.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../lib/log4j-1.2.16.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../lib/jline-0.9.94.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../zookeeper-3.4.7.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../src/java/lib/*.jar:/home/zhoumingyao/zookeeper-3.4.7/bin/../conf:.:/usr/lib/jdk1.7.0_79/lib:/usr/lib/jdk1.7.0_79/jre/lib:
2016-01-19 16:15:06,715 [myid:] - INFO [main:Environment@100] - Clientenvironment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2016-01-19 16:15:06,715 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2016-01-19 16:15:06,715 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2016-01-19 16:15:06,716 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2016-01-19 16:15:06,716 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2016-01-19 16:15:06,716 [myid:] - INFO [main:Environment@100] - Clientenvironment:os.version=3.10.0-123.el7.x86_64
2016-01-19 16:15:06,716 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2016-01-19 16:15:06,717 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2016-01-19 16:15:06,717 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/zhoumingyao/zookeeper-3.4.7/bin
2016-01-19 16:15:06,720 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection,connectString=node2:2182 sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5dc6bb75
Welcome to ZooKeeper!
2016-01-19 16:15:06,774 [myid:] - INFO [main-SendThread(node2:2182):ClientCnxn$SendThread@1032] - Openingsocket connection to server node2/172.10.201.56:2182. Will not attempt toauthenticate using SASL (unknown error)
2016-01-19 16:15:06,783 [myid:] - INFO [main-SendThread(node2:2182):ClientCnxn$SendThread@876] - Socketconnection established to node2/172.10.201.56:2182, initiating session
JLine support is enabled
2016-01-19 16:15:06,820 [myid:] - INFO [main-SendThread(node2:2182):ClientCnxn$SendThread@1299] - Sessionestablishment complete on server node2/172.10.201.56:2182, sessionid =0x25258f06e1f0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: node2:2182(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path dataacl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
[zk: node2:2182(CONNECTED) 1]
以上就证明分布式模式启动成功,这里不多加描述,和伪分布式方式基本一样。
欢迎关注麦克叔叔每晚十点说,感兴趣的朋友可以关注公众号,一起交流、一起学习。