Zookeeper_ single

Zookeeper

Zookeeper entry

1.1 Overview

Zookeeper is an open source distributed, to provide coordinated services to distributed applications Apache project.
Here Insert Picture Description

1.2 Features

Here Insert Picture Description

1.3 Data Structure

Here Insert Picture Description

1.4 application scenarios

Services provided include: unified naming services, unified configuration management, unified cluster management, offline dynamic server node, soft load balancing.

Uniform Naming Service

Here Insert Picture Description
Unified Configuration Management
Here Insert Picture Description
Unified Cluster Management
Here Insert Picture Description

The dynamic server offline

Here Insert Picture Description

Soft Load Balancing
Here Insert Picture Description

1.5 Download

1. Official website home page:
https://zookeeper.apache.org/

2. Download theme, as shown in FIG 5-5,5-6,5-7
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

2 Zookeeper installation

2.1 Local mode installation deployment

1. Preparation Before Installation
(1) mounting the Jdk
(2) copies of the installation package Zookeeper Linux system
(3) to extract the specified directory

[atguigu@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/

2. Configuration changes

(1) at the zoo_sample.cfg /opt/module/zookeeper-3.4.10/conf modify this path zoo.cfg;

[atguigu@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
(2)打开zoo.cfg文件,修改dataDir路径:
[atguigu@hadoop102 zookeeper-3.4.10]$ vim zoo.cfg

Modify the following:
dataDir = / opt / Module / ZooKeeper-3.4.10 / zkData
(3) Create a folder on /opt/module/zookeeper-3.4.10/ zkData this directory

[atguigu@hadoop102 zookeeper-3.4.10]$ mkdir zkData

3. Operation Zookeeper
(. 1) Start Zookeeper

[atguigu@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start

(2) to see whether the process started

[atguigu@hadoop102 zookeeper-3.4.10]$ jps
4020 Jps
4001 QuorumPeerMain

(3) View Status:

[atguigu@hadoop102 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

(4) to start the client:

[atguigu@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh

(5) to exit the client:

[zk: localhost:2181(CONNECTED) 0] quit

(6) Stop Zookeeper

[atguigu@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh stop

2.2 Interpretation of configuration parameters

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:客户端连接端口
监听客户端连接的端口。

3 Zookeeper内部原理

3.1 Zookeeper内部原理

1)半数机制:集群中半数以上机器存活,集群可用。所以Zookeeper适合安装奇数台服务器。
2)Zookeeper虽然在配置文件中并没有指定Master和Slave。但是,Zookeeper工作时,是有一个节点为Leader,其他则为Follower,Leader是通过内部的选举机制临时产生的。
3)以一个简单的例子来说明整个选举的过程。
假设有五台服务器组成的Zookeeper集群,它们的id从1-5,同时它们都是最新启动的,也就是没有历史数据,在存放数据量这一点上,都是一样的。假设这些服务器依序启动,来看看会发生什么,如图5-8所示。

Here Insert Picture Description

(1)服务器1启动,此时只有它一台服务器启动了,它发出去的报文没有任何响应,所以它的选举状态一直是LOOKING状态。
(2)服务器2启动,它与最开始启动的服务器1进行通信,互相交换自己的选举结果,由于两者都没有历史数据,所以id值较大的服务器2胜出,但是由于没有达到超过半数以上的服务器都同意选举它(这个例子中的半数以上是3),所以服务器1、2还是继续保持LOOKING状态。
(3)服务器3启动,根据前面的理论分析,服务器3成为服务器1、2、3中的老大,而与上面不同的是,此时有三台服务器选举了它,所以它成为了这次选举的Leader。
(4)服务器4启动,根据前面的分析,理论上服务器4应该是服务器1、2、3、4中最大的,但是由于前面已经有半数以上的服务器选举了服务器3,所以它只能接收当小弟的命了。
(5)服务器5启动,同4一样当小弟。

3.2 节点类型

Here Insert Picture Description

3.3 Stat结构体

1) czxid- create nodes transaction zxid
each modification ZooKeeper state receives a zxid form of a time stamp, which is ZooKeeper Transaction ID.
The total transaction ID is modified in order ZooKeeper all. Each modification has a unique zxid, if zxid1 less than zxid2, then zxid1 occur before zxid2.
2) ctime - The number of milliseconds znode be created (starting in 1970)
3) mzxid - znode last updated transaction zxid
4) mtime - znode last modified milliseconds (since 1970)
5) pZxid-znode last updated child node zxid
6) cversion - znode child node number change, modify znode child node number
7) dataversion - znode data change No.
8) aclVersion - znode access control list change number
9) ephemeralOwner- If the node is temporary, this is znode have session's id. If it is not temporary node is 0.
Data length 10) dataLength- znode of
11) numChildren - Number of child nodes znode

3.4 listener principle (the focus of the interview)

Here Insert Picture Description

3.5 write data flow

Here Insert Picture Description

Published 112 original articles · won praise 12 · views 8094

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/104427273