Article directory
1. Introduction
1. Working mechanism
Official address: https://zookeeper.apache.org/
Zookeeper is an open source distributed Apache project that provides coordination services for distributed frameworks.
- Zookeeper is understood from the perspective of design pattern: it is a distributed service management framework designed based on the observer pattern. It is responsible for storing and managing data that everyone cares about, and then accepts the registration of observers. Once the status of these data changes, Zookeeper Will be responsible for notifying those observers registered on Zookeeper to react accordingly.
2. Features
- Zookeeper: a leader and a cluster of followers.
- As long as more than half of the nodes in the cluster survive, the Zookeeper cluster can serve normally. Therefore, Zookeeper is suitable for installing an odd number of servers.
- Global data consistency: Each server saves a copy of the same data. No matter which server the client connects to, the data is consistent.
- Update requests are executed sequentially, and update requests from the same Client are executed sequentially in the order they are sent.
- Data update is atomic, a data update either succeeds or fails.
- Real-time, within a certain time range, the Client can read the latest data.
3. Data structure
- The structure of the ZooKeeper data model is very similar to the Unix file system.
- It can be regarded as a tree as a whole, and each node is called a ZNode.
- Each ZNode can store 1MB of data by default, and each ZNode can be uniquely identified by its path.
4. Application scenarios
The services provided include: unified naming service, unified configuration management, unified cluster management, dynamic online and offline server nodes, soft load balancing, etc.
- Unified naming service
- Unified configuration management
- Unified cluster management
- Server nodes dynamically go online and offline
- Soft load balancing, etc.
Unified naming service
- In a distributed environment, it is often necessary to name applications/services uniformly for easy identification. For example: IP is not easy to remember, but domain name is easy to remember.
Unified configuration management
-
In a distributed environment, configuration file synchronization is very common.
- It is generally required that the configuration information of all nodes in a cluster is consistent, such as a Kafka cluster.
- After modifying the configuration file, I hope it can be quickly synchronized to each node.
-
Configuration management can be implemented by ZooKeeper.
- Configuration information can be written to a Znode on ZooKeeper.
- Each client server listens to this Znode.
- Once the data in the Znode is modified, ZooKeeper will notify each client server.
Unified cluster management
- In a distributed environment, it is necessary to know the status of each node in real time.
- Some adjustments can be made based on the real-time status of the node.
- ZooKeeper can monitor node status changes in real time.
- Node information can be written to a ZNode on ZooKeeper.
- Monitor this ZNode to obtain its real-time status changes.
Server dynamic online and offline
Soft load balancing
- Record the number of visits to each server in Zookeeper, and let the server with the least number of visits handle the latest client request.
5. Election mechanism
first start
- 1) Server 1 starts and initiates an election. Server 1 casts its vote. At this time, server 1 has one vote, which is not enough for more than half (3 votes), the election cannot be completed, and the status of server 1 remains LOOKING;
- 2) Server 2 starts and initiates another election. Servers 1 and 2 each vote for themselves and exchange vote information: At this time, server 1 finds that the myid of server 2 is larger than the one it is currently voting for (server 1), and changes the vote to recommend server 2. At this time, server 1 has 0 votes, and server 2 has 2 votes. Without more than half of the results, the election cannot be completed, and the status of servers 1 and 2 remains LOOKING.
- 3) Server 3 starts and initiates an election. At this time, both servers 1 and 2 will change the votes to server 3. The results of this vote: Server 1 has 0 votes, Server 2 has 0 votes, and Server 3 has 3 votes. At this time, server 3 has more than half of the votes, and server 3 is elected leader. Servers 1 and 2 change the status to FOLLOWING, and server 3 changes the status to LEADING;
- 4) Server 4 starts and initiates an election. At this time, servers 1, 2, and 3 are no longer in the LOOKING state and will not change the voting information. The result of exchanging vote information: Server 3 has 3 votes and Server 4 has 1 vote. At this time, server 4 obeys the majority, changes the voting information to server 3, and changes the status to FOLLOWING;
- 5) Server 5 starts and acts as the younger brother like 4.
Not the first startup
2. Software installation
# The number of milliseconds of each tick
# 通信心跳时间,Zookeeper服务器与客户端心跳时间,单位毫秒
tickTime=2000d
# The number of ticks that the initial
# synchronization phase can take
# LF初始通信时限
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# LF同步通信时限
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
# 保存Zookeeper中的数据
dataDir=/opt/module/zookeeper-3.5.7/zkData
# the port at which the clients will connect
#客户端连接端口,通常不做修改
clientPort=2181
1. Stand-alone version installation
Install JDK (you need to install JDK first)
yum install java-1.8.0-openjdk-devel.x86_64
Create a file/opt/module
mkdir /opt/module
cd /opt/module
download file
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
unzip files
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module
Modify the installation directory name
mv apache-zookeeper-3.5.7-bin zookeeper-3.5.7
Configure environment variables
vim /etc/profile.d/my_env.sh
# ZOOKEEPER_HOME
export ZOOKEEPER_HOME=/opt/module/zookeeper-3.5.7
export PATH=$PATH:$ZOOKEEPER_HOME/bin
Load environment variables
source /etc/profile
Add configuration file
cd /opt/module/zookeeper-3.5.7/conf/
mv zoo_sample.cfg zoo.cfg
Modify configuration file
vim zoo.cfg
# 修改数据所在文件
dataDir=/opt/module/zookeeper-3.5.7/zkData
Create log directory
mkdir /opt/module/zookeeper-3.5.7/zkData
Start zookeeper
bin/zkServer.sh start
Query startup status
# 查看进程
jps
# 查看状态
bin/zkServer.sh status
Start client
bin/zkCli.sh
Exit client
quit
stopzookeeper
bin/zkServer.sh stop
2. Cluster installation
Install JDK (you need to install JDK first)
yum install java-1.8.0-openjdk-devel.x86_64
Create a file/opt/module
mkdir /opt/module
cd /opt/module
download file
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
unzip files
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/module
Modify the installation directory name
cd /opt/module
mv apache-zookeeper-3.5.7-bin zookeeper-3.5.7
Create data storage directory
cd /opt/module/zookeeper-3.5.7/
mkdir zkData
/opt/module/zookeeper-3.5.7/zkData
Create a myid file in the directory
Add the number corresponding to the server in the file (note: there should be no blank lines above and below, and no spaces on the left and right)
cd /opt/module/zookeeper-3.5.7/zkData
vim myid
# 服务器编号,每台服务器要不同
1
Configure the zoo.cfg configuration file
cd /opt/module/zookeeper-3.5.7/conf
mv zoo_sample.cfg zoo.cfg
Modify configuration file
vim /opt/module/zookeeper-3.5.7/conf/zoo.cfg
# 修改数据存储位置
dataDir=/opt/module/zookeeper-3.5.7/zkData
# 增加集群配置
#######################cluster##########################
server.1=hadoop101:2888:3888
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
Start the cluster (all 3 units must be started)
cd /opt/module/zookeeper-3.5.7
bin/zkServer.sh start
View status
bin/zkServer.sh status
3. Interpretation of configuration parameters (zoo.cfg)
The meaning of the parameters in the configuration file zoo.cfg in Zookeeper is interpreted as follows:
# 通信心跳时间
tickTime=2000
# LF初始通信时限
initLimit=10
# LF同步通信时限
syncLimit=5
# 保存Zookeeper中的数据
dataDir=/tmp/zookeeper
# 客户端连接端口,通常不做修改
clientPort=2181
- tickTime = 2000: Communication heartbeat time, Zookeeper server and client heartbeat time, unit milliseconds
- initLimit = 10: LF initial communication time limit
The maximum number of heartbeats that the Leader and Follower can tolerate during the initial connection (the number of tickTimes)
- syncLimit = 5: LF synchronization communication time limit
If the communication time between Leader and Follower exceeds syncLimit * tickTime
, Leader considers Follower to be dead and deletes Follower from the server list.
- dataDir: save data in Zookeeper
Note: The default tmp directory is easily deleted regularly by the Linux system, so the default tmp directory is generally not used.
- clientPort = 2181: Client connection port, usually not modified.
4. ZK cluster startup script
#!/bin/bash
case $1 in
"start"){
for i in hadoop102 hadoop103 hadoop104
do
echo ---------- zookeeper $i 启动 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start"
done
};;
"stop"){
for i in hadoop102 hadoop103 hadoop104
do
echo ---------- zookeeper $i 停止 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop"
done
};;
"status"){
for i in hadoop102 hadoop103 hadoop104
do
echo ---------- zookeeper $i 状态 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status"
done
};;
esac
3. Command line operation
1. Grammar
Basic command syntax | Function description |
---|---|
help | Show all operation commands |
ls path | Use the ls command to view the child nodes of the current znode [can be monitored] -w monitors child node changes -s additional secondary information |
create | Ordinary creation -s contains sequence -e temporary (disappears after restart or timeout) |
get path | Get the value of the node [can be monitored] -w monitor the node content changes -s additional secondary information |
set | Set the specific value of the node |
stat | View node status |
delete | Delete node |
deleteall | Recursively delete nodes |
2. Use
Start client
bin/zkCli.sh -server hadoop101:2181
Show all operation commands
help
View the content contained in the current znode
ls /
View detailed data of the current node
ls -s /
czxid
: Transaction zxid of the created node- Each time the ZooKeeper state is modified, a ZooKeeper transaction ID is generated. The transaction ID is the total order of all modifications in ZooKeeper. Each modification has a unique zxid. If zxid1 is less than zxid2, then zxid1 occurs before zxid2.
ctime
: The number of milliseconds since the znode was created (since 1970)mzxid
: zxid of the last updated transaction of znodemtime
: The number of milliseconds since the znode was last modified (since 1970)pZxid
: zxid of the last updated child node of znodecversion
:Znode sub-node change number, znode sub-node modification timesdataversion
:znode data change numberaclVersion
: Change number of znode access control listephemeralOwner
: If it is a temporary node, this is the session id of the znode owner. It is 0 if it is not a temporary node.dataLength
: Data length of znodenumChildren
:The number of znode child nodes
3. Node related
Nodes can be divided into persistent nodes and transient nodes.
持久(Persistent)
: After the client and server are disconnected, the created nodes will not be deleted.- After the persistent directory node client disconnects from Zookeeper, the node still exists
- After the persistent sequential number directory node client disconnects from Zookeeper, the node still exists, but Zookeeper sequentially numbers the node name.
短暂(Ephemeral)
: After the client and server are disconnected, the created node will be deleted by itself.- After the temporary directory node client disconnects from Zookeeper, the node is deleted.
- After the temporary sequential number directory node client disconnects from Zookeeper, the node is deleted, but Zookeeper sequentially numbers the node name.
Description: Set the sequence identifier when creating a znode. A value will be appended to the znode name. The sequence number is a monotonically increasing counter maintained by the parent node.
Note: In a distributed system, sequence numbers can be used to globally order all events, so that clients can infer the order of events based on the sequence number.
Create 2 ordinary nodes respectively (when creating a node, assign a value)
create /sanguo "diaochan"
create /sanguo/shuguo "liubei"
Get child nodes
ls /sanguo
sanguo
value obtained
get -s /sanguo
- Ordinary node:
create /test/test1 "zhansan01"
- Node with serial number:
create -s /test/test1 "zhansan01"
: The created node has a serial numbertest1000001
- Transient node:
create -e /test/test1 "zhansan01"
: will disappear after closing the client (temporary)
Create a normal node
create /test/test1 "zhansan01"
Create a node with a serial number
create -s /test/test2/test21 "zhansan02"
create -s /test/test2/test22 "lisi02"
create -s /test/test2/test23 "wangwu02"
Create a short-lived node (disappears after closing the client)
create -e /test/test3 "zhansan03"
# 创建短暂的带序号的节点
create -e -s /test/test3 "lisi03"
Modify the value of a node
set /test/test3 "wangwu03"
4. Listener principle
The client registers to monitor the directory nodes it cares about. When the directory nodes change (data changes, nodes are deleted, subdirectory nodes are added and deleted), ZooKeeper will notify the client. The monitoring mechanism ensures that any changes in data saved by ZooKeeper can quickly respond to the application monitoring the node.
Monitoring principle
- 1) First there must be a main() thread
- 2) Create a Zookeeper client in the main thread. At this time, two threads will be created, one responsible for network connection communication (connet) and one responsible for listening (listener).
- 3) Send registered listening events to Zookeeper through the connect thread.
- 4) Add the registered listening events to the list of registered listeners in Zookeeper.
- 5) When Zookeeper detects data or path changes, it will send this message to the listener thread.
- 6) The process() method is called internally in the listener thread.
Common monitoring
#监听节点数据的变化
get path [watch]
# 监听子节点增减的变化
ls path [watch]
Monitor /test
value changes
get -w /test
# 更新/test值
set /test "zhangsan"
Note: If you modify the value of /sanguo multiple times in hadoop103, monitoring will no longer be received on hadoop104. Because you register once, you can only listen once. If you want to monitor again, you need to register again.
Monitor subdirectories for changes
ls -w /test
# 新建子目录值
create /test/test4 "zhangsan04"
Note: The path change of the node is also registered once and takes effect once. If you want it to take effect multiple times, you need to register multiple times.
5. Node deletion and viewing
Delete node
delete /test/test4
Recursively delete nodes
deleteall /test/test2
View node status
stat /test