集群之 -- zookeeper -JAVA小例子 节点的操作

@Test
public void tt() {
    String connectionString = "192.168.1.100:2181,192.168.1.100:2182,192.168.1.100:2183";
    int n =44;
    try {
        ZooKeeper zk = new ZooKeeper(connectionString, 50000, new Watcher(){
            @Override
            public void process(WatchedEvent e) {
                System.out.println("mypath: " + e.getPath()+" mystate:"+e.getState()+" mytype:"+e.getType());
                WatcherEvent we = e.getWrapper();
                System.out.println(" we state:"+we.getState()+" we path:"+we.getPath()+" we type:"+we.getType());
            }
        });
        System.out.println("zk已连接=======================");
        ZooBean zb = new ZooBean();
        zb.setA("cs===");
        zb.setB("fark===");
        String s = JsonUtil.toJson(zb);
        zk.create("/root" + n, s.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        System.out.println("已创建一个bean==========================");
        Stat stat = new Stat();
        byte[] bytes = zk.getData("/root" + n, true, stat);
        String r = new String(bytes);
        System.out.println("r:" + r);
        ZooBean zb1 = JsonUtil.toObject(r, ZooBean.class);
        System.out.println("获得结果:"+zb1.getA()+" "+zb1.getB());
        System.out.println("修改成字符串===================");
        zk.setData("/root" + n, ("byupdate" + n).getBytes(), -1);
        byte[] bytes1 = zk.getData("/root" + n, true, stat);
        String r1 = new String(bytes1);
        System.out.println("输出字符串:"+r1);
        System.out.println("创建儿子节点=====");
        zk.create("/root" + n + "/child",
                "child".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        zk.create("/root" + n + "/child1",
                "child1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        /**
         * 转自说明:http://www.cnblogs.com/shengkejava/p/5611671.html
         *
         * 提供默认的权限OPEN_ACL_UNSAFE、CREATOR_ALL_ACL、READ_ACL_UNSAFE
         OPEN_ACL_UNSAFE:完全开放
         CREATOR_ALL_ACL:创建该znode的连接拥有所有权限
         READ_ACL_UNSAFE:所有的客户端都可读

         * createMode:节点类型
         PERSISTENT:持久化节点
         PERSISTENT_SEQUENTIAL:持久化有序节点
         EPHEMERAL:临时节点(连接断开自动删除)
         EPHEMERAL_SEQUENTIAL:临时有序节点(连接断开自动删除)
         */
        List childs = zk.getChildren("/root" + n, new Watcher() {
            public void process(WatchedEvent we) {
                System.out.println(" we state:" + we.getState() + " we path:" + we.getPath() + " we type:" + we.getType());
            }
        });
        Stat st =  zk.exists("/root"+n+"/child", true);
        System.out.println("儿子个数:"+st.getNumChildren());
        Stat st1 = zk.exists("/root"+n,true);
        System.out.println("你是老子的儿子,有几个兄弟:"+st1.getNumChildren());
        System.out.println("删除儿子=======");
        zk.delete("/root" + n + "/child", -1);
        zk.delete("/root" + n + "/child1", -1);
        System.out.println("删除老子======");
        zk.delete("/root" + n, -1);
        System.out.println("好了,关闭,一了百了=========");
        zk.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

你可能感兴趣的:(JAVA,集群)