In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.