今天就跟大家聊聊有关ZooKeeper服务器动态上下线代码怎么写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。服务端:package com.hgs.test;import
今天就跟大家聊聊有关ZooKeeper服务器动态上下线代码怎么写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
服务端: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:2181,192.168.6.132:2181,192.168.6.133:2181"; 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();}}}服务端: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.131:2181,192.168.6.132:2181,192.168.6.133:2181"; private final static int sessionTimeout = 2000; private volatile List<String> serverList = new ArrayList<String>(); private final static String parentNode="/servers"; int i = 10;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(st!=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<String> ls = new ArrayList<String>();try {Stat st = zk.exists(parentNode, true);if(st!=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();}}}
看完上述内容,你们对zookeeper服务器动态上下线代码怎么写有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网服务器频道,感谢大家的支持。
--结束END--
本文标题: zookeeper服务器动态上下线代码怎么写
本文链接: https://www.lsjlt.com/news/231580.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0