ZooKeeper(二) idea中使用Java操作zookeeper

本章介绍使在idea环境中使用maven工程进行连接zookeeper,并进行简单的增删查改操作。

一、使用的pom依赖

添加zookeeper依赖,以及进行测试的junit测试依赖。



    4.0.0

    com.xiaohui.zk
    zkdemo1
    1.0-SNAPSHOT

    
        
            org.apache.zookeeper
            zookeeper
            3.4.7
        
        
            junit
            junit
            4.12
        
    

二、添加测试类进行连接、新增、修改、查询等操作

1,进行连接zookeeper,并进行创建一个 /huizi 节点

在创建时参数 ZooDefs.Ids.OPEN_ACL_UNSAFE 表示节点的访问权限,OPEN_ACL_UNSAFE 表示可以被所有的客户端节点访问到。


public class FirstTest {

    @Test
    public void test() throws Exception {
        //1, 创建zk连接
        ZooKeeper zooKeeper = new ZooKeeper("172.18.255.118:2181", 2000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("收到监听:type:"+watchedEvent.getType()+", Path:"+watchedEvent.getPath()+", State:"+watchedEvent.getState());
            }
        });

        //2,创建父节点
        String path = zooKeeper.create("/huizi",
                "nodeValue".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
                CreateMode.PERSISTENT);//四种模式
        System.out.println("路径:"+path);

    }

}

执行结束后可以通过linux客户端工具在安装目录bin下使用 ./zkCli.sh 进入客户端命令控制台  命令 ls / 查看成功创建节点 huizi 。

2,创建子节点代码

package com.xiaohui.zk.test;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class FirstTest {

    @Test
    public void test() throws Exception {
        //1, 创建zk连接
        ZooKeeper zooKeeper = new ZooKeeper("172.18.255.118:2181", 2000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("收到监听:type:"+watchedEvent.getType()+", Path:"+watchedEvent.getPath()+", State:"+watchedEvent.getState());
            }
        });

        //2,创建父节点
//        String path = zooKeeper.create("/huizi",
//                "nodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("路径:"+path);


        //3, 创建子节点
        String childenPath = zooKeeper.create("/huizi/childenNode",
                "childenNodeValue".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
                CreateMode.PERSISTENT);//四种模式
        System.out.println("childenPath路径:"+childenPath);

    }
}

控制台使用 ls /huizi 查看其下面的子节点:

3,打印父节点和子节点中的值

package com.xiaohui.zk.test;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import java.io.IOException;
import java.util.List;

public class FirstTest {

    @Test
    public void test() throws Exception {
        //1, 创建zk连接
        ZooKeeper zooKeeper = new ZooKeeper("172.18.255.118:2181", 2000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("收到监听:type:"+watchedEvent.getType()+", Path:"+watchedEvent.getPath()+", State:"+watchedEvent.getState());
            }
        });

        //2,创建父节点
//        String path = zooKeeper.create("/huizi",
//                "nodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("路径:"+path);


        //3, 创建子节点
//        String childenPath = zooKeeper.create("/huizi/childenNode",
//                "childenNodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("childenPath路径:"+childenPath);

        //4, 获取节点中的值(父节点和子节点)
        byte[] value = zooKeeper.getData("/huizi",false,null);
        System.out.println("p:"+new String(value));

        List children = zooKeeper.getChildren("/huizi", false);
        for (String child : children) {
            System.out.println("c:"+child);
        }
    }
}

打印如下:

收到监听:type:None, Path:null, State:SyncConnected
p:nodeValue
c:childenNode

4,修改节点中的值

在set 方法中  最后一个参数 表示版本号,-1 表示可以被所有版本都可以匹配到。

package com.xiaohui.zk.test;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import java.io.IOException;
import java.util.List;

public class FirstTest {

    @Test
    public void test() throws Exception {
        //1, 创建zk连接
        ZooKeeper zooKeeper = new ZooKeeper("172.18.255.118:2181", 2000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("收到监听:type:"+watchedEvent.getType()+", Path:"+watchedEvent.getPath()+", State:"+watchedEvent.getState());
            }
        });

        //2,创建父节点
//        String path = zooKeeper.create("/huizi",
//                "nodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("路径:"+path);


        //3, 创建子节点
//        String childenPath = zooKeeper.create("/huizi/childenNode",
//                "childenNodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("childenPath路径:"+childenPath);

        //4, 获取节点中的值(父节点和子节点)
//        byte[] value = zooKeeper.getData("/huizi",false,null);
//        System.out.println("p:"+new String(value));
//
//        List children = zooKeeper.getChildren("/huizi", false);
//        for (String child : children) {
//            System.out.println("c:"+child);
//        }

        //5, 修改节点的值
        zooKeeper.setData("/huizi", "newValue".getBytes(), -1);
        byte[] value = zooKeeper.getData("/huizi",false,null);
        System.out.println("data:"+new String(value));
    }
}

5,删除某个节点,并查看其是否存在

package com.xiaohui.zk.test;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class FirstTest {


    @Test
    public void test() throws Exception {
        //1, 创建zk连接
        ZooKeeper zooKeeper = new ZooKeeper("172.18.255.118:2181", 2000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("收到监听:type:"+watchedEvent.getType()+", Path:"+watchedEvent.getPath()+", State:"+watchedEvent.getState());
            }
        });

        //2,创建父节点
//        String path = zooKeeper.create("/huizi",
//                "nodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("路径:"+path);


        //3, 创建子节点
//        String childenPath = zooKeeper.create("/huizi/childenNode",
//                "childenNodeValue".getBytes(),
//                ZooDefs.Ids.OPEN_ACL_UNSAFE,  //
//                CreateMode.PERSISTENT);//四种模式
//        System.out.println("childenPath路径:"+childenPath);

        //4, 获取节点中的值(父节点和子节点)
//        byte[] value = zooKeeper.getData("/huizi",false,null);
//        System.out.println("p:"+new String(value));
//
//        List children = zooKeeper.getChildren("/huizi", false);
//        for (String child : children) {
//            System.out.println("c:"+child);
//        }

        //5, 修改节点的值
//        zooKeeper.setData("/huizi", "newValue".getBytes(), -1);
//        byte[] value = zooKeeper.getData("/huizi",false,null);
//        System.out.println("data:"+new String(value));

        //6, 判断某个节点是否存在
        Stat exists = zooKeeper.exists("/huizi/childenNode", false);
        System.out.println(exists);
        //7, 删除节点
        zooKeeper.delete("/huizi/childenNode", -1);
        Stat exists1 = zooKeeper.exists("/huizi/childenNode", false);
        System.out.println(exists1);
    }
}

打印如下:

收到监听:type:None, Path:null, State:SyncConnected
4294967326,4294967326,1596599387683,1596599387683,0,0,0,0,16,0,4294967326

null

 

你可能感兴趣的:(ZK+Dubbo)