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() { @Override public 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 block e.printStackTrace(); } } } } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(); } } }); } catch (IOException e) { // TODO Auto-generated catch block e.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 block System.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 block e.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(){ @Override public 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 block e.printStackTrace(); } } }); } catch (IOException e) { // TODO Auto-generated catch block e.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 block e.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 block e.printStackTrace(); } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2199889/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 基於Apache Zookeeper手寫實現動態配置中心(純程式碼實踐)Apache
- Zookeeper原始碼(啟動+選舉)原始碼
- 動態執行c#程式碼C#
- 不用程式碼趣講 ZooKeeper 叢集
- 【zookeeper原始碼】啟動流程詳解原始碼
- 什麼是動態IPvps?如何連線vps伺服器?伺服器
- 動態圖和靜態圖的程式碼區別
- 使用Python程式碼遠端連線伺服器Python伺服器
- 動態連結串列的建立(程式碼)
- 同步線上伺服器程式碼到web節點指令碼伺服器Web指令碼
- 伺服器狀態碼伺服器
- 中斷與數碼管動態顯示程式碼
- 如何用程式碼動態生成ABAP型別型別
- 動態adsl伺服器有什麼好的連線方法嗎?伺服器
- iOS使用指令碼跟隨工程程式碼動態生成FrameworkiOS指令碼Framework
- Zookeeper原始碼分析(二) —– zookeeper日誌原始碼
- Zookeeper原始碼分析(二) ----- zookeeper日誌原始碼
- 短視訊開發,製作上下動態的箭頭動畫動畫
- Springboot原始碼——應用程式上下文分析Spring Boot原始碼
- C#呼叫IronPython動態執行Python程式碼C#Python
- php程式碼審計之——phpstorm動態除錯PHPORM除錯
- 藉助 Webpack 靜態分析能力實現程式碼動態載入Web
- Zookeeper原始碼分析原始碼
- 動態規劃-----線性動態規劃
- 伺服器遠端連線操作方法及程式碼分享伺服器
- Java程式碼加密支援Android App Bundle動態化框架,守護核心程式碼安全Java加密AndroidAPP框架
- spring+groovy實現動態程式碼注入執行Spring
- android程式碼中動態調整圖片的位置Android
- Unity學習-程式碼中動態生成預製體Unity
- 鴻蒙開發Hvigor外掛動態生成程式碼鴻蒙Go
- 靜態程式碼塊
- 原生JS動態載入JS、CSS檔案及程式碼指令碼JSCSS指令碼
- Abp vnext EFCore 實現動態上下文DbSet踩坑記
- 動態監控程式
- 線上直播原始碼,JS動態效果之,側邊欄滾動固定效果原始碼JS
- 使用 git hook 實現伺服器程式碼自動部署GitHook伺服器
- 動態開點線段樹
- 服務優雅上下線