diff --git a/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/template/ZookeeperTemplate.java b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/template/ZookeeperTemplate.java new file mode 100644 index 00000000..397e5323 --- /dev/null +++ b/zlt-commons/zlt-zookeeper-spring-boot-starter/src/main/java/com/central/common/zookeeper/template/ZookeeperTemplate.java @@ -0,0 +1,231 @@ +package com.central.common.zookeeper.template; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.central.common.constant.CommonConstant; +import lombok.SneakyThrows; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.recipes.cache.*; +import org.apache.zookeeper.CreateMode; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; + +import java.util.List; + +/** + * zookeeper模板类 + * + * @author zlt + * @version 1.0 + * @date 2021/4/10 + *
+ * Blog: https://zlt2000.gitee.io
+ * Github: https://github.com/zlt2000
+ */
+@Component
+public class ZookeeperTemplate {
+ private final CuratorFramework client;
+
+ public ZookeeperTemplate(CuratorFramework client) {
+ this.client = client;
+ }
+
+ /**
+ * 创建空节点,默认持久节点
+ *
+ * @param path 节点路径
+ * @param node 节点名称
+ * @return 完整路径
+ */
+ @SneakyThrows
+ public String createNode(String path, String node) {
+ return createNode(path, node, CreateMode.PERSISTENT);
+ }
+
+ /**
+ * 创建带类型的空节点
+ * @param path 节点路径
+ * @param node 节点名称
+ * @param createMode 类型
+ * @return 完整路径
+ */
+ @SneakyThrows
+ public String createNode(String path, String node, CreateMode createMode) {
+ path = buildPath(path, node);
+ client.create()
+ .orSetData()
+ .creatingParentsIfNeeded()
+ .withMode(createMode)
+ .forPath(path);
+ return path;
+ }
+
+
+ /**
+ * 创建节点,默认持久节点
+ * @param path 节点路径
+ * @param node 节点名称
+ * @param value 节点值
+ * @return 完整路径
+ */
+ @SneakyThrows
+ public String createNode(String path, String node, String value) {
+ return createNode(path, node, value, CreateMode.PERSISTENT);
+ }
+
+ /**
+ * 创建节点,默认持久节点
+ * @param path 节点路径
+ * @param node 节点名称
+ * @param value 节点值
+ * @param createMode 节点类型
+ * @return 完整路径
+ */
+ @SneakyThrows
+ public String createNode(String path, String node, String value, CreateMode createMode) {
+ Assert.isTrue(StrUtil.isNotEmpty(value), "zookeeper节点值不能为空!");
+
+ path = buildPath(path, node);
+ client.create()
+ .orSetData()
+ .creatingParentsIfNeeded()
+ .withMode(createMode)
+ .forPath(path, value.getBytes());
+ return path;
+ }
+
+ /**
+ * 获取节点数据
+ * @param path 路径
+ * @param node 节点名称
+ * @return 节点值
+ */
+ @SneakyThrows
+ public String get(String path, String node) {
+ path = buildPath(path, node);
+ byte[] bytes = client.getData().forPath(path);
+ if (bytes.length > 0) {
+ return new String(bytes);
+ }
+ return null;
+ }
+
+ /**
+ * 更新节点数据
+ * @param path 节点路径
+ * @param node 节点名称
+ * @param value 更新值
+ * @return 完整路径
+ */
+ @SneakyThrows
+ public String update(String path, String node, String value) {
+ Assert.isTrue(StrUtil.isNotEmpty(value), "zookeeper节点值不能为空!");
+
+ path = buildPath(path, node);
+ client.setData().forPath(path, value.getBytes());
+ return path;
+ }
+
+ /**
+ * 删除节点,并且递归删除子节点
+ * @param path 路径
+ * @param node 节点名称
+ */
+ @SneakyThrows
+ public void delete(String path, String node) {
+ path = buildPath(path, node);
+ client.delete().quietly().deletingChildrenIfNeeded().forPath(path);
+ }
+
+ /**
+ * 获取子节点
+ * @param path 节点路径
+ * @return 子节点集合
+ */
+ @SneakyThrows
+ public List