在zookeeper中创建子节点/servers用于存储服务器端结点信息
流程:当启动服务器hadoop102时候,会在zookeeper中/servers下创建临时结点:/servers/hadoop102,当hadoop服务器下线时候,临时结点/servers/hadoop102的信息也会随之消失
服务器端
服务器在启动起来的时候,会向zookeeper注解节点信息,方便将来下线时zookeeper通知客户端
代码部分:
package com.atguigu.zk.case1;
import org.apache.zookeeper.*;
import java.io.IOException;
public class DistributeServer {
private String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout=2000;
private ZooKeeper zooKeeper;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeServer server = new DistributeServer();
//1.获取zk连接
server.getConnection();
//2.注册服务器到zk集群
server.register(args[0]);
//3.启动业务逻辑(睡觉)
server.bussiness();
}
private void bussiness() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void register(String hostname) throws InterruptedException, KeeperException {
String s = zooKeeper.create("/servers/"+hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname+"已经上线啦!");
}
private void getConnection() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
}
客户端
客户端从zookeeper获取服务器列表,注册监听信息
代码部分
package com.atguigu.zk.case1;
import jdk.nashorn.internal.ir.CallNode;
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.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class DistributeClient {
private String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout=200000;
private ZooKeeper zooKeeper;
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
DistributeClient client = new DistributeClient();
//获取zk连接
client.getConnection();
//监听/servers的增删改
client.superviser();
//业务逻辑(睡觉)
client.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void superviser() throws InterruptedException, KeeperException, UnsupportedEncodingException {
//用于存储各个节点信息
ArrayList<String> childInfo = new ArrayList<>();
//获取子节点
List<String> children = zooKeeper.getChildren("/servers", true);
for (String child : children) {
byte[] data = zooKeeper.getData("/servers/" + child, false, null);
childInfo.add(new String(data));
}
System.out.println(childInfo);
}
private void getConnection() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
superviser();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (KeeperException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
});
}
}