Server-side: Monitor child node changes of the parent node on ZK
Package Monitor;import Java.util.list;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.timeunit;import Org.apache.zookeeper.createmode;import org.apache.zookeeper.WatchedEvent; Import Org.apache.zookeeper.watcher;import Org.apache.zookeeper.watcher.event.keeperstate;import Org.apache.zookeeper.zoodefs;import org.apache.zookeeper.zookeeper;/** * Monitor the upper and lower lines of the client * @author LISG * */public class Servermonitor {public static final string HOSTS = "Vm1";p rivate ZooKeeper ZK = null;public static final string Parent_path = "/monitor";p rivate static int client_count = 0;public Servermonitor () {final Countdownlatch CDL = new Countdownlatch (1) ; try {ZK = new ZooKeeper (HOSTS, Watcher, new () {@Overridepublic void process (Watchedevent event) {if (Keeperstate.sync Connected.equals (Event.getstate ())) {Cdl.countdown ();}}); Cdl.await ();//Create parent node if (Zk.exists (Parent_path, false) = = null) {zk.create (Parent_path, "". GetBytes (), ZooDefs.Ids.OPEN _acl_unsafe, createmode.persistent);}} catch (excePtion e) {e.printstacktrace ();}} public void Run () {try {Zk.getchildren (Parent_path, New Clientchangewatcher ())} catch (Exception e) {e.printstacktrace ( );}} Class Clientchangewatcher implements Watcher {@Overridepublic void process (Watchedevent event) {try {if ( Event.EventType.NodeChildrenChanged.equals (Event.gettype ())) {//Get child nodes while registering for monitoring final list<string> children = Zk.getchildren (Parent_path, this), if (Client_count > Children.size ()) {System.out.println ("client Downline");} else { SYSTEM.OUT.PRINTLN ("Have Client on-line");} Client_count = Children.size ();}} catch (Exception e) {e.printstacktrace ();}}} public static void Main (string[] args) {new Servermonitor (). run (); try {TimeUnit.DAYS.sleep (1);} catch ( Interruptedexception e) {e.printstacktrace ();}}}
Client: Create ephemeral child nodes on parent node when on-line
Package Monitor;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.timeunit;import Org.apache.zookeeper.createmode;import Org.apache.zookeeper.watchedevent;import Org.apache.zookeeper.Watcher; Import Org.apache.zookeeper.watcher.event.keeperstate;import Org.apache.zookeeper.zoodefs.ids;import org.apache.zookeeper.zookeeper;/** * Client * Create a short child node on ZK when on-line * @author LISG * */public class Client {public static final String client_path_prefix = "cli-";p ublic static void Main (string[] args) {final Countdownlatch CDL = new Countdownlatch ( 1); try {ZooKeeper ZK = new ZooKeeper (servermonitor.hosts, New Watcher () {@Overridepublic void process (Watchedevent E Vent) {if (KeeperState.SyncConnected.equals (Event.getstate ())) {Cdl.countdown ();}}); Cdl.await ();//Create parent node Zk.create (Servermonitor.parent_path + "/" + Client_path_prefix, "". GetBytes (), Ids.open_acl_ UNSAFE, createmode.ephemeral_sequential); TimeUnit.SECONDS.sleep (5); Zk.close ();} catch (Exception e) {e.printstacktrace ();}}}
From for notes (Wiz)
List of attachments
Zookeeper Applications-Monitoring