zoukankan      html  css  js  c++  java
  • 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();
    
            }
        }
    迎风少年
  • 相关阅读:
    uva 147 Dollars
    hdu 2069 Coin Change(完全背包)
    hdu 1708 Fibonacci String
    hdu 1568 Fibonacci
    hdu 1316 How Many Fibs?
    poj 1958 Strange Towers of Hanoi
    poj 3601Tower of Hanoi
    poj 3572 Hanoi Tower
    poj 1920 Towers of Hanoi
    筛选法——素数打表
  • 原文地址:https://www.cnblogs.com/ZYH-coder0927/p/14049909.html
Copyright © 2011-2022 走看看