转载  Zookeeper之ZKClient的使用详解

分类: 2017-05-06T17:07:01    331人阅读   

关于Zookeeper的本地集群测试环境搭建,请百度或者加群交流(391053981

实战源码:git@git.oschina.net:xiaowei2014/frameworkdubboseries.git

1.创建会话

public class createSession {  
  
    public static void main(String[] args) {  
        //zk集群的地址  
        String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
      
        /** 
         * 创建会话 
         * new SerializableSerializer() 创建序列化器接口,用来序列化和反序列化 
         */  
        ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
          
        System.out.println("conneted ok!");  
          
    }  
}  

2.创建节点


public class createNode {  
  
    public static void main(String[] args) {  
        //zk集群的地址  
        String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
        ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
          
        System.out.println("conneted ok!");  
          
        User user = new User();  
        user.setId(1);  
        user.setName("testUser");  
          
        /** 
         * "/testUserNode" :节点的地址 
         * user:数据的对象 
         * CreateMode.PERSISTENT:创建的节点类型 
         */  
        String path = zkClient.create("/testUserNode", user, CreateMode.PERSISTENT);  
        //输出创建节点的路径  
        System.out.println("created path:"+path);  
    }  
}  
//注意:一定要实现序列化接口  implements Serializable  
public class User implements Serializable{  
      
    private Integer id;  
    private String name;  
      
    public Integer getId() {  
        return id;  
    }  
    public void setId(Integer id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
}  
3.获取节点中的数据



public class getData {  
    public static void main(String[] args) {  
                //zk集群的地址  
                String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
                ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
                System.out.println("conneted ok!");  
                  
                Stat stat = new Stat();  
                //获取 节点中的对象  
                User  user = zkClient.readData("/testUserNode",stat);  
                System.out.println(user.getName());  
                System.out.println(stat);  
    }  
}  


4.判断节点是否存在


public class getData {  
    public static void main(String[] args) {  
                //zk集群的地址  
                String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
                ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
                System.out.println("conneted ok!");  
                  
                boolean e = zkClient.exists("/testUserNode");  
                //返回 true表示节点存在 ,false表示不存在  
                System.out.println(e);  
    }  
}  


5.删除节点


public class getData {  
    public static void main(String[] args) {  
                //zk集群的地址  
                String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
                ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
                System.out.println("conneted ok!");  
                  
                //删除单独一个节点,返回true表示成功  
                boolean e1 = zkClient.delete("/testUserNode");  
                //删除含有子节点的节点  
                boolean e2 = zkClient.deleteRecursive("/test");  
                  
                //返回 true表示节点成功 ,false表示删除失败  
                System.out.println(e1);  
    }  
}  
6.更新数据



public static void main(String[] args) {  
                //zk集群的地址  
                String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
                ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
                System.out.println("conneted ok!");  
                  
                User user = new User();  
                user.setId(2);  
                user.setName("testUser2");  
                /** 
                 * testUserNode 节点的路径 
                 * user 传入的数据对象 
                 */  
                zkClient.writeData("/testUserNode", user);  
    }  


7.订阅节点的信息改变(创建节点,删除节点,添加子节点)


public class SubscribeChildChanges {  
    private static class ZKChildListener implements IZkChildListener{  
        /** 
         * handleChildChange: 用来处理服务器端发送过来的通知 
         * parentPath:对应的父节点的路径 
         * currentChilds:子节点的相对路径 
         */  
        public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {  
              
            System.out.println(parentPath);  
            System.out.println(currentChilds.toString());  
              
        }  
          
    }  
      
    public static void main(String[] args) throws InterruptedException {  
        //zk集群的地址  
        String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
        ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
        System.out.println("conneted ok!");  
        /** 
         * "/testUserNode" 监听的节点,可以是现在存在的也可以是不存在的 
         */  
        zkClient.subscribeChildChanges("/testUserNode3", new ZKChildListener());  
        Thread.sleep(Integer.MAX_VALUE);  
    }  
}  

8.订阅节点的数据内容的变化


public class SubscribeDataChanges {  
    private static class ZKDataListener implements IZkDataListener{  
  
        public void handleDataChange(String dataPath, Object data) throws Exception {  
              
            System.out.println(dataPath+":"+data.toString());  
        }  
  
        public void handleDataDeleted(String dataPath) throws Exception {  
              
            System.out.println(dataPath);  
              
        }  
         
          
    }  
      
    public static void main(String[] args) throws InterruptedException {  
        //zk集群的地址  
        String ZKServers = "192.168.30.164:2181,192.168.30.165:2181,192.168.30.166:2181";  
        ZkClient zkClient = new ZkClient(ZKServers,10000,10000,new SerializableSerializer());  
        System.out.println("conneted ok!");  
  
        zkClient.subscribeDataChanges("/testUserNode", new ZKDataListener());  
        Thread.sleep(Integer.MAX_VALUE);  
          
    }  
}  


分享到: