Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write the dynamic online and offline code of zookeeper server

Shulou Source: shulou.com Published: 2022-06-01 07:59:58 10月03日 Update

Today, I will talk to you about how to write the dynamic online and offline code of the zookeeper server. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.

Server: package com.hgs.test;import java.io.IOException;import java.util.List;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.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;public class ZKserver {private ZooKeeper zk = null Private final static String connectString = "192.168.6.131 servers 2181192.168.6.132"; private final static int sessionTimeout = 2000; private final static String parentNode= "/ servers"; public static void main (String [] args) {ZKserver zkserver = new ZKserver (); zkserver.getConnect (); zkserver.register (args [0]); zkserver.doSomthing () } public void getConnect () {try {zk = new ZooKeeper (connectString, sessionTimeout, new Watcher () {@ Overridepublic void process (WatchedEvent event) {try {if (zk.exists (parentNode,false) = = null) {System.out.println (zk.getChildren ("/", true));} else {if (event.getType () = EventType.NodeChildrenChanged) {try {System.out.println (zk.getChildren (parentNode, true) + event.getType (). ToString () + "+ (event.getType () = = EventType.NodeChildrenChanged)) } catch (Exception e) {/ / TODO Auto-generated catch blocke.printStackTrace ();} catch (Exception e2) {/ / TODO Auto-generated catch blocke2.printStackTrace ();});} catch (IOException e) {/ / TODO Auto-generated catch blocke.printStackTrace ();}} public void register (String host) {try {if (zk.exists (parentNode,false) = = null) {zk.create (parentNode, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT) } zk.create (parentNode+ "/ server", host.getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);} catch (Exception e) {/ / TODO Auto-generated catch blockSystem.out.println ("register sever failed!"); System.exit (1);} public void doSomthing () {System.out.println ("doSomething."); try {Thread.sleep (Long.MAX_VALUE);} catch (InterruptedException e) {/ / TODO Auto-generated catch blocke.printStackTrace () } server: package com.hgs.test;import java.io.IOException;import java.util.ArrayList;import java.util.List;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;public class ZKclient {private ZooKeeper zk = null; private final static String connectString = "192.168.6.131VOR 2181192.168.6.132VOR 2181192.168.6.133RU 2181" Private final static int sessionTimeout = 2000; private volatile List serverList = new ArrayList (); private final static String parentNode= "/ servers"; int I = 10 ZKclient client public static void main (String [] args) {ZKclient client = new ZKclient (); client.getConnect (); client.getServerList (); client.doSomething ();} public void getConnect () {try {zk = new ZooKeeper (connectString, sessionTimeout, new Watcher () {@ Overridepublic void process (WatchedEvent event) {try {Stat st = zk.exists (parentNode, true) / / System.out.println (st); if (stalled null) {getServerList ();} else {zk.getChildren ("/", true);}} catch (KeeperException | InterruptedException e) {/ / TODO Auto-generated catch blocke.printStackTrace ();});} catch (IOException e) {/ / TODO Auto-generated catch blocke.printStackTrace ();}} public void getServerList () {List ls = new ArrayList (); try {Stat st = zk.exists (parentNode, true) If (stalled null) {for (String str:zk.getChildren (parentNode, true)) {String hosts = new String (zk.getData (parentNode+ "/" + str, false, null)); ls.add (hosts);}} else {zk.getChildren ("/", true);}} catch (KeeperException | InterruptedException e) {/ / TODO Auto-generated catch blocke.printStackTrace ();} serverList = ls;System.out.println (ls);} public void doSomething () {System.out.println (serverList) Try {Thread.sleep (Long.MAX_VALUE);} catch (InterruptedException e) {/ / TODO Auto-generated catch blocke.printStackTrace ();} after reading the above, do you have any further understanding of how to write the dynamic uplink and unloading code of the zookeeper server? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Services content offline code dynamic server more knowledge articles industry information information channels channels access support related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker MySQL Microsoft Redmi