curator介绍

https://blog.csdn.net/wo541075754/article/details/68067872 关于第三方客户端的小介绍

zkClient有对dubbo的一些操作支持,但是zkClient几乎没有文档,下面是curator

curator简介

curatorNetflix公司开源的一个 zookeeper客户端,后捐献给 apache,,curator框架在zookeeper原生API接口上进行了包装,解决了很多zooKeeper客户端非常底层的细节开发。提供zooKeeper各种应用场景(比如:分布式锁服务、集群领导选举、共享计数器、缓存机制、分布式队列等的抽象封装,实现了Fluent风格的APl接口,是最好用,最流行的zookeeper的客户端

原生zookeeperAPI的不足

  • 连接对象异步创建,需要开发人员自行编码等待

  • 连接没有自动重连超时机制

  • watcher一次注册生效一次

  • 不支持递归创建树形节点

curator特点

  • 解决session会话超时重连

  • watcher反复注册

  • 简化开发api

  • 遵循Fluent风格API

    <!-- Zookeeper -->
   <dependency>
       <groupId>org.apache.zookeeper</groupId>
       <artifactId>zookeeper</artifactId>
       <version>3.4.10</version>
   </dependency>
   <dependency>
       <groupId>org.apache.curator</groupId>
       <artifactId>curator-framework</artifactId>
       <version>2.6.0</version>
       <exclustions>
           <exclustion>
              <groupId>org.apache.zookeeper</groupId>
              <artifactId>zookeeper</artifactId>
           </exclustion>
       </exclustions>
   </dependency>
   <dependency>
       <groupId>org.apache.curator</groupId>
       <artifactId>curator-recipes</artifactId>
       <version>2.6.0</version>
   </dependency>

 

基础用法

    public static void main(String[] args) {
       // 工厂创建,fluent风格
       CuratorFramework client = CuratorFrameworkFactory.builder()
               // ip端口号
              .connectString("192.168.133.133:2181,192.168.133.133:2182,192.168.133.133:2183")
               // 会话超时
              .sessionTimeoutMs(5000)
               // 重试机制,这里是超时后1000毫秒重试一次
              .retryPolicy(new RetryOneTime(1000))
               // 名称空间,在操作节点的时候,会以这个为父节点
              .namespace("create")
              .build();
       client.start();
       
       System.out.println(client.getState());
       client.close();

  }
  • session重连策略

    • RetryPolicy retry Policy = new RetryOneTime(3000);

      • 说明:三秒后重连一次,只重连一次

    • RetryPolicy retryPolicy = new RetryNTimes(3,3000);

      • 说明:每三秒重连一次,重连三次

    • RetryPolicy retryPolicy = new RetryUntilElapsed(1000,3000);

      • 说明:每三秒重连一次,总等待时间超过个10秒后停止重连

    • RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3)

      • 说明:这个策略的重试间隔会越来越长

        • 公式:baseSleepTImeMs * Math.max(1,random.nextInt(1 << (retryCount + 1)))

          • baseSleepTimeMs = 1000 例子中的值

          • maxRetries = 3 例子中的值

创建

public class curatorGettingStart {
   public static CuratorFramework client;

   // ids权限
   public static void create1() throws Exception {
       // 新增节点
       client.create()
               // 节点的类型
              .withMode(CreateMode.EPHEMERAL)
               // 节点的acl权限列表
              .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
               // arg1:节点路径,arg2:节点数据
              .forPath("/node1",new byte[0]);
  }
   // 自定义权限
   public static void create2() throws Exception {
       ArrayList<ACL> acls = new ArrayList<>();
       Id id = new Id("world", "anyone");
       acls.add(new ACL(ZooDefs.Perms.READ,id));
       // 新增节点
       client.create()
               // 节点的类型
              .withMode(CreateMode.EPHEMERAL)
               // 节点的acl权限列表
              .withACL(acls)
               // arg1:节点路径,arg2:节点数据
              .forPath("/node2",new byte[0]);
  }
   // 递归创建
   public static void create3() throws Exception {
       // 新增节点
       client.create()
               // 递归创建
              .creatingParentsIfNeeded()
               // 节点的类型
              .withMode(CreateMode.EPHEMERAL)
               // 节点的acl权限列表
              .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
               // arg1:节点路径,arg2:节点数据
              .forPath("/node2/nodex",new byte[0]);
  }
   // 递归创建
   public static void create4() throws Exception {
       // 新增节点
       System.out.println(1);
       client.create()

              .creatingParentsIfNeeded()
               // 节点的类型
              .withMode(CreateMode.EPHEMERAL)
               // 节点的acl权限列表
              .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
               // 异步
              .inBackground(new BackgroundCallback() {
                   @Override
                   public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                       System.out.println("异步创建成功");
                  }
              })
               // arg1:节点路径,arg2:节点数据
              .forPath("/node2/nodex",new byte[0]);
       System.out.println(2);
  }
   public static void main(String[] args) throws Exception {
       // 工厂创建,fluent风格
       CuratorFramework client = CuratorFrameworkFactory.builder()
               // ip端口号
              .connectString("192.168.133.133:2181,192.168.133.133:2182,192.168.133.133:2183")

相关文章: