zoukankan      html  css  js  c++  java
  • zk 08之:Curator之一:zk客户端Curator

    Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端编程。

    它包含以下几个组件:

    Componentdescription
    Recipes Implementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework.
    Framework The Curator Framework is a high-level API that greatly simplifies using ZooKeeper. It adds many features that build on ZooKeeper and handles the complexity of managing connections to the ZooKeeper cluster and retrying operations.
    Utilities Various utilities that are useful when using ZooKeeper.
    Client A replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities.
    Errors How Curator deals with errors, connection issues, recoverable exceptions, etc.
    Extensions The curator-recipes package implements the common recipes that are described in the ZooKeeper documentation. To avoid bloating that package, recipes/applications that have a vertical appeal will be put in separate “extension” packages using the naming convention curator-x-name.

    示例:

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.sf.zkclient</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>zkclient</name>
      <url>http://maven.apache.org</url>
      <properties>
          <curator.version>2.11.1</curator.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-client</artifactId>
            <version>${curator.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>
    </project>

    java代码:

    示例一:常见的添加、修改、删除示例:

    public static void test() throws Exception {
            String address = "localhost:2181";
            CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
            
            try{
                client.start();
                
                String path = "/yongjiu2/yongjiu3/test5";
                
                //a. 创建永久性节点
                client.create()
                  .creatingParentContainersIfNeeded()
                  .withMode(CreateMode.PERSISTENT)
                  .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                  .forPath(path, "hello, zk".getBytes());
                System.out.println("b");
                
                //b. 创建临时节点
                client.create().withMode(CreateMode.EPHEMERAL).forPath("/temptest5", "hello".getBytes());  
                
                System.out.println("c");
                //c.获取节点值
                byte[] buf = client.getData().forPath(path);
                System.out.println("get data path:"+path+", data:"+new String(buf));
                
                System.out.println("d");
                //d.设置节点值
                client.setData().inBackground().forPath(path, "ricky".getBytes());
                
                System.out.println("e");
                //e.checkExists
                Stat stat = client.checkExists().forPath(path);
                if(stat==null){
                    System.out.println("exec create path:"+path);
                }else {
                    System.out.println("exec getData");
                }
                
                System.out.println("f");
                //f.删除节点
                client.delete().deletingChildrenIfNeeded().forPath(path);
                
                byte[] buf2 = client.getData().forPath(path);
                System.out.println("get data path:"+path+", data:"+new String(buf2));
                
            }finally {
                if(client!=null)
                    client.close();
            }
        }

    示例二:临时节点不能有子节点、不能级联创建示例:

    /**
         * 级联创建临时节点测试,结果会创建不成功,报错
         * @throws Exception
         */
        public static void test2() throws Exception {
            String address = "localhost:2181";
            CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
            
            try{
                client.start();
                
                String path = "/linshi";
                
                //b. 创建临时节点
                client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes());  
                
                byte[] buf = client.getData().forPath(path);
                System.out.println("get data path:"+path+", data:"+new String(buf));
                
                
                //Zookeeper创建临时节点的时候,不能创建级联的节点,下面的create会报错
                String path2 = "/linshi/test";
                //b. 创建临时节点
                client.create().withMode(CreateMode.EPHEMERAL).forPath(path2, "hello".getBytes());  
                
                
                //byte[] buf2 = client.getData().forPath(path2);
                //System.out.println("get data path:"+path2+", data:"+new String(buf2));
                
                
                TimeUnit.SECONDS.sleep(100 * 1000);
            }finally {
                if(client!=null)
                    client.close();
            }
        }

    示例三:3、临时节点何时失效演示(在会话结束时被移除)

    /**
         * 临时节点什么时候失效?是在本次会话结束时
         * @throws Exception
         */
        public static void test3() throws Exception {
            String address = "localhost:2181";
            CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));
            
            try{
                client.start();
                
                String path = "/linshi";
                
                //b. 创建临时节点
                client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes());  
                
                byte[] buf = client.getData().forPath(path);
                System.out.println("get data path:"+path+", data:"+new String(buf));
                
                TimeUnit.SECONDS.sleep(100 * 1000);
            }finally {
                if(client!=null)
                    client.close();
            }
        }

  • 相关阅读:
    .NET之权限管理
    .NET之带星期的日期显示
    ASP.net MVC 同一view或页面使用多个Model或数据集的方法
    ISBN号校检程序(C#与SQL版)
    ASP操作类似多维Cookies
    C# webBrowser自动登陆windows集成验证方法
    JOI 系列乱做
    NOI2021 部分题解
    「NEERC 2015」Jump 题解
    CF/AT 乱做
  • 原文地址:https://www.cnblogs.com/duanxz/p/2585746.html
Copyright © 2011-2022 走看看