This section describes the Zookeeper technology
ZooKeeper summarizes a mind map to share with you
ZooKeeper aims to encapsulate key services that are complex and error-prone, and provide users with easy-to-use interfaces, efficient performance, and stable functions.
ZooKeeper contains a simple set of primitives providing Java and C interfaces. 2021 interview questions
The ZooKeeper code version, which provides an interface for distributed exclusive locks, elections, and queues, is in Zookeeper-3.4.3 SRC \recipes. Distribution locks and queues have Java and C versions, and elections have only Java versions.
The main function is to solve some data management problems frequently encountered in distributed applications, such as cluster management, unified naming management, distributed configuration management, distributed message queue, distributed lock, distributed notification coordination
Official website: Zookeeper.apache.org/
Zookeeper architecture
A ZK cluster has a Leader, one or more followers, they communicate with each other, and there is a client that accesses the ZK server.
The server has the Fast Fail feature. If the primary server fails, the primary server elects the secondary server to become the new primary server. The primary server mode is the most common mode.
Zookeeper namespace NODE by NODE, similar to the file system, in which each NODE is equal to the directories and files, through the path as a unique identifier, and file systems, each NODE has a corresponding data content, at the same time also can have child nodes, it coordinate is used to store data, such as status, configuration and location information, each NODE The amount of data stored is small, KB.
Watches are introduced
Zk adds, deletes, changes, and searches nodes to trigger listening
The Watch event is a one-time trigger that notifies the client that it is set up when the data it monitors changes
Zookeeper has been installed
Four servers
10.0.0.8 zk – 001
10.0.0.9 zk – 002
10.0.0.10 zk – 003
Zk – client 10.0.0.100
Download and install the JDK
tar zxf jdk-8u60-linux-x64.tar.gz
The mv jdk1.8.0 _60 / usr/local/JDK
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:
JAVA_HOME/lib/tools.jarexport PATH=
PATHsource /etc/profile
java -version
[root@zk-001 ~]# java -version
Java version “1.8.0 comes with _60”
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
Download and install ZooKeeper
Wget www.apache.org/dist/zookee…
The tar ZXF zookeeper – 3.4.6. Tar. Gz
Zookeeper – 3.4.6 mv/usr/local/zookeeper
[root@zk-001 ~]# cd /usr/local/zookeeper/
Configuring environment Variables
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=
PATHsource /etc/profile
They are the configuration of the
Three configuration modes
1. Pseudo-distribution mode
2. Completely distributed
3. Independent mode
Standalone mode configuration
[root@zk-001 zookeeper]# cd conf/
[root@zk-001 conf]# ll
total 12
-rw-rw-r– 1 1000 1000 535 Feb 20 2014 configuration.xsl
-rw-rw-r– 1 1000 1000 2161 Feb 20 2014 log4j.properties
-rw-rw-r– 1 1000 1000 922 Feb 20 2014 zoo_sample.cfg
[root@zk-001 conf]# cp zoo_sample.cfg zoo.cfg
[root@zk-001 conf]#vim zoo.cfg
The number of milliseconds of each tick
TickTime =2000 # Heartbeat check interval in milliseconds
the directory where the snapshot is stored.
do not use /tmp for storage, /tmp here is just
example sakes.
DataDir =/ TMP /zookeeper
the port at which the clients will connect
ClientPort =2181 # Port for clients to connect to
Cluster Mode Configuration
tickTime=2000
initLimit=10
syncLimit=5
//server.n=host:port1:port2
The number N must be the value in myID, which is located in the dataDIR directory and can only be an N value
//port1: indicates the leader port, which is connected to the followers as the leader
//port2: indicates the port to be connected by followers when the leader is elected
server.1=s1:2888:3888
server.2=s2:2888:3888
server.3=s3:2888:3888
The algorithm of ZK to deal with cluster faults is 2N +1, which is best deployed in odd number of machines. If multiple instances are configured on the same server, port conflicts should be paid attention to, which can be written as follows
server.1=s1:2888:3888
server.2=s2:2889:3889
server.3=s3:2887:3887
The configuration process is as follows
Configuring the hosts file
vim /etc/hosts
10.0.0.8 zk – 001
10.0.0.9 zk – 002
10.0.0.10 zk – 003
Configure the server configuration file
[root@zk-001 conf]# mkdir /tmp/zookeeper
[root@zk-001 conf]# cd /tmp/zookeeper/
[root@zk-001 zookeeper]# echo 8 >myid
[root@zk-001 conf]# egrep -v “^#|^$” zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
server.8=zk-001:2888:3888
server.9=zk-002:2888:3888
server.10=zk-003:2888:3888
[root@zk-002 ~]# mkdir /tmp/zookeeper
[root@zk-002 ~]# cd /tmp/zookeeper/
[root@zk-002 zookeeper]# echo 9 >myid
[root@zk-002 zookeeper]# ll
total 4
-rw-r–r– 1 root root 2 May 13 15:51 myid
[root@zk-002 zookeeper]# cd /usr/local/zookeeper/conf/
[root@zk-002 conf]# cp zoo_sample.cfg zoo.cfg
[root@zk-002 conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
server.8=zk-001:2888:3888
server.9=zk-002:2888:3888
server.10=zk-003:2888:3888
[root@zk-003 ~]# mkdir /tmp/zookeeper
[root@zk-003 ~]# cd /tmp/zookeeper/
[root@zk-003 zookeeper]# echo 10 >myid
[root@zk-003 zookeeper]# cd /usr/local/zookeeper/conf/
[root@zk-003 conf]# cp zoo_sample.cfg zoo.cfg
[root@zk-003 conf]# vim zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
server.8=zk-001:2888:3888
server.9=zk-002:2888:3888
server.10=zk-003:2888:3888
Start the service
[root@zk-001 bin]# zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Starting zookeeper … STARTED
[root@zk-002 bin]# zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Starting zookeeper … STARTED
[root@zk-003 bin]# zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Starting zookeeper … STARTED
Check the status
[root@zk-001 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Mode: follower
[root@zk-002 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Mode: follower
[root@zk-003 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Mode: leader
You can see that the leader has been elected, indicating that the configuration is correct
Tests the failover after a leader failure
[root@zk-003 conf]# jps
2694 Jps
2430 QuorumPeerMain
[root@zk-003 conf]# kill 2430
[root@zk-003 conf]# jps
2724 Jps
[root@zk-002 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Mode: follower
[root@zk-001 conf]# zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper/bin/.. /conf/zoo.cfg
Mode: leader
You can see that the ZK-001 server has been elected the new leader
Zookeeper client four-character command
Conf Configuration information
Cons Connection Information
Dump unprocessed session nodes
Envi Environment information
Reqs did not process the request
Stat Statistics
Details about the WCHS server Watch
WCHP Lists the server information in the specified path
/ root @ zk – 003 ~ # echo the conf | nc 10.0.0.8 2181
clientPort=2181
dataDir=/tmp/zookeeper/version-2
dataLogDir=/tmp/zookeeper/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=8
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3888
quorumPort=2888
peerType=0
/ root @ zk – 003 ~ # echo envi | nc 10.0.0.8 2181
Environment:
Zookeeper. version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
host.name=zk-001
Java version = 1.8.0 comes with _121
java.vendor=Oracle Corporation
Java. Home = / usr/lib/JVM/Java — 1.8.0 comes with its 1.8.0.121-0. Bl3. El6_8. X86_64 / jre
java.class.path=/usr/local/zookeeper/bin/.. /build/classes:/usr/local/zookeeper/bin/.. /build/lib/.jar:/usr/local/zookeeper/bin/.. / lib/slf4j – log4j12-1.6.1. Jar: / usr/local/zookeeper/bin /.. / lib/slf4j – API – 1.6.1. Jar: / usr/local/zookeeper/bin /.. / lib/netty – 3.7.0. Final. Jar: / usr/local/zookeeper/bin /.. / lib/log4j – 1.2.16. Jar: / usr/local/zookeeper/bin /.. / lib/jline – 0.9.94. Jar: / usr/local/zookeeper/bin /.. / zookeeper – 3.4.6. Jar: / usr/local/zookeeper/bin /.. /src/java/lib/.jar:/usr/local/zookeeper/bin/.. /conf:
java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.io.tmpdir=/tmp
java.compiler=
os.name=Linux
os.arch=amd64
OS, version = 2.6.32-504. The el6. X86_64
user.name=root
user.home=/root
user.dir=/usr/local/zookeeper/conf
/ root @ zk – 003 ~ # echo stat | nc 10.0.0.8 2181
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/ 10.0.0.10:411920
Latency min/avg/max: 0/0/0
Received: 4
Sent: 3
Connections: 1
Outstanding: 0
Zxid: 0x200000000
Mode: leader
Node count: 4
Zookeeper client command
Run the zkcli. sh command to connect to the server
zkCli.sh -server zk-001:2181
- server 10.0.0.9:2181Copy the code
[root@zk-001 ~]# zkCli.sh -server zk-002:2181
Connecting to zk-002:2181
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: zK-002:2181 (CONNECTED) 0
ZooKeeper -server host:port cmd args
stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port Copy the code
[zk: zk-002:2181(CONNECTED) 1] ls /
List the contents of the directory
[zookeeper]
[zk: zk-002:2181(CONNECTED) 2] ls /zookeeper
[quota]
[zk: zk-002:2181(CONNECTED) 3] ls /zookeeper/quota
[]
[zk: zk-002:2181(CONNECTED) 4] create /root testfile
Create directories and data
Created /root
[zk: zK-002:2181 (CONNECTED)5]get /root
testfile
cZxid = 0x200000002
ctime = Sat May 13 20:09:52 CST 2017
mZxid = 0x200000002
mtime = Sat May 13 20:09:52 CST 2017
pZxid = 0x200000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: zk-002:2181(CONNECTED) 6] set /root testfile-001
cZxid = 0x200000002
ctime = Sat May 13 20:09:52 CST 2017
mZxid = 0x200000003
mtime = Sat May 13 21:04:17 CST 2017
pZxid = 0x200000002
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
Note that ZK cannot create multiple nodes at once
[zk: zk-002:2181(CONNECTED) 7] create /root/s1/s1-1
[zk: zK-002:2181 (CONNECTED) 8] ls /root
[]
[zk: zk-002:2181(CONNECTED) 9] create /root/s1 s1-data
Created /root/s1
[zk: zk-002:2181(CONNECTED) 10] create /root/s2 s2-data
Created /root/s2
[zk: zk-002:2181(CONNECTED) 11] create /root/s3 s3-data
Created /root/s3
[zk: zk-002:2181(CONNECTED) 13] ls /root
[s3, s1, s2]
Check the status
[zk: zk-002:2181(CONNECTED) 14] stat /root
cZxid = 0x200000002
ctime = Sat May 13 20:09:52 CST 2017
mZxid = 0x200000003
mtime = Sat May 13 21:04:17 CST 2017
pZxid = 0x200000006
cversion = 3
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 3
Delete the directory
[zk: zk-002:2181(CONNECTED) 15] delete /root/s3
[zk: zk-002:2181(CONNECTED) 16] ls /root
[s1, s2]
disconnect
[zk: zk-002:2181(CONNECTED) 17] close
2017-05-13 21:13:00,603 [myID :] -info [main:ZooKeeper@684] -session: 0x95C0122eFA50000 Closed 2017-05-13 21:13:00,603 [myID :] -info [main:ZooKeeper@684] -session: 0x95C0122eFA50000 Closed
2017-05-13 21:13:00,604 [myID :] -info [main-eventThread :ClientCnxn$EventThread@512] -EventThread Shut Down
Reconnect the
Since the data is consistent across the ZK servers, THIS time I connect to another server
[zk: zk-002:2181(CLOSED) 18] connect zk-001:2181
[zk: zk-001:2181(CONNECTED) 19] get /root
testfile-001
cZxid = 0x200000002
ctime = Sat May 13 20:09:52 CST 2017
mZxid = 0x200000003
mtime = Sat May 13 21:04:17 CST 2017
pZxid = 0x200000007
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 2
[zk: zk-001:2181(CONNECTED) 20] get /root/s1
s1-data
cZxid = 0x200000004
ctime = Sat May 13 21:07:42 CST 2017
mZxid = 0x200000004
mtime = Sat May 13 21:07:42 CST 2017
pZxid = 0x200000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
The last
I have arranged a: ZK related information documents, Spring family barrel series, Java systematic information, (including Java core knowledge, interview topics and the latest Internet real questions in 20 years, e-books, etc.) friends who need to pay attention to the public number [procedure Yuan Small wan] can be obtained.