Zookeeper 是一个分布式协调服务,提供了许多关键功能来简化分布式应用的开发和管理。以下是 Zookeeper 的主要功能及其详细解释和代码示例:
1. 命名服务
Zookeeper 提供了一种分布式命名服务,允许应用程序通过路径标识资源,类似于文件系统中的路径。
代码示例:创建和读取节点
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;
public class ZookeeperNamingService {
private static final String PATH = "/myapp/service";
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZooKeeper zooKeeper = client.getZooKeeper();
// 创建命名节点
if (zooKeeper.exists(PATH, false) == null) {
zooKeeper.create(PATH, "service_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// 读取命名节点数据
byte[] data = zooKeeper.getData(PATH, false, null);
System.out.println("Data of node " + PATH + ": " + new String(data));
client.close();
}
}
2. 配置管理
Zookeeper 可以用来集中管理分布式系统的配置信息,并且可以动态更新配置。
代码示例:配置节点的创建和更新
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;
public class ZookeeperConfigManagement {
private static final String CONFIG_PATH = "/myapp/config";
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZooKeeper zooKeeper = client.getZooKeeper();
// 创建配置节点
if (zooKeeper.exists(CONFIG_PATH, false) == null) {
zooKeeper.create(CONFIG_PATH, "initial_config".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// 更新配置节点数据
Stat stat = zooKeeper.setData(CONFIG_PATH, "updated_config".getBytes(), -1);
System.out.println("Updated config with version: " + stat.getVersion());
// 读取配置节点数据
byte[] data = zooKeeper.getData(CONFIG_PATH, false, null);
System.out.println("Config data: " + new String(data));
client.close();
}
}
3. 分布式锁
Zookeeper 可以用来实现分布式锁,以解决多个进程之间的同步问题。
代码示例:实现分布式锁
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.WatchedEvent;
public class ZookeeperDistributedLock {
private static final String LOCK_ROOT = "/locks";
private static final String LOCK_NODE = "/lock_";
private ZooKeeper zooKeeper;
private String lockPath;
public ZookeeperDistributedLock(ZookeeperClient client) throws Exception {
this.zooKeeper = client.getZooKeeper();
if (zooKeeper.exists(LOCK_ROOT, false) == null) {
zooKeeper.create(LOCK_ROOT, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
public void lock() throws KeeperException, InterruptedException {
lockPath = zooKeeper.create(LOCK_ROOT + LOCK_NODE, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
while (true) {
List<String> children = zooKeeper.getChildren(LOCK_ROOT, false);
Collections.sort(children);
if (lockPath.endsWith(children.get(0))) {
System.out.println("Acquired lock: " + lockPath);
return;
}
synchronized (this) {
wait();
}
}
}
public void unlock() throws KeeperException, InterruptedException {
if (lockPath != null && zooKeeper.exists(lockPath, false) != null) {
zooKeeper.delete(lockPath, -1);
System.out.println("Released lock: " + lockPath);
}
}
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZookeeperDistributedLock lock = new ZookeeperDistributedLock(client);
lock.lock();
// Critical section
lock.unlock();
client.close();
}
}
4. 分布式队列
Zookeeper 可以用来实现分布式队列,协调多个进程对任务的处理。
代码示例:实现分布式队列
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.KeeperException;
import java.util.List;
import java.util.Collections;
public class ZookeeperDistributedQueue {
private static final String QUEUE_ROOT = "/queue";
private static final String QUEUE_NODE = "/element_";
private ZooKeeper zooKeeper;
public ZookeeperDistributedQueue(ZookeeperClient client) throws Exception {
this.zooKeeper = client.getZooKeeper();
if (zooKeeper.exists(QUEUE_ROOT, false) == null) {
zooKeeper.create(QUEUE_ROOT, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
public void produce(String data) throws KeeperException, InterruptedException {
zooKeeper.create(QUEUE_ROOT + QUEUE_NODE, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
}
public String consume() throws KeeperException, InterruptedException {
while (true) {
List<String> children = zooKeeper.getChildren(QUEUE_ROOT, false);
if (children.isEmpty()) {
synchronized (this) {
wait();
}
} else {
Collections.sort(children);
String firstNode = children.get(0);
String path = QUEUE_ROOT + "/" + firstNode;
byte[] data = zooKeeper.getData(path, false, null);
zooKeeper.delete(path, -1);
return new String(data);
}
}
}
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZookeeperDistributedQueue queue = new ZookeeperDistributedQueue(client);
queue.produce("task1");
queue.produce("task2");
String task = queue.consume();
System.out.println("Consumed task: " + task);
client.close();
}
}
5. 服务发现
Zookeeper 可以用来实现服务发现机制,客户端可以动态查找和连接服务。
代码示例:实现服务注册和发现
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.WatchedEvent;
import java.util.List;
public class ZookeeperServiceDiscovery {
private static final String SERVICE_ROOT = "/services";
private static final String SERVICE_NODE = "/service_";
private ZooKeeper zooKeeper;
public ZookeeperServiceDiscovery(ZookeeperClient client) throws Exception {
this.zooKeeper = client.getZooKeeper();
if (zooKeeper.exists(SERVICE_ROOT, false) == null) {
zooKeeper.create(SERVICE_ROOT, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
public void registerService(String serviceData) throws Exception {
zooKeeper.create(SERVICE_ROOT + SERVICE_NODE, serviceData.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
}
public void discoverServices() throws Exception {
List<String> children = zooKeeper.getChildren(SERVICE_ROOT, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeChildrenChanged) {
try {
discoverServices();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
for (String child : children) {
byte[] data = zooKeeper.getData(SERVICE_ROOT + "/" + child, false, null);
System.out.println("Discovered service: " + new String(data));
}
}
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZookeeperServiceDiscovery serviceDiscovery = new ZookeeperServiceDiscovery(client);
serviceDiscovery.registerService("Service1");
serviceDiscovery.discoverServices();
client.close();
}
}
总结
Zookeeper 提供了强大的分布式协调功能,如命名服务、配置管理、分布式锁、分布式队列和服务发现等。通过这些功能,可以简化分布式系统的开发和管理,提高系统的可靠性和可扩展性。上述代码示例展示了如何使用 Zookeeper 实现这些功能,为进一步的学习和应用提供了基础。