Using Zookeeper for Configuration Management, Distributed Locks, Queues, and Load Balancing in Backend Systems
This article explains how Zookeeper can be employed in backend applications for centralized configuration management, implementing distributed locks, building distributed queues, and achieving load balancing, providing detailed Java code examples and step‑by‑step explanations of each use case.
The article introduces Zookeeper as a powerful coordination service and demonstrates its practical applications in backend systems.
1. Introduction
Previously the author wrote about Zookeeper basics; here several concrete scenarios are presented, including configuration management, distributed locks, queues, and load balancing.
2. Specific Applications
2.1 Consistent Configuration Management
Configuration data such as database connection strings can be stored in Zookeeper nodes, allowing all servers to read and update them without restarting.
public class CommonConfig implements Serializable {
private String dbUrl;
private String username;
private String password;
private String driverClass;
// getters, setters, constructors, toString omitted for brevity
}Configuration manager reads/writes this object to a Zookeeper node.
public class ZkConfigMng {
private String nodePath = "/commConfig";
private CommonConfig commonConfig;
private ZkClient zkClient;
public CommonConfig initConfig(CommonConfig commonConfig) { ... }
public CommonConfig update(CommonConfig commonConfig) { ... }
public void syncConfigToZookeeper() { ... }
}A client retrieves the configuration:
public class ZkConfigClient implements Runnable {
private String nodePath = "/commConfig";
private CommonConfig commonConfig;
public void run() { ... }
}2.2 Distributed Lock
By creating a persistent lock node and temporary sequential child nodes, processes can acquire the lock based on the smallest sequence number.
public class DistributedLock {
private static class Constant {
static final int SESSION_TIMEOUT = 10000;
static final String CONNECTION_STRING = "127.0.0.1:2181";
static final String LOCK_NODE = "/distributed_lock";
static final String CHILDREN_NODE = "/task_";
}
private ZkClient zkClient;
public DistributedLock() { ... }
public String getLock() { ... }
public Boolean acquireLock(String lockName) throws InterruptedException { ... }
public void releaseLock(String lockName) { zkClient.delete(lockName); }
public void closeZkClient() { zkClient.close(); }
}Example usage in a Spring Boot main method creates the lock, performs business logic, then releases it.
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ZookeeperDemoApplication.class, args);
DistributedLock lock = new DistributedLock();
String lockName = lock.getLock();
if (lockName != null) {
lock.releaseLock(lockName);
}
lock.closeZkClient();
}2.3 Distributed Queue
Using Zookeeper's temporary sequential nodes, producers enqueue messages and consumers dequeue them in FIFO order.
public interface AppConstant {
static String ZK_CONNECT_STR = "127.0.0.1:2181";
static String NODE_PATH = "/mailbox";
static String CHILD_NODE_PATH = "/mail_";
static int MAILBOX_SIZE = 10;
}
public class MailConsumer implements Runnable, AppConstant { ... }
public class MailProducer implements Runnable, AppConstant { ... }2.4 Load Balancing
Service providers register their IPs under a Zookeeper path; consumers subscribe to changes and select a provider using a simple random algorithm.
public class ServiceProvider {
static String ZK_CONNECT_STR = "127.0.0.1:2181";
static String NODE_PATH = "/service";
static String SERIVCE_NAME = "/myService";
private ZkClient zkClient;
public void registryService(String localIp, Object obj) { ... }
}
public class ServiceConsumer {
static String ZK_CONNECT_STR = "127.0.0.1:2181";
static String NODE_PATH = "/service";
static String SERIVCE_NAME = "/myService";
private List
serviceList = new ArrayList<>();
private ZkClient zkClient;
public void subscribeSerivce() { ... }
public void consume() { int index = new Random().nextInt(serviceList.size()); ... }
}3. Summary
Zookeeper is a versatile tool that can handle configuration management, distributed locks, queues, service registration, and load balancing, among other coordination tasks in distributed architectures.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.