zookeeper的javaAPI操作(基于Curator的CRUD)

 


Curator介绍

•Curator 是 Apache ZooKeeper 的Java客户端库,目标是简化 ZooKeeper 客户端的使用。

•Curator 最初是 Netfix 研发的,后来捐献了 Apache 基金会,目前是 Apache 的顶级项目。

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

 

 

获得对zookeeper服务端的连接对象:

在使用javaAPI的Curator进行增删改查的操作之前我们需要获得对zookeeper服务端的连接对象:

复制代码
    @Before
    public void ceshiConnect() {
//定义重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
        client = CuratorFrameworkFactory.builder()
                .connectString("192.168.31.81:2181")   //zookeeper的地址
                .sessionTimeoutMs(60 * 1000)      //会话超时时间
                .connectionTimeoutMs(15 * 1000)    //连接超时时间
                .retryPolicy(retryPolicy)    //重试策略
                .namespace("hui")     //命名空间
                .build();


        client.start();
    }
复制代码

 

 

 

 

 基于javaAPI的Curator的增加结点:

复制代码

 


@Test//创建不带有数据的结点但是,默认数据是当前客户端的ip public void testCreate() throws Exception { String forPath = client.create().forPath("/app1"); System.out.println(forPath); } @Test//创建带有数据的结点 public void testNodetext() throws Exception { String forPath = client.create().forPath("/app4", "yfsn".getBytes()); System.out.println(forPath); } @Test//创建结点的同时设置结点的类型 public void testMadeTheTypeNode() throws Exception { String forPath = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app6", "yfsn".getBytes()); System.out.println(forPath); while (true) { } } @Test//创建多级结点,创建的多级结点中客户机的ip是只存在于叶子结点中的,创建的父节点的数据是空不是null public void testMadeManeyNode() throws Exception { String forPath = client.create().creatingParentsIfNeeded().forPath("/app9/bpp1/bpp2"); System.out.println(forPath); }
复制代码

 

 

 

 

 

 基于javaAPI的Curator的查询结点:

 

复制代码
    @Test//查询数据 get
    public void testGETData() throws Exception {

        byte[] bytes = client.getData().forPath("/app9/bpp1");
        System.out.println(new String(bytes));


    }

    @Test//查询子节点
    public void testFindChild() throws Exception {
        List<String> list = client.getChildren().forPath("/");//这里的/其实是对应的/hui
        System.out.println(list);

    }

    @Test//查询结点的信息
    public void getStatusforNode() throws Exception {

        Stat stat = new Stat();//状态容器
        byte[] bytes = client.getData().storingStatIn(stat).forPath("/app9");//往容器中装填、app9的信息

        System.out.println(stat);//打印状态信息


    }
复制代码

 

 

 

 基于javaAPI的Curator的修改结点:

复制代码
    @Test//修改数据
    public void testChangeData() throws Exception {

        client.setData().forPath("/app9", "yfsn".getBytes());

    }

    @Test//根据版本修改,每一次的修改之后版本都会加1
    public void testSetData() throws Exception {
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath("/app9");
        int version = stat.getVersion();
        System.out.println(version);

        client.setData().withVersion(version).forPath("/app9", "zyh1".getBytes());
        client.getData().storingStatIn(stat).forPath("/app9");
        System.out.println(stat.getVersion() + "后面的状态");
    }
复制代码

 

 

 

 

 

 基于javaAPI的Curator的删除结点:

 

 

复制代码
    @Test//删除没有子节点的结点
    public void testDeleteNode() throws Exception {

        client.delete().forPath("/app1");

    }

    @Test//删除带有子节点的结点
    public void testDeleteNodeWithChilren() throws Exception {
        client.delete().deletingChildrenIfNeeded().forPath("/app9");


    }

    @Test//测试必须删除成功,防止出现网络抖动不能够正常删除
    public void testMustDelete() throws Exception {

        client.delete().guaranteed().forPath("/app2");


    }

@Test//测试回调删除,别忘了学习一下lamada表达式
    public void testDeletedCallback() throws Exception {

        client.delete().guaranteed().inBackground(new BackgroundCallback() {
            @Override
            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                System.out.println("我被删除了");
                System.out.println(curatorEvent.getType());
            }
        }).forPath("/app3");




}
复制代码

 

 

 

 

 

 操作完毕之后关闭连接:

复制代码
 @After
    public void closeClient() {


        if (client != null) {
            client.close();

        }
    }
复制代码

 

posted @ 2020-11-27 19:30  Joker-0927  阅读(134)  评论(0)    收藏  举报
编辑推荐:
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
阅读排行:
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· 我用这13个工具,让开发效率提升了5倍!
· 32岁入行STM32迟吗?
· C#.Net筑基-泛型T & 协变逆变
· Coze工作流实战:一键生成鸡汤视频——厉害的人,早已戒掉情绪
// 侧边栏目录
点击右上角即可分享
微信分享提示