Zookeeper Applications-Monitoring

Source: Internet
Author: User

Server-side: Monitor child node changes of the parent node on ZK

  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.