1 Zookeeper入门
1.1 概述
Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目。
1.2 特点
1.3 数据结构
1.4 应用场景
提供的服务包括:统一命名服务、统一配置管理、统一集群管理、服务器节点动态上下线、软负载均衡等。
统一命名服务
统一配置管理
统一集群管理
服务器节点动态上下线
软负载均衡
2 Zookeeper安装
2.1 本地模式安装部署
1.安装前准备
(1)安装Jdk
(2)拷贝Zookeeper安装包到Linux系统下
(3)解压到指定目录
[root@topcheer mnt]# mkdir /opt/module/ [root@topcheer mnt]# tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/ zookeeper-3.4.10/ zookeeper-3.4.10/LICENSE.txt zookeeper-3.4.10/lib/ zookeeper-3.4.10/lib/log4j-1.2.16.LICENSE.txt zookeeper-3.4.10/lib/cobertura/ zookeeper-3.4.10/lib/cobertura/README.txt zookeeper-3.4.10/lib/jline-0.9.94.LICENSE.txt
2.配置修改
(1)将/opt/module/zookeeper-3.4.10/conf这个路径下的zoo_sample.cfg修改为zoo.cfg;
[root@topcheer conf]# mv zoo_sample.cfg zoo.cfg [root@topcheer conf]# vim zoo.cfg
(2)打开zoo.cfg文件,修改dataDir路径:
修改如下内容:
dataDir=/opt/module/zookeeper-3.4.10/zkData
(3)在/opt/module/zookeeper-3.4.10/这个目录上创建zkData文件夹
[root@topcheer conf]# mkdir /opt/module/zookeeper-3.4.10/zkData
3.操作Zookeeper
(1)启动Zookeeper
[root@topcheer zookeeper-3.4.10]# bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
(2)查看状态:
[root@topcheer zookeeper-3.4.10]# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: standalone [root@topcheer zookeeper-3.4.10]#
(4)启动客户端:
[root@topcheer zookeeper-3.4.10]# bin/zkCli.sh Connecting to localhost:2181 2019-12-17 21:42:58,170 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT 2019-12-17 21:42:58,185 [myid:] - INFO [main:Environment@100] - Client environment:host.name=topcheer 2019-12-17 21:42:58,186 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_181 2019-12-17 21:42:58,194 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2019-12-17 21:42:58,194 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre 2019-12-17 21:42:58,194 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/module/zookeeper-3.4.10/bin/../build/classes:/opt/module/zookeeper-3.4.10/bin/../build/lib/*.jar:/opt/module/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/module/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/opt/module/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/opt/module/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/opt/module/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/opt/module/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/opt/module/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/opt/module/zookeeper-3.4.10/bin/../conf: 2019-12-17 21:42:58,195 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2019-12-17 21:42:58,195 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2019-12-17 21:42:58,195 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2019-12-17 21:42:58,195 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2019-12-17 21:42:58,195 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2019-12-17 21:42:58,196 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-957.el7.x86_64 2019-12-17 21:42:58,196 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2019-12-17 21:42:58,196 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2019-12-17 21:42:58,196 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/module/zookeeper-3.4.10 2019-12-17 21:42:58,198 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dc Welcome to ZooKeeper! 2019-12-17 21:42:58,240 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2019-12-17 21:42:58,392 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session 2019-12-17 21:42:58,467 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x16f140b9b8f0000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
(5)退出客户端:
[zk: localhost:2181(CONNECTED) 0] quit Quitting... 2019-12-17 21:43:32,790 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x16f140b9b8f0000 closed 2019-12-17 21:43:32,793 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x16f140b9b8f0000 [root@topcheer zookeeper-3.4.10]#
(6)停止Zookeeper
[root@topcheer zookeeper-3.4.10]# bin/zkServer.sh stop
2.2 配置参数解读
Zookeeper中的配置文件zoo.cfg中参数含义解读如下:
1.tickTime =2000:通信心跳数,Zookeeper服务器与客户端心跳时间,单位毫秒
Zookeeper使用的基本时间,服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳,时间单位为毫秒。
它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间。(session的最小超时时间是2*tickTime)
2.initLimit =10:LF初始通信时限
集群中的Follower跟随者服务器与Leader领导者服务器之间初始连接时能容忍的最多心跳数(tickTime的数量),用它来限定集群中的Zookeeper服务器连接到Leader的时限。
3.syncLimit =5:LF同步通信时限
集群中Leader与Follower之间的最大响应时间单位,假如响应超过syncLimit * tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer。
4.dataDir:数据文件目录+数据持久化路径
主要用于保存Zookeeper中的数据。
5.clientPort =2181:客户端连接端口
监听客户端连接的端口。