1、创建服务端ZookeeperServer
/**
* 监听服务器动态上下线案例:服务端
*/
public class ZookeeperServer {
private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient;
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
BasicConfigurator.configure();
ZookeeperServer server = new ZookeeperServer();
//1、连接Zookeeper集群
server.getConnect();
//2、注册节点
server.regist(args[0]);
//3、业务逻辑
server.business();
}
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
}
});
}
private void regist(String hostname) throws KeeperException, InterruptedException {
zkClient.create("/servers/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname+" is online");
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
}
2、创建客户端ZookeeperClient
/**
* 监听服务器动态上下线案例:客户端
*/
public class ZookeeperClient {
private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient;
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
BasicConfigurator.configure();
ZookeeperClient client = new ZookeeperClient();
//1、获取Zookeeper连接
client.getConnect();
//2、注册监听
client.getChildren();
//3、业务逻辑
client.business();
}
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
try {
getChildren();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
private void getChildren() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/servers", true);
//存储服务器节点主机名称集合
ArrayList<String> hosts = new ArrayList<String>();
for(String child : children){
byte[] data = zkClient.getData("/servers/" + child, false, null);
hosts.add(new String(data));
}
//打印所有在线主机名称
System.out.println(hosts);
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
}
3、测试Zookeeper
public class TestZookeeper {
private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient;
/**获取Zookeeper客户端对象*/
@Before
public void init() throws IOException {
BasicConfigurator.configure();
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
List<String> children;
try {
children = zkClient.getChildren("/", true);
for(String child : children){
System.out.println(child);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
/**创建Zookeeper节点*/
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = zkClient.create("/shanxi", "guxiang".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(path);
}
/**获取子节点并监控节点的变化*/
@Test
public void getDataAndWatch() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", true);
for(String child : children){
System.out.println(child);
}
Thread.sleep(Long.MAX_VALUE);
}
/**判断Zookeeper节点是否存在*/
@Test
public void exist() throws KeeperException, InterruptedException {
Stat exists = zkClient.exists("/shanxi", false);
System.out.println(exists==null?"节点不存在":"节点存在");
}
}