日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zookpeer实现对服务器动态上下线的监听

發(fā)布時間:2024/8/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zookpeer实现对服务器动态上下线的监听 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

服務器動態(tài)上下線程序的工作機制

服務器代碼:
補充:volatile關鍵字:java中一切都是對象,當多個線程操作同一個對象時候,該對象會放在堆內(nèi)存中,而多個線程相當于在多個棧中,當A線程想要去除對象中的數(shù)據(jù)并修改,往往不是直接拿對象的值直接改變其內(nèi)容,而是先把中的對象賦值一份到A線程棧中,然后再對賦值的對象進行修改,最后把賦值對象與堆中的對象進行比較,不同則修改堆中對象,這樣當多個線程訪問對象時會存在,當A線程修改了堆中賦值對象的值,但還沒來得及修改堆中的對象,而B線程此時拿到的仍然是原對象值并沒有發(fā)生更改,為了避免這種問題。因此同volatile關鍵字讓線程直接獲取對象并修改內(nèi)容無需賦值一份再去修改。

package cn.itcast.bigdata.zkdist;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper;public class DistributedServer {private static final String connectString = "mini1:2181,mini2:2181,mini3:2181";private static final int sessionTimeout = 2000;private static final String parentNode = "/servers";private ZooKeeper zk = null;/*** 創(chuàng)建到zk的客戶端連接* * @throws Exception*/public void getConnect() throws Exception {zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {// 收到事件通知后的回調(diào)函數(shù)(應該是我們自己的事件處理邏輯)System.out.println(event.getType() + "---" + event.getPath());try {zk.getChildren("/", true);} catch (Exception e) {}}});}/*** 向zk集群注冊服務器信息* * @param hostname* @throws Exception*/public void registerServer(String hostname) throws Exception {String create = zk.create(parentNode + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);//當服務斷掉時ZooKeeper將此臨時節(jié)點刪除,這樣client就不會得到服務的信息了System.out.println(hostname + "is online.." + create);}/*** 業(yè)務功能* * @throws InterruptedException*/public void handleBussiness(String hostname) throws InterruptedException {System.out.println(hostname + "start working.....");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws Exception {// 獲取zk連接DistributedServer server = new DistributedServer();server.getConnect();// 利用zk連接注冊服務器信息server.registerServer(args[0]);// 啟動業(yè)務功能server.handleBussiness(args[0]);}}

輸出:當args[0]等于weijie1時候,輸出如下

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. 45null weijie1is online/servers/server0000000006 weijie1 is starting

客戶端代碼:

package com.itcast.zookpeer.zk;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;public class DistributedClient {private static final String connectString = "weijie1:2181,weijie2:2181,weijie3:2181";private static final int sessionTimeout = 2000;private static final String parentNode = "/servers";private static volatile List<String> serverList; ZooKeeper zk = null;//創(chuàng)建客戶端連接private void getConnection() throws Exception {zk = new ZooKeeper(connectString, sessionTimeout, new Watcher(){@Overridepublic void process(WatchedEvent event) {System.out.println(event.getType()+"--"+event.getPath());try{getServerList();}catch(Exception e){}}});}//獲取服務器信息列表private void getServerList() throws Exception {//獲取服務器子節(jié)點的信息,并且對父節(jié)點進行監(jiān)聽List<String> server = zk.getChildren(parentNode, true);//先創(chuàng)建一個list來存儲服務器信息List<String> servers = new ArrayList<String>();for (String string : server) {byte[] data = zk.getData(parentNode+"/"+string, false, null);servers.add(new String(data));//new String對data進行解析轉成字符串類型}serverList = servers;System.out.println(serverList);}private void BundleBusiness() throws Exception {System.out.println("is starting");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws Exception {DistributedClient distribute = new DistributedClient();distribute.getConnection();distribute.getServerList();distribute.BundleBusiness();}}

輸出:

  • 當啟動一次服務器時候此時輸出結果
    log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
    log4j:WARN Please initialize the log4j system properly.
    None--null
    [weijie1]
    is starting
    [weijie1]
  • 再啟動一次DistributedServer時候輸出
    log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
    log4j:WARN Please initialize the log4j system properly.
    None--null
    [weijie1]
    is starting
    [weijie1]
    NodeChildrenChanged--/servers
    [weijie1, weijie1]
    分析:當啟動兩次服務器此時在servers目錄下又創(chuàng)建了一個臨時帶序號的節(jié)點,此時總共有兩個臨時節(jié)點,因此輸出主機名兩次,因為在List<String> server = zk.getChildren(parentNode, true);中多父節(jié)點”/servers”進行了監(jiān)聽。
  • 總結

    以上是生活随笔為你收集整理的zookpeer实现对服务器动态上下线的监听的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。