Zookeeper is understood from a design pattern perspective:
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 that it has been registered on Zookeeper. those observers respond accordingly.
Features:
1) Zookeeper: a leader (Leader), a cluster of multiple followers (Follower).
2) As long as more than half of the nodes in the cluster survive, the Zookeeper cluster can serve normally. So Zookeeper is suitable for installing odd number of servers.
3) Consistent global data: Each server saves a copy of the same data, and the data is consistent no matter which server the client connects to.
4) The update requests are executed sequentially, and the update requests from the same Client are executed in the order in which they are sent.
5) Data update atomicity, a data update either succeeds or fails.
6) Real-time, within a certain time range, Client can read the latest data.
Application scenarios:
Unified naming service, unified configuration management, unified cluster management, dynamic server online and offline, server dynamic online and offline
Stand-alone construction
On the virtual machine, you need to install jdk first, download the compressed file on the official website, and upload it to the server
Unzip to opt directory
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt
Renamed to zk-3.5.7
mv apache-zookeeper-3.5.7-bin/ zk-3.5.7
View Files
cd /opt/zk-3.5.7/
Configure zookeeper
cd conf/
mv zoo_sample.cfg zoo.cfg
Modify the configuration file
#Because /tmp is a temporary file directory, it needs to be changed to its own directory
dataDir=/tmp/zookeeper
Return to the client to create a new folder
mkdir zkData
Modify the file
dataDir = / opt / zkData
Start the server
cd /opt/zk-3.5.7
bin/zkServer.sh start
start the client
bin/zkCli.sh
ls / #You can see the node
quit #Exit
View status
bin/zkServer.sh status
Stop Zookeeper
bin/zkServer.sh stop
Interpretation of configuration parameters
Parameters in the configuration file zoo.cfg in Zookeeper:
tickTime = 2000: communication heartbeat time, the heartbeat time between the Zookeeper server and the client, in milliseconds
initLimit = 10: The time when the master and slave are connected for the first time 10*tickTime
syncLimit = 5: Master-slave synchronous communication time limit
dataDir: save data in Zookeeper
clientPort = 2181: client connection port, usually not modified
cluster
Create a myid file in zkData and specify a custom id number
cd zkData/
vim myid
After configuring the other two machines to install zookeeper, the same operation
Modify the zoo.cfg configuration file and add server.A=B:C:D to the last line
server.1=192.168.6.100:2888:3888
server.2=192.168.6.101:2888:3888
server.3=192.168.6.102:2888:3888
A is a number indicating which server this is
B is the address of this server;
C is the port through which this server Follower exchanges information with the Leader server in the cluster;
D is that in case the leader server in the cluster hangs, a port is needed to re-elect and elect a new leader, and this port is the port used to communicate with each other during the election.
start the cluster
bin/zkServer.sh start
View status
bin/zkServer.sh status
If the startup is unsuccessful, you can enter it in the bin directory for troubleshooting
./zkServer.sh start-foreground
Election mechanism
start the election
Server 1 starts, initiates an election, votes for itself first, and determines whether there are more than half of the votes
Server 2 starts up, votes for itself, and then exchanges vote information. When server 1 finds that server 2's myid is larger than itself, it changes the vote to server 2. At this time, server 2 is the main one, and then another server starts. , which automatically changes from
re-election
The server cannot connect to other servers and will start a re-election
1: If the current server hangs up and cannot be connected to the master, the election is unsuccessful:
2: If the master hangs up, it will be re-elected, first judging Epoch, then judging ZXID, and then judging SID
SID: Server ID. It is used to uniquely identify a machine in a ZooKeeper cluster. Each machine cannot be repeated, and it is the same as myid.
ZXID: Transaction ID. ZXID is a transaction ID used to identify a server state change.
Epoch: The code name of each leader's term (the logical clock value in the same round of voting without a leader is the same. This data will increase after each vote is cast)