Introduction to ZooKeeper
This chapter begins our journey into the source code analysis of the Apache ZooKeeper project. We'll cover the essential processes of obtaining, compiling, and running ZooKeeper from its source code.
ZooKeeper Source Code Compilation
Environment Prerequisites
Before we begin compiling the ZooKeeper source code, you'll need to prepare the following development tools:
- JDK 8 or higher
- Git version control system
- Maven build tool
- IntelliJ IDEA IDE
Obtaining the Source Code
First, clone the ZooKeeper repository from GitHub using the following command:
git clone [email protected]:apache/zookeeper.git
After cloning, you'll see the following project structure in your local repository:
The source code obtained through git clone is on the master branch. Since the master branch continuously integrates new features, it's not ideal for source code study. Instead, we recommend using a release version. For this guide, we'll use version 3.5.9. Switch to this version using:
git checkout release-3.5.9
Project Setup and Compilation
After switching versions, open the project in IntelliJ IDEA. The IDE will automatically recognize it as a Maven project. Before compilation, you need to remove all <scope>provided</scope>
tags from the POM files to prevent JAR download failures (ZooKeeper uses local JARs for some dependencies).
Execute the following Maven command to compile:
mvn clean install -DskipTests=true
After executing this command, verify that the generated-sources folder exists in the zookeeper-jute compilation results. If it's missing, the compilation failed and needs to be retried. A successful compilation will show:
Configuring the Runtime Environment
- Configure logging: Copy the log4j.properties file from conf/log4j.properties to the resources directory in the zookeeper-server folder:
- Set up the resources directory: Mark the resources folder in zookeeper-server as a Resources Root in IDEA:
- Prepare the configuration file: Copy conf/zoo_sample.cfg and rename it to zoo.cfg (keep the content unchanged).
Starting the Server
Configure the run parameters for the ZooKeeperServerMain class, which is the core startup class. Set the zoo.cfg file as shown:
Upon starting ZooKeeperServerMain, the console will display environment information, startup time, port usage, and file snapshot details:
Client Testing
Now that the server is running, you can interact with ZooKeeper using the zkCli tool. The client executable is located in the bin directory (use zkCli.cmd for Windows or zkCli.sh for Linux):
Basic Operations Example:
- List root directory:
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper]
- Check node status:
[zk: localhost:2181(CONNECTED) 6] stat /zookeeper
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -2
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
Summary
This chapter provided detailed instructions for obtaining and compiling the ZooKeeper source code. Two critical aspects of the compilation process were highlighted: modifying JAR package scopes in POM files and configuring the ZooKeeper source code startup. After successful compilation and startup, we demonstrated basic ZooKeeper operations using the zkCli tool.