Zookeepe r operates on the server side in two modes: standalone and quorum. Standalone mode stands for standalone: a service provided by a single server. In arbitration mode, a group of ZooKeeper servers can replicate data between them and serve clients. The ZooKeeper cluster referred to in the title runs in mediation mode, which is how it is actually used in production.

In a cluster, in order to ensure that the failure of any node does not affect the data consistency of the whole cluster, the number of nodes in the cluster is 2N +1, unless the failure is greater than N (the node here is a ZooKeeper server meaning, that is, running a ZooKeeper server process). To demonstrate this example, three ZooKeeper server processes will be run.

The installation

Download the ZooKeeper package from the official website, which is usually a tar package. After the package is decompressed, use tar -xvf to decompress the package. The shell script for running the ZooKeeper command is displayed in the bin directory.

configuration

To run the three ZooKeeper server processes, create directories for storing the running and configuration data of the three zooKeeper processes, for example, z1, z2, and z3. Create a data directory in each of these directories to store the data when the process is running. Then create a file named myID in each of these directories with the contents of 1, 2, and 3 respectively. Finally, create a. CFG configuration file in each of the three directories.

The configuration file in the z1 directory is called z1.cfg. The content is as follows:

TickTime =2000 initLimit=10 syncLimit=5 dataDir=$(full path of z1)/data clientPort=2981 server.1=127.0.0.1:2222:2223 Server. 2 = 127.0.0.1:3333-3334 for server 3 = 127.0.0.1:4444-4445Copy the code

The configuration file in the z2 directory is called z2.cfg. The content is as follows:

TickTime =2000 initLimit=10 syncLimit=5 dataDir=$(full path of z2 directory)/data clientPort=2982 server.1=127.0.0.1:2222:2223 Server. 2 = 127.0.0.1:3333-3334 for server 3 = 127.0.0.1:4444-4445Copy the code

The configuration file in the z3 directory is called z3. CFG. The content is as follows:

TickTime =2000 initLimit=10 syncLimit=5 dataDir=$(full path of z3 directory)/data clientPort=2983 server.1=127.0.0.1:2222:2223 Server. 2 = 127.0.0.1:3333-3334 for server 3 = 127.0.0.1:4444-4445Copy the code

TickTime: The interval between servers or between clients and servers for maintaining heartbeat. That is, every tickTime, a heartbeat is sent in milliseconds. It is also a time unit in ZooKeeper. All time in ZooKeeper is based on this time unit and is configured in integer multiples. For example, the minimum session timeout is 2*tickTime.

InitLimit and syncLimit: both indicate the number of connected heartbeats.

DataDir: indicates the directory in which ZooKeeper data is saved. By default, zooKeeper log files that write data are also saved in this directory.

ClientPort: indicates the port through which the client connects to the server. Zookeeper listens to this port and receives access requests from the client.

Server. N: XXXX: P1, P2. N indicates the server number, XXXX indicates the IP address of the server, and P1 and P2 are two TCP port numbers used for arbitration and Learder election respectively. The server number also corresponds to the content of the myID file configured above, for example, the content of myID in z1 above is 1, that is, server.1.

Start the

Start the three ZooKeeper processes using the newly created configuration file

Start the z1:

Sh start $(full path of the z1 directory)/z1.cfgCopy the code

Start the z2:

Sh start $(full path of the Z2 directory)/z2.cfgCopy the code

Start the z3:

Sh start $(full path of the Z3 directory)/z3. CFGCopy the code

If the following information is displayed, the ZooKeeper process is started

Sh start command is used to start the server. You can run the zkserver. sh status command to check the server status

Sh status $(full path of the Z2 directory)/z2.cfgCopy the code

You will see the result:

Taking my computer as an example, we can see that these three processes are actually one leader and two followers

Client connection

Run zkcli. sh to connect to ZooKeeper.

Sh $(zookeeper package decompression after the full path)/bin/zkCli. Sh - server 127.0.0.1:2981127.00 0.1:2982127.00 0.1:2983Copy the code

If CONNECTED is displayed on the console, the connection is successful:

You can then use commands such as ls create delete on the console.