1. Zookeeper architecture
The zookeeper server runs in two modes: standalone mode and quorum mode. Independent mode: There is a single server, and the zookeeper status cannot be replicated. Arbitration mode: There is a group of zookeeper servers, which we call zookeeper clusters (zookeeper ensemble). They can replicate the state before and serve client requests at the same time.
2. Start using zookeeper
Download link: https://archive.apache.org/dist/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5- bin.tar.gz
First we run zookeeper in standalone mode to create a session.
Upload to the Linux server and decompress:
[[emailprotected] opt]# tar xvf apache-zookeeper-3.5.5-bin.tar.gz -C /opt/
< p>If you are using a Windows system, you can use winzip’s decompression tool to decompress the release package.
In the distribution directory, you will find a script to start zookeeper in the bin directory. The scripts ending in .sh run on Unix platforms, and the scripts ending in .cmd are used on Windows. Save the configuration file in the conf directory. The lib directory contains Java jar files, which are the third-party support needed to run zookeeper.
Modify the configuration file:
[[emailprotected] zookeeper]# cd conf/
[[emailprotected] conf]# mv zoo_sample.cfg zoo.cfg
[[ email protected] conf]# vim zoo.cfg
dataDir=/opt/zookeeper/dataDir
Officially, it is not recommended to use the default dataDir. Modify dataDir to save and exit.
Start the server and execute the following command:
[[emailprotected] conf]# cd ..
[[emailprotected] zookeeper]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
This server-side command makes zookeeper The server runs in the background. If you are running in the foreground to view the server output, you can use the following command to run:
[[email protected] zookeeper]# bin/zkServer.sh start-foreground
This option provides a lot of The output of detailed information to allow viewing of what happened to the server.
After startup, check the log file for error messages:
[[emailprotected] zookeeper]# cd logs/
[[emailprotected] logs]# ll
total 8
-rw-r--r-- 1 root root 7516 Oct 21 18:28 zookeeper-root-server-localhost.localdomain.out
[[emailprotected] logs]# grep -E- i "((exception)|(error))" *
If the query result is empty, there is no error message.
Now use the zookeeper client to connect to the zookeeper server, and run the following command:
[[emailprotected] zookeeper]# bin/zkCli.sh
.........< br />2019-10-21 18:37:35,242 [myid:localhost:2181]-INFO [main-SendThread(localhost:2181):[emailprotected]]-Socket connection established, initiating session, client: /0: 0:0:0:0:0:0:1:39276, server: localhost/0:0:0:0:0:0:0:1:2181
2019-10-21 18:37: 35,308 [myid:localhost:2181]-INFO [main-SendThread(localhost:2181):[email protected]]-Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181 , sessionid = 0x1000132e0500000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED ) 0]
Through the above information, we know the connection between the zookeeper client and the server.
3. Basic zookeeper commands
View all znodes under root:
[zk: localhost:2181(CONNECTED) 3] ls -R /
/
/zookeeper
/zookeeper/config
/zookeeper/quota
Create a znode called workers:
[zk: localhost:2181(CONNECTED) 4] create /workers
Created /workers
[zk: localhost:2181(CONNECTED) 7] ls -R /
/
/ workers
/zookeeper
/zookeeper/config
/zookeeper/quota
Delete the created worker nodes and exit zkCli:
[zk: localhost:2181(CONNECTED) 8] delete /workers
[zk: localhost:2181(CONNECTED) 9] ls -R /
/
/zookeeper
/zookeeper/config< br />/zookeeper/quota
[zk: localhost:2181(CONNECTED) 10] quit
WATCHER::
WatchedEvent state:Closed type:None path :null
2019-10-21 19:35:46,321 [myid:]-INFO [main:[email protected]]-Session: 0x1000132e0500000 closed
2019-10-21 19:35:46,322 [ myid:]-INFO [main-EventThread:[email protected]]-EventThread shut down for session: 0x1000132e0500000
Close zoo Keeper server:
[[email protected] zookeeper]# bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../ conf/zoo.cfg
Stopping zookeeper ... STOPPED
By looking at the zkServer.sh script, we know that 6 parameters are acceptable, as follows: start: start service start-foreground: foreground start service print -cmd: print command line information stop: stop the service restart: restart the service (stop first, then start) status: service status