ZKServer.java
package com.univer.server;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZKServer {
private String connectString = "hadoop:201:2181,hadoop202:2181,hadoop203:2181";
private int sessionTimeout = 2000;
private ZooKeeper zooKeeper = null;
private String parentNode = "/servers";
public void getZKClient() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("get watcher,msg :");
System.out.println("\t eventType : " + event.getType());
System.out.println("\t eventPath : " + event.getPath());
}
});
}
public void regist(String hostname) throws KeeperException, InterruptedException {
String create = zooKeeper.create(parentNode + "/" + hostname, ("this is " + hostname).getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname + " is online " + create);
}
public void business(String hostname) throws InterruptedException {
System.out.println("server " + hostname + " is begin!");
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// 获取连接
ZKServer zkServer = new ZKServer();
zkServer.getZKClient();
// 注册
zkServer.regist(args[0]);
// server业务
zkServer.business(args[0]);
}
}
ZKClient
package com.univer.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class ZKClient {
private String connectString = "hadoop:201:2181,hadoop202:2181,hadoop203:2181";
private int sessionTimeout = 2000;
private ZooKeeper zooKeeper = null;
private String parentNode = "/servers";
public void getZKClient() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("get watcher,msg :");
System.out.println("\t eventType : " + event.getType());
System.out.println("\t eventPath : " + event.getPath());
try {
getServers();
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
});
}
public void getServers() throws KeeperException, InterruptedException {
List<String> children = zooKeeper.getChildren(parentNode, true);
List<String> servers = new ArrayList<>();
List<String> cList = new ArrayList<>();
for (String child : children) {
cList.add(child);
byte[] data = zooKeeper.getData(parentNode + "/" + child, false, null);
servers.add(new String(data));
}
System.out.println("children:" + cList);
System.out.println("data:" + servers);
}
public void business() throws InterruptedException {
System.out.println("client is working!");
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// 获取连接
ZKClient zkClient = new ZKClient();
zkClient.getZKClient();
// 监听节点变化
zkClient.getServers();
// client业务
zkClient.business();
}
}