5.zookeeper application case distributed server dynamic on-line sensing

Source: Internet
Author: User

Zookeeper application case of the distributed server dynamically on the offline sense, when the server on-line and offline when the client can perceive, and what other machines online. And the zookeeper management of the server to monitor the node;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/79/13/wKioL1aIH_mh9v_SAAEiKkz3bWs961.png "title=" Capture Xxxxxxxxx. PNG "alt=" Wkiol1aih_mh9v_saaeikkz3bws961.png "/>

Code implementation: Client

Whenever the server has servers on-line or offline at the client can be detected through the listener

package org.zookeeper.anli;import java.util.arraylist;import java.util.list;import  org.apache.zookeeper.watchedevent;import org.apache.zookeeper.watcher;import  Org.apache.zookeeper.watcher.event.eventtype;import org.apache.zookeeper.zookeeper;public class  ZookeeperClient {private volatile List<String> servers = null; private zookeeper zk = null;//  Get ZK Connection private void getzkclient ()  throws  exception {zk = new zookeeper (globalconstants.zkhosts,  Globalconstants.sessiontimeout, new watcher ()  {@Overridepublic  void process ( watchedevent event)  {if (Event.gettype ()  == eventtype.none)  return;try {//  Get a list of new servers, re-register for monitoring updateservers ();}  catch  (exception e)  {e.printstacktrace ();}});} /** *  getting online server information from ZK  */public void updateservers ()  throws exception {//  gets all the child nodes from the Servers parent node and registers the listener List<string> children = zk.getchildren ( Globalconstants.parentznodepath, true); Arraylist<string> serverlist = new arraylist<string> ();for  (String  Child : children)  {byte[] data = zk.getdata (Globalconstants.parentznodepath  +  "/"  + child, false, null); Serverlist.add (new string (data));}   If the client is a multithreaded program and each thread competes to access the servers list, a servers variable is decorated with volatile in the member//  and in this method of updating the server information, is updated with a temporary list variable servers = serverlist;//  the server list information after the update is printed on the console watch for  (string server  : servers)  {system.out.println (server);}} /** *  business logic  *  *  @throws  InterruptedException */private void  RequestService ()  throws interruptedexception {thread.sleep (long.max_value);} Public static void main (String[] args) &NBSP;THROWS&NBSp Exception {zookeeperclient client = new zookeeperclient ();//  First constructs a ZK connection client.getzkclient ();//  Gets the server list client.updateservers ();//  the client enters the orchestration, Request Server service Client.requestservice ();}}


Code implementation: Service side

Package org.zookeeper.anli;import java.util.list;import org.apache.zookeeper.createmode;import  org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs.Ids;import  Org.apache.zookeeper.zookeeper;public class zookeeperserver {private zookeeper zk  = null;private volatile List<String> servers = null;private  Void getzkclient ()  throws exception{//server does not need to do any monitoring in the requirements Zk = new zookeeper ( Globalconstants.zkhosts, globalconstants.sessiontimeout,null);} /** *  creating sub-nodes  *  @throws  interruptedexception  * @ to/servers in zookeeper Throws keeperexception  */private void connectzk (string servername,string  Port)  throws exception{//first create the parent node if (zk.exists (globalconstants.parentznodepath, false)  ==  null) {zk.create (globalconstants.parentznodepath, null, ids.open_acl_unsafe, createmode.persistent);} Connect ZK to create Znodezk.create (globalconstants.parentznodepath+ "/",  (servername+ ":" +port). GetBytes (),  Ids.open_acl_unsafe, createmode.ephemeral_sequential); System.out.println ("server "  + serverName +  " is online  ..."); Server-specific business processing functions Private void handle (string servername)  throws exception { System.out.println ("server "  + serverName +  " is waiting for task  process ... "); Thread.Sleep (Long.max_value);} Public static void main (String[] args)  throws exception {zookeeperserver  server = new zookeeperserver ();//Get Client connection to zookeeper Communication server.getzkclient ();// Start on the zookeeper to register the server information, Parameters 1:  server hostname      Parameter 2: server's listening port//simulation   Create a server under Servers node   server01 1212  represents a server on-line Server.connectzk (args[0],args[1]);//Enter the business logic processing process Server.handle (args[0]);}}


The jar package that needs to be used:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/79/13/wKioL1aILoTAZK4MAAAN31cBo9s940.png "title=" Capture 8. PNG "alt=" Wkiol1ailotazk4maaan31cbo9s940.png "/>


Test method, the server side into a jar package client into a jar package to drop the server on the server repeatedly execute the JAR package

Execute Client.jar package in Windows View server on line sense

5.zookeeper application case distributed server dynamic on-line sensing

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.