zoukankan      html  css  js  c++  java
  • 二、curator入门

    简介

    curator是Apache下开源的项目,它包含了zookeeper的客户端高层级API的实现,并提供了一些常见的用例实现方便我们直接使用。简单来说,使用curator作为zookeeper客户端能够使我们更加简单且可靠地在我们的程序中使用zookeeper。

    curator官网:http://curator.apache.org/

    JavaDoc:http://curator.apache.org/apidocs/index.html

    依赖

    它主要包含三个依赖(curator的依赖都已经放到maven仓库,你直接使用maven来构建它。对于大多数人来说,我们可能最常需要引入的是curator-recipes):

    1)curator-recipes:包含了curator实现的各项功能,如读写锁、互斥锁、队列等,依赖于framework和Client:http://curator.apache.org/curator-recipes/index.html

    2)curator-framework:包含了高层级的流式API,构建在Client之上如对节点的增删改查等:http://curator.apache.org/curator-framework/index.html

    3)curator-client:zookeeper的基础客户端实现,如连接、重试、超时处理等:http://curator.apache.org/curator-client/index.html

    下面示例我们将看到如何使用curator连接zookeeper并进行CRUD操作,这不需要recipes,只需要client和framework即可。使用maven的话,引入framework的时候会自动引入Client,因此,我们将直接引入framework。

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>   

    兼容性

    zookeeper 3.4.x版本和3.5.x版本已经被大量的使用在了生产环境中,即使截止到18年5月3.5.x还处于beta版本中。

    在curator4.0之前,对于3.4.x和3.5.x的兼容需要引入两个不同的版本来对应,分别是:

    1)curator 2.x.x  -> zookeeper 3.4.x 和 3.5.x

    2)curator 3.x.x  -> zookeeper 3.5.x

    而在curator4.0以后,可以同时兼容这两种版本。但是你需要排除zookeeper的客户端依赖包,如:

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>4.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    并,引入你需要的zookeeper依赖包版本,比如引入3.4.6:

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>

    curator会自动兼容你引入的zookeeper依赖,并根据你引入的依赖来决定采用哪一种模式。

    增删改查

    CuratorFramework对外提供基本的流式API操作,需要通过CuratorFrameworkFactory来连接zookeeper,并build一个实例对象。

    在使用API操作之前,你需要调用start()方法,操作结束以后需要调用close()方法结束。不过CuratorFramework是线程安全的,所以你可以采用单例的形式去使用它而不用频繁地创建、开始、关闭等。

    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    public class CuratorDemo {
    
        public static void main(String[] args) throws Exception {
            // 连接zookeeper到本地的2181端口,并设置重试策略为3秒一次,最多3次重试
            CuratorFramework client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").retryPolicy(new ExponentialBackoffRetry(3000, 3)).build();
            System.out.println("启动");
            client.start();
            System.out.println("创建节点");
            // 创建
            client.create().forPath("/user", "lay".getBytes());
            System.out.println("查询节点");
            // 查看
            byte[] result = client.getData().forPath("/user");
            System.out.println(new String(result));
            System.out.println("更新节点");
            // 更新
            client.setData().forPath("/user", "marry".getBytes());
            result = client.getData().forPath("/user");
            System.out.println(new String(result));
            System.out.println("删除节点");
            // 删除
            client.delete().forPath("/user");
            // 判断节点存在
            System.out.println("isExsits: " + client.checkExists().forPath("/user") != null);;
            System.out.println("关闭");
            client.close();
        }
    }

    使用curator必须先调用start()方法,当你不需要使用的时候需要调用close()去关闭。 

  • 相关阅读:
    May 9, 17:3020:00, 1479, "Concentrationcompactness/Rigidity method" lecture 4 by Yang Lan
    May 21, 10:0011:00, 1303, "Capacity, Primeter and ADMmass " by Jie Xiao
    课程期末安排
    问题: Schrodinger方程的广义Strichartz估计
    May 21, 11:1012:10 "On small solutions for nonlinear wave equations in exterior domains " by Makoto NAKAMURA
    SQL Server2019数据库备份与还原脚本,数据库可批量备份
    记一次 IIS 站点配置文件备份和还原,物理路径文件批量备份
    从网上收集的一些图片方面的操作
    android获取本地IP
    java利用httpconnect向服务器发送信息与接收回馈信息
  • 原文地址:https://www.cnblogs.com/lay2017/p/9634587.html
Copyright © 2011-2022 走看看