zookeeper combat operations

A: zookeeper monitor server node data change

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ConfigApp1 {
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        //连接zookeeper服务器
        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 5000, 
                new Watcher() {
                    public void process(WatchedEvent event) {
                        if (KeeperState.SyncConnected == event.getState()) {    //zk连接成功通知事件
                            if ( EventType.None == event.getType() && null ==event.getPath ()) { 
                                connectedSemaphore.countDown (); 
                                System.out.println ( "===========" ); 
                            } 
                        } 
                        
                    } 
                }); 

        connectedSemaphore.await (); 
        // Create a node app1, not ACL access control, EPHEMERAL: temporary node 
        zk.create ( "/ app1", "app1Date" .getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 
        
        // register listeners that node 
        zk.exists ( "/ App1", new new WatcherClass (ZK)); 
        
        // after a 2 second delay begins to change data 
        TimeUnit.SECONDS.sleep (2 );
         for(int i = 0; i < 10; i++) {
            TimeUnit.SECONDS.sleep(1);
            String s = ("app" + i * 10);
            zk.setData("/app1", s.getBytes(), -1);
            System.out.println("数据改变了:"+s);
        }
        System.in.read();
    }
    
    static class WatcherClass implements Watcher {
        private ZooKeeper zk;
        
        public WatcherClass(ZooKeeper zk) {
            this.zk = zk;
        }

        @Override
        public void Process (WatchedEvent the arg0) {
             the try {
                 byte [] B = zk.getData ( "/ App1", to false , null ); 
                System.out.println ( "data change notification:" + new new String (B)); 
                
                // after obtaining the data, the node listens again 
                zk.exists ( "/ App1", new new WatcherClass (ZK)); 
            } the catch (KeeperException E) { 
                e.printStackTrace (); 
            } the catch (InterruptedException E) { 
                e.printStackTrace () ; 
            } 
        } 
        
    } 
}

Screenshot console results:

 

 II: Cluster Management

  Application Clusters, we often need to let each machine knows which cluster (or other dependent of one cluster) machine is alive and quickly in the case of cluster because the machine is down, the broken chain network and other reasons can not longer be notice to every machine

 Thinking: analog with three classes into three server to connect zookeeper, zookeeper three servers monitor the root node, each on-line server creates a temporary node under the root node zookeeper, so that the three servers zookeeper watcher the root node can dynamically perceive the situation on the offline server.

 

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

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.ZooKeeper;

public class Cluster1 {

    private static final int zkSessionTimeOut = 5000;
    
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        //连接zookeeper服务器
        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", zkSessionTimeOut, 
                new Watcher() {
                    public void process(WatchedEvent event) {
                        if (KeeperState.SyncConnected == event.getState()) {    //zk连接成功通知事件
                            if ( EventType.None == event.getType() && null == event.getPath() ) {
                                connectedSemaphore.countDown();
                                System.out.println("===========");
                            }
                        }
                        
                    }
                });

        connectedSemaphore.await();
        
        
        Stat stat = zk.exists("/root", true);
         if(stat == null) {
             System.out.println("/ root" + "path does not exist, create the node" );
              // create node root, not ACL access control, PERSISTENTAL: permanent node only permanent nodes can create a child node of the temporary node 
             zk.create ( " / the root "," rootDate " .getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
         } 
        String clusterPath = zk.create (" / the root / cluster1 "," cluster1Date " .getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode. Ephemeral); 
        System.out.println (clusterPath); 
        
        ZkWatcher zkWatcher = new new ZkWatcher (ZK); 
        List <String> ClusterList = zk.getChildren ( "/ the root" , zkWatcher); 
        
        System.out.println ( "**** ************ ");
        for(String str : clusterList) {
            System.out.println("cluster:" + str);
        }
        System.out.println("****************");
        
        while(true) {
            
        }
    }

}
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;

public class Cluster2 {

    private static final int zkSessionTimeOut = 5000;
    
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        //连接zookeeper服务器
        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", zkSessionTimeOut, 
                new Watcher() {
                    public void process(WatchedEvent event) {
                        if (KeeperState.SyncConnected == event.getState()) {    //zk连接成功通知事件
                            if ( EventType.None == event.getType() && null == event.getPath() ) {
                                connectedSemaphore.countDown();
                                System.out.println("===========");
                            }
                        }
                        
                    }
                });

        connectedSemaphore.await();
        
        Stat stat = zk.exists("/root", true);
         if(stat == null) {
             System.out.println("/root" + "路径不存在,请先创建该节点");
             zk.create("/root", "rootDate".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
         }
        String clusterPath = zk.create("/root/cluster2", "cluster2Date".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(clusterPath);
        
        ZkWatcher zkWatcher = new ZkWatcher(zk);
        List<String> clusterList = zk.getChildren("/root", zkWatcher);
        
        System.out.println("****************");
        for(String str : clusterList) {
            System.out.println("cluster:" + str);
        }
        System.out.println("****************");
        
        while(true) {
            
        }
    }

}
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;

public class Cluster3 {

    private static final int zkSessionTimeOut = 5000;
    
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
    
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        //连接zookeeper服务器
        ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", zkSessionTimeOut, 
                new Watcher() {
                    public void process(WatchedEvent event) {
                        if (KeeperState.SyncConnected == event.getState()) {    //zk连接成功通知事件
                            if ( EventType.None == event.getType() && null == event.getPath() ) {
                                connectedSemaphore.countDown();
                                System.out.println("===========");
                            }
                        }
                        
                    }
                });

        connectedSemaphore.await();
        
         Stat stat = zk.exists("/root", true);
         if(stat == null) {
             System.out.println("/root" + "路径不存在,请先创建该节点");
             zk.create("/root", "rootDate".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
         }
        String clusterPath = zk.create("/root/cluster3", "cluster3Date".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(clusterPath);
        
        ZkWatcher zkWatcher = new ZkWatcher(zk);
        List<String> clusterList = zk.getChildren("/root", zkWatcher);
        
        System.out.println("****************");
        for(String str : clusterList) {
            System.out.println("cluster:" + str);
        }
        System.out.println("****************");
        
        while(true) {
            
        }
    }

}
import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;

public class ZkWatcher implements Watcher{

    private ZooKeeper zk;
    
    public ZkWatcher(ZooKeeper zk) {
        this.zk = zk;
    }
    
    @Override
    public void process(WatchedEvent event) {
        if(EventType.NodeChildrenChanged.equals(event.getType())) {
            List<String> clusterList = null;
            try {
                clusterList = zk.getChildren("/root", this);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("****************");
            System.out.println("changed");
            for(String str : clusterList) {
                System.out.println("cluster:" + str);
            }
            System.out.println("****************");
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/myseries/p/11294373.html