Zookeeper 的节点类型主要包括以下几种:
- 持久节点(Persistent ZNode):节点创建后即使 Zookeeper 服务器重启也不会消失。
- 临时节点(Ephemeral ZNode):节点的生命周期和客户端会话绑定,会话结束节点删除。
- 顺序节点(Sequential ZNode):节点名称带有顺序编号。
- 持久顺序节点和临时顺序节点:结合了持久和顺序、临时和顺序的特性。
代码示例
以下代码示例展示了如何使用 Zookeeper 创建和操作不同类型的节点。
1. 添加 Maven 依赖
在 pom.xml 中添加 Zookeeper 客户端的依赖:
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
</dependency>
2. 创建 Zookeeper 客户端
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
public class ZookeeperClient {
private static final String ZK_ADDRESS = "localhost:2181";
private static final int SESSION_TIMEOUT = 3000;
private ZooKeeper zooKeeper;
public void connect() throws Exception {
zooKeeper = new ZooKeeper(ZK_ADDRESS, SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("Event received: " + event);
}
});
}
public void close() throws InterruptedException {
if (zooKeeper != null) {
zooKeeper.close();
}
}
public ZooKeeper getZooKeeper() {
return zooKeeper;
}
}
3. 创建和操作不同类型的节点
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
public class ZookeeperNodeTypesExample {
private static final String PERSISTENT_PATH = "/persistent_node";
private static final String EPHEMERAL_PATH = "/ephemeral_node";
private static final String PERSISTENT_SEQUENTIAL_PATH = "/persistent_sequential_node";
private static final String EPHEMERAL_SEQUENTIAL_PATH = "/ephemeral_sequential_node";
public static void main(String[] args) throws Exception {
ZookeeperClient client = new ZookeeperClient();
client.connect();
ZooKeeper zooKeeper = client.getZooKeeper();
// 创建持久节点
if (zooKeeper.exists(PERSISTENT_PATH, false) == null) {
String createdPath = zooKeeper.create(PERSISTENT_PATH, "persistent_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("Created persistent node path: " + createdPath);
}
// 创建临时节点
if (zooKeeper.exists(EPHEMERAL_PATH, false) == null) {
String createdPath = zooKeeper.create(EPHEMERAL_PATH, "ephemeral_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("Created ephemeral node path: " + createdPath);
}
// 创建持久顺序节点
String persistentSequentialPath = zooKeeper.create(PERSISTENT_SEQUENTIAL_PATH, "persistent_sequential_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println("Created persistent sequential node path: " + persistentSequentialPath);
// 创建临时顺序节点
String ephemeralSequentialPath = zooKeeper.create(EPHEMERAL_SEQUENTIAL_PATH, "ephemeral_sequential_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Created ephemeral sequential node path: " + ephemeralSequentialPath);
// 读取节点数据
byte[] data = zooKeeper.getData(PERSISTENT_PATH, false, null);
System.out.println("Data of persistent node " + PERSISTENT_PATH + ": " + new String(data));
data = zooKeeper.getData(EPHEMERAL_PATH, false, null);
System.out.println("Data of ephemeral node " + EPHEMERAL_PATH + ": " + new String(data));
data = zooKeeper.getData(persistentSequentialPath, false, null);
System.out.println("Data of persistent sequential node " + persistentSequentialPath + ": " + new String(data));
data = zooKeeper.getData(ephemeralSequentialPath, false, null);
System.out.println("Data of ephemeral sequential node " + ephemeralSequentialPath + ": " + new String(data));
// 删除节点
zooKeeper.delete(PERSISTENT_PATH, -1);
System.out.println("Deleted persistent node " + PERSISTENT_PATH);
zooKeeper.delete(EPHEMERAL_PATH, -1);
System.out.println("Deleted ephemeral node " + EPHEMERAL_PATH);
zooKeeper.delete(persistentSequentialPath, -1);
System.out.println("Deleted persistent sequential node " + persistentSequentialPath);
zooKeeper.delete(ephemeralSequentialPath, -1);
System.out.println("Deleted ephemeral sequential node " + ephemeralSequentialPath);
client.close();
}
}
详细解释
-
持久节点(Persistent ZNode):
- 创建:使用
CreateMode.PERSISTENT创建持久节点。 - 特性:节点创建后即使 Zookeeper 服务器重启也不会消失。
- 创建:使用
-
临时节点(Ephemeral ZNode):
- 创建:使用
CreateMode.EPHEMERAL创建临时节点。 - 特性:节点的生命周期和客户端会话绑定,会话结束节点删除。
- 创建:使用
-
持久顺序节点(Persistent Sequential ZNode):
- 创建:使用
CreateMode.PERSISTENT_SEQUENTIAL创建持久顺序节点。 - 特性:节点名称带有顺序编号,创建时自动添加一个递增的编号。
- 创建:使用
-
临时顺序节点(Ephemeral Sequential ZNode):
- 创建:使用
CreateMode.EPHEMERAL_SEQUENTIAL创建临时顺序节点。 - 特性:节点名称带有顺序编号,创建时自动添加一个递增的编号,节点的生命周期和客户端会话绑定,会话结束节点删除。
- 创建:使用
总结
Zookeeper 提供了多种类型的节点,以满足不同的应用需求。通过上述代码示例,可以看到如何创建和操作持久节点、临时节点、持久顺序节点和临时顺序节点。了解这些节点类型及其特性,有助于在实际应用中选择合适的节点类型,实现分布式系统的协调和管理。