zookeeper-服务器动态上下线监听案例
服务器动态上下线监听案例
需求
某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知 到主节点服务器的上下线。
需求分析

具体实现
先在集群上创建
/servers
节点[zk: localhost:2181(CONNECTED) 10] create /servers "servers" Created /servers
在
IDEA
中创建包明:com.atguigu.case1
服务器端向Zookeeper注册代码
package com.atguigu.case1;
import org.apache.zookeeper.*;
import java.io.IOException;
/**
* @author: fxl
* @Description:
* @Data:Create in 2022-02-05
* @Modified By:
*/
public class DistributeServer {
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 2000;
private ZooKeeper zk;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeServer server = new DistributeServer();
//1. 获取zk链接
server.getConnect();
//2. 注册服务器zk集群
server.regist(args[0]);
//3. 启动业务逻辑(休眠)
server.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void regist(String hostname) throws InterruptedException, KeeperException {
String create = zk.create("/servers/"+hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname + "is online");
}
private void getConnect() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}客户端代码
package com.atguigu.case1;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author: fxl
* @Description:
* @Data:Create in 2022-02-05
* @Modified By:
*/
public class DistributeClient {
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 2000;
private ZooKeeper zk;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeClient client = new DistributeClient();
//1. 获取zk连接
client.getConnnect();
//2.监听/servers下面子节点的增加和删除
client.getServerList();
//3.业务逻辑(休眠)
client.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void getServerList() throws InterruptedException, KeeperException {
List<String> children = zk.getChildren("/servers", true);
ArrayList<String> servers = new ArrayList<>();
for (String child : children) {
byte[] data = zk.getData("/servers/" + child, false, null);
servers.add(new String(data));
}
//打印
System.out.println(servers);
}
private void getConnnect() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
getServerList();
} catch (Exception e) {
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
已经上线