1 客户端能实时洞察服务器上下线的情况
2 服务端程序
//DistributedServer.java
package demo2;
import org.apache.zookeeper.*;
import java.io.IOException;
public class DistributedServer {
private ZooKeeper zk = null;
private static final String connectString = "192.168.154.131:2181,192.168.154.131:2181,192.168.154.131:2181";
private static final int sessionTimeout = 200000;
private static final String parentNode="/servers";
public void getConnect() throws IOException {
zk=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println(event.getType()+"---"+event.getPath());
try{
zk.getChildren("/",true);
}catch (Exception e){
}
}
});
}
/*
* 向zk集群注册服务器
* */
public void registerServer(String hostname) throws KeeperException, InterruptedException {
String create= zk.create(parentNode+"/server",hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname+" is online..."+create);
}
/*
* 业务功能
* */
public void handleBussiness(String hostname) throws InterruptedException {
System.out.println(hostname+" start work......");
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
//获取zk连接
DistributedServer server=new DistributedServer();
server.getConnect();
//利用zk连接注册服务器信息
server.registerServer(args[0]);
//启动业务功能
server.handleBussiness(args[0]);
}
}
3 客户端程序
//DistributedClient.java
package demo2;
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;
public class DistributedClient {
private ZooKeeper zk = null;
private static final String connectString = "192.168.154.131:2181,192.168.154.131:2181,192.168.154.131:2181";
private static final int sessionTimeout = 200000;
private static final String parentNode = "/servers";
//
private volatile List<String> serverList;
public void getConnect() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
//重新更新服务器列表,并且注册了监听
getServerList();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/*
*
* 获取服务器信息列表
* */
public void getServerList() throws KeeperException, InterruptedException {
//获取服务器子节点信息,对父节点监听
List<String> children = zk.getChildren(parentNode, true);
List<String> servers = new ArrayList<>();
for (String child : children) {
//child 只是子节点名
byte[] data = zk.getData(parentNode + "/" + child, false, null);
servers.add(new String(data));
}
//把servers赋值给成员变量serverList,以提供给各业务线程使用
serverList=servers;
//
System.out.println(serverList);
}
/*
* 业务功能
* */
public void handleBussiness() throws InterruptedException {
System.out.println(" client start work......");
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args)throws Exception {
//获取zk连接
DistributedClient client = new DistributedClient();
client.getConnect();
//获取servers的子节点信息,从中获取服务器列表信息
client.getServerList();
//业务线程启动
client.handleBussiness();
}
}
4 效果测试