前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「周末福报」初识 curator

「周末福报」初识 curator

作者头像
FoamValue
修改2020-08-31 17:29:42
4680
修改2020-08-31 17:29:42
举报
文章被收录于专栏:FoamValue

Apache Curator is a Java/JVM client library for Apache ZooKeeper, a distributed coordination service.  http://curator.apache.org

Apache Curator是用于Apache ZooKeeper(一种分布式协调服务)的Java / JVM客户端库。

在这之前,作为一条舒适区咸鱼的我来说,真的是完全没有听说过 Curator  这个东西😭。

同样是 Apache 大家庭的成员 —— Zookeeper 却经常能在各大技术论坛中找到它的身影。

用一句话来介绍 Zookeeper:它是一个开源的高度可靠的分布式协调的开源服务器。


Curator 基础

Maven 依赖

  • curator-recipes
    • All of the recipes. Note: this artifact has dependencies on client and framework and, so, Maven (or whatever tool you're using) should pull those in automatically.
  • curator-async
    • Asynchronous DSL with O/R modeling, migrations and many other features.
  • curator-framework
    • The Curator Framework high level API. This is built on top of the client and should pull it in automatically.
  • curator-client
    • The Curator Client - replacement for the ZooKeeper class in the ZK distribution.
  • curator-test
    • Contains the TestingServer, the TestingCluster and a few other tools useful for testing.
  • curator-examples
    • Example usages of various Curator features.
  • curator-x-discovery
    • A Service Discovery implementation built on the Curator Framework.
  • org.apache.curator
    • urator-x-discovery-server A RESTful server that can be used with Curator Discovery.

Curator 实战

创建会话

代码语言:javascript
复制
 private static CuratorFramework getZkClient() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
    CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .retryPolicy(retryPolicy)
            .build();
    curatorFramework.start();
    return curatorFramework;
}

创建数据节点

代码语言:javascript
复制
private static Set<String> registeredPathSet = ConcurrentHashMap.newKeySet();
public static void createPersistentNode(String path) {
  try {
    if (registeredPathSet.contains(path) || getZkClient().checkExists().forPath(path) != null) {
      System.out.println(String.format("节点已经存在,节点为:[{}]", path));
    } else {
      getZkClient().create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path);
      System.out.println(String.format("节点创建成功,节点为:[{}]", path));
    }
    registeredPathSet.add(path);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

删除数据节点

代码语言:javascript
复制
public static void removeChildrenNodes(String path) {
  try {
    getZkClient()
      .delete()
      .deletingChildrenIfNeeded()
      .forPath(path);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

读取数据节点

代码语言:javascript
复制
public static List<String> getChildrenNodes(String path) {
  List<String> result = null;
  try {
    result = getZkClient().getChildren().forPath(path);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return result;
}

Zookeeper 数据格式

代码语言:javascript
复制
[zk: 127.0.0.1:2181(CONNECTED) 3] get /my-rpc

cZxid = 0x25 # 节点分配的Id
ctime = Sun Jun 14 08:24:47 UTC 2020 # 节点创建时间
mZxid = 0x25 # 修改后的id
mtime = Sun Jun 14 08:24:47 UTC 2020 # 修改时间
pZxid = 0x26 # 子节点id
cversion = 1 # 子节点的version
dataVersion = 0 # 当前节点数据的版本号
aclVersion = 0 #  权限Version
ephemeralOwner = 0x0 # 临时基节点ID编号,持久节点的id为0
dataLength = 0 # 数据长度
numChildren = 1 # 子节点个数

小结

写到这里,The Last of Us: Part II 好像下载完成了

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FoamValue 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档