zookeeper-伺服器動態上下線監聽案例
阿新 • • 發佈:2022-02-05
伺服器動態上下線監聽案例
需求
某分散式系統中,主節點可以有多臺,可以動態上下線,任意一臺客戶端都能實時感知 到主節點伺服器的上下線。
需求分析
具體實現
先在叢集上建立
/servers
節點[zk:localhost:2181(CONNECTED)10]create/servers"servers"Created/servers
在
IDEA
中建立包明:com.atguigu.case1
伺服器端向Zookeeper註冊程式碼
packagecom.atguigu.case1;
importorg.apache.zookeeper.*;
importjava.io.IOException;
/**
*@author
*@Description:
*@Data:Createin2022-02-05
*@ModifiedBy:
*/
publicclassDistributeServer{
privateStringconnectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
privateintsessionTimeout=2000;
privateZooKeeperzk;
publicstaticvoidmain(String[]args)throwsIOException,InterruptedException,KeeperException{
DistributeServerserver=new
//1.獲取zk連結
server.getConnect();
//2.註冊伺服器zk叢集
server.regist(args[0]);
//3.啟動業務邏輯(休眠)
server.business();
}
privatevoidbusiness()throwsInterruptedException{
Thread.sleep(Long.MAX_VALUE);
}
privatevoidregist(Stringhostname)throwsInterruptedException,KeeperException{
Stringcreate=zk.create("/servers/"+hostname,hostname.getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname+"isonline"
}
privatevoidgetConnect()throwsIOException{
zk=newZooKeeper(connectString,sessionTimeout,newWatcher(){
@Override
publicvoidprocess(WatchedEventwatchedEvent){
}
});
}
}客戶端程式碼
packagecom.atguigu.case1;
importorg.apache.zookeeper.KeeperException;
importorg.apache.zookeeper.WatchedEvent;
importorg.apache.zookeeper.Watcher;
importorg.apache.zookeeper.ZooKeeper;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
/**
*@author:fxl
*@Description:
*@Data:Createin2022-02-05
*@ModifiedBy:
*/
publicclassDistributeClient{
privateStringconnectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
privateintsessionTimeout=2000;
privateZooKeeperzk;
publicstaticvoidmain(String[]args)throwsIOException,InterruptedException,KeeperException{
DistributeClientclient=newDistributeClient();
//1.獲取zk連線
client.getConnnect();
//2.監聽/servers下面子節點的增加和刪除
client.getServerList();
//3.業務邏輯(休眠)
client.business();
}
privatevoidbusiness()throwsInterruptedException{
Thread.sleep(Long.MAX_VALUE);
}
privatevoidgetServerList()throwsInterruptedException,KeeperException{
List<String>children=zk.getChildren("/servers",true);
ArrayList<String>servers=newArrayList<>();
for(Stringchild:children){
byte[]data=zk.getData("/servers/"+child,false,null);
servers.add(newString(data));
}
//列印
System.out.println(servers);
}
privatevoidgetConnnect()throwsIOException{
zk=newZooKeeper(connectString,sessionTimeout,newWatcher(){
@Override
publicvoidprocess(WatchedEventwatchedEvent){
try{
getServerList();
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
}
測試
在
Linux
命令列上操作增加減少伺服器啟動
DistributeClient
客戶端在
hadoop102
上zk
客戶端/servers
目錄上建立臨時帶序號節點[zk:localhost:2181(CONNECTED)1]create-e-s
/servers/hadoop102"hadoop102"
[zk:localhost:2181(CONNECTED)2]create-e-s
/servers/hadoop103"hadoop103"觀察
IDEA
控制檯變化hadoop102, hadoop103]
執行刪除操作
[zk:localhost:2181(CONNECTED)8]delete/servers/hadoop1020000000000
觀察 Idea 控制檯變化
[hadoop103]
在 Idea 上操作增加減少伺服器
啟動
DistributeClient
客戶端(如果已經啟動過,不需要重啟)啟動
DistributeServer
服務①點選
Edit Configurations
在彈出的視窗中
(Program arguments)
輸入想啟動的主機,例如,hadoop102
回到
DistributeServer
的main
方 法 , 右 鍵 , 在 彈 出 的 窗 口 中 點 擊Run “DistributeServer.main()”
觀察
DistributeServer
控制檯,提示hadoop102 is working
觀察
DistributeClient
控制檯,提示hadoop102
已經上線