Operations 6 min read

Application and Principles of Zookeeper in Microservices

This article explains Zookeeper's core features—such as its tree‑like directory, persistent and ephemeral nodes, sequential ordering, and watcher mechanism—and demonstrates how these capabilities enable distributed locks and service registration/discovery within microservice architectures.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Application and Principles of Zookeeper in Microservices

1. Background

Zookeeper is an open‑source distributed coordination service widely used in microservice frameworks like Dubbo and Spring Cloud for service discovery and registration.

2. Zookeeper Features

Zookeeper organizes data in a hierarchical tree structure where each node (znode) can be created, read, updated, or deleted.

2.1 Persistent Node

A persistent node remains in Zookeeper even after the client disconnects.

2.2 Persistent Sequential Node

Based on a persistent node, Zookeeper assigns an ordered sequence number (e.g., 0000001, 0000002) to the node name.

2.3 Ephemeral Node

An ephemeral node is automatically removed when the client session ends and cannot have child nodes.

2.4 Ephemeral Sequential Node

Combines the properties of an ephemeral node with an ordered sequence number.

2.5 Watcher

Clients can register a watcher on a znode; when the node changes (e.g., is deleted), Zookeeper notifies the watcher exactly once, enabling distributed coordination.

3. Microservice Application Scenarios

3.1 Distributed Lock

Each process creates an ordered ephemeral node under a lock path and watches the node with the immediate smaller sequence number; the process holding the smallest node obtains the lock. When a process finishes, it deletes its node, allowing the next smallest node to acquire the lock.

3.2 Service Registration and Discovery

3.2.1 Registration

Service providers create a persistent node for each application (e.g., /order, /member) and under it create an ephemeral node containing the service’s address.

3.2.2 Discovery

Consumers subscribe to the provider’s Zookeeper node; any change (addition or removal of service instances) triggers Zookeeper to push the updated list to the consumer, achieving dynamic discovery.

Author: Marvin Mai

Source: https://dwz.cn/6ByfGykc

microservicesservice discoveryZookeeperDistributed LockDistributed Coordination
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.