zookeeper 服务器动态上下线程序小Demo

服务器端


public class DistributedServer {
    
    private ZooKeeper zKeeper = null;

    private static final String connectString = "192.168.203.129:2181,192.168.203.130:2181,192.168.203.131:2181";
    private static final int sessionTimeout = 2000;
    private static final String parentNode = "/servers";
    /*
     * 创建到zk的客户端连接
     */
    public void getContect() throws IOException {
        zKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
            }
            
        });
    }
    /*
     * 向zk集群注册服务器信息
     */
    public void registerServer(String hostname ) throws  Exception{
     
        String create= zKeeper.create(parentNode+"/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname + "is online..." + create);
    }
    /*
     * 业务功能
     */
    
    public void handleBussiness(String hostname) throws Exception {
        System.out.println(hostname + "is Working.....");
        
        Thread.sleep(Long.MAX_VALUE);
    }
    public static void main(String[] args) throws Exception {
        //获取zk连接
        DistributedServer server = new DistributedServer();
        server.getContect();
        //注册服务器信息
        server.registerServer(args[0]);
        //业务逻辑
        server.handleBussiness(args[0]);
    }
}

客户端


public class DistributedClient {
     

    private ZooKeeper zKeeper = null;

    private static final String connectString = "192.168.203.129:2181,192.168.203.130:2181,192.168.203.131:2181";
    private static final int sessionTimeout = 2000;
    private static final String parentNode = "/servers";
    //加volatile 
    private volatile List<String> serversList;
    /*
     * 创建到zk的客户端连接
     */
    public void getContect() throws IOException {
        zKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                try {
                    //重新更新服务器列表,并且注册监听
                    getServerList();
                } catch (Exception e) {
                     
                }
            }
            
        });
    }
    /*
     * 获取服务器信息列表
     */
    public void getServerList() throws  Exception {
        List<String> children = zKeeper.getChildren(parentNode, true);
        List<String> servers = new ArrayList<String>();
        for(String child:children) {
            byte[] data =zKeeper.getData(parentNode+"/"+child, false, null);
            servers.add(new String(data));
            
        }
        serversList = servers;
        System.out.println(serversList);
    }

    /*
     * 业务功能
     */
    
    public void handleBussiness() throws Exception {
        System.out.println("client is Working.....");
        
        Thread.sleep(Long.MAX_VALUE);
    }
    
      public static void main(String[] args) throws  Exception{
          //获取zk连接
          DistributedClient client = new DistributedClient();
          client.getContect();
          //获取servers 的节点信息(并监听),从中获取服务器信息列表
          
          client.getServerList();
          client.handleBussiness();
       }
}

猜你喜欢

转载自blog.csdn.net/ieiqny1/article/details/83824705