Install, Visualize, and Use ZooKeeper as a Spring Cloud Registry
This guide walks you through installing ZooKeeper on Windows and Linux, managing it with the PrettyZoo GUI, and configuring it as a Spring Cloud service registry, covering node types, command‑line operations, and practical examples of service registration and discovery.
ZooKeeper is a top‑level open‑source distributed coordination project widely used by Dubbo, Kafka and other projects.
Introduction
ZooKeeper is a distributed coordination framework that provides consistency services for distributed systems. It was originally developed by Yahoo, later donated to the Apache Foundation, and is now a top‑level Apache project with over 9.5k stars on GitHub.
Distributed Coordination
Understanding ZooKeeper starts with the concept of distributed coordination. In a microservice system, many service instances exist; a registry coordinates service names and instance IPs, and ZooKeeper can act as this coordinator.
Installation
ZooKeeper can be installed on both Windows and Linux.
Windows Installation
Download the ZooKeeper binary package from https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz .
Extract it to a chosen directory.
Create a configuration file
conf/zoo.cfgwith the following content:
<code># Set tick time in milliseconds
tickTime=2000
# Directory for snapshot data
dataDir=I:/developer/env/apache-zookeeper-3.7.0-bin/data
# Client connection port
clientPort=2181
</code>Start the service from the
bindirectory:
<code>zkServer.cmd</code>When the service starts successfully, the console displays startup information.
Linux Installation
Pull the Docker image:
<code>docker pull zookeeper:3.7.0</code>Create a configuration directory and file
zoo.cfg:
<code>mkdir -p /mydata/zookeeper/conf/
cd /mydata/zookeeper/conf/
touch zoo.cfg
</code>Write the same configuration as above into
zoo.cfg:
<code># Set tick time in milliseconds
tickTime=2000
# Directory for snapshot data
dataDir=/tmp/zookeeper
# Client connection port
clientPort=2181
</code>Run the ZooKeeper container:
<code>docker run -p 2181:2181 --name zookeeper \
-v /mydata/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-d zookeeper:3.7.0
</code>Command‑line Operations
Use the zkCli tool to interact with ZooKeeper.
Connect:
<code>zkCli.cmd -server 127.0.0.1:2181</code>List root znodes:
<code>[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]
</code>Create a znode
/zk_testwith data
my_data:
<code>[zk: 127.0.0.1:2181(CONNECTED) 2] create /zk_test my_data
Created /zk_test
</code>Retrieve data:
<code>[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk_test
my_data
</code>Update data:
<code>[zk: 127.0.0.1:2181(CONNECTED) 5] set /zk_test test_data
[zk: 127.0.0.1:2181(CONNECTED) 6] get /zk_test
test_data
</code>Delete the znode:
<code>[zk: 127.0.0.1:2181(CONNECTED) 7] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 8] ls /
[zookeeper]
</code>Visual Management
PrettyZoo is a JavaFX‑based graphical client for ZooKeeper.
Download PrettyZoo from GitHub releases and install.
Create a connection to the ZooKeeper server; PrettyZoo supports SSH tunneling.
The UI shows the hierarchical data stored in ZooKeeper.
Right‑click a node to create or delete znodes, eliminating the need for command‑line operations.
PrettyZoo also provides a built‑in command‑line tab for advanced usage.
Node Types
ZooKeeper nodes (znodes) have four main types:
Persistent – remains after the session ends.
Persistent Sequential – persistent with an auto‑incremented suffix.
Ephemeral – removed when the session that created it closes.
Ephemeral Sequential – ephemeral with an auto‑incremented suffix.
When creating nodes via the CLI, the
-soption adds the sequential flag and
-eadds the ephemeral flag. Example:
<code># Create a persistent sequential node
create -s /test/seq segText
# Create an ephemeral node
create -e /test/tmp tmpText
# Create an ephemeral sequential node
create -s -e /test/seqTmp setTmpText
</code>Using ZooKeeper as a Service Registry
ZooKeeper follows the CP model of the CAP theorem, making it suitable for service registration.
Add the Spring Cloud ZooKeeper discovery starter to
pom.xml:
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</code>Configure
application.yml:
<code>spring:
cloud:
zookeeper:
# ZooKeeper connection string
connect-string: localhost:2181
discovery:
# Register services
register: true
# Use IP address instead of hostname
prefer-ip-address: true
</code>In a sample Spring Cloud project, two services (
zookeeper-ribbon-serviceand
zookeeper-user-service) register with ZooKeeper. Using PrettyZoo you can see the service names, IPs, and ports stored as temporary nodes.
When a service shuts down, its temporary node is automatically removed, ensuring high availability.
Summary
This guide covered ZooKeeper installation on Windows and Linux, management with PrettyZoo, command‑line operations, node types, and how to use ZooKeeper as a Spring Cloud service registry, laying a foundation for further exploration such as distributed locks, leader election, or unique ID generation.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.