前提:具体操作见
https://blog.csdn.net/weixin_41265887/article/details/113974315
1.创建DistributeServer.java类
package cn.tod;
import org.apache.zookeeper.*;
import java.io.IOException;
public class DistributeServer {
private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private static int sessionTimeout = 4000;
private ZooKeeper zk = null;
private String parentNode = "/servers";
public void getConnect() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
}
});
}
public void registServer(String hostname) throws KeeperException, InterruptedException {
String node = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname + " is online " + node);
}
public void business(String hostname) throws InterruptedException {
System.out.println(hostname + " is working ...");
Thread.sleep(Long.MAX_VALUE);
}
public void close() throws InterruptedException {
zk.close();
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeServer server = new DistributeServer();
server.getConnect();
server.registServer(args[0]);
server.business(args[0]);
server.close();
}
}
2.创建DistributeClient.java类
package cn.tod;
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 DistributeClient {
private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private static int sessionTimeout = 4000;
private ZooKeeper zk = null;
private String parentNode = "/servers";
public void getConnect() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
try {
getServerList();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
public void getServerList() throws KeeperException, InterruptedException {
List<String> children = zk.getChildren(parentNode, true);
ArrayList<String> servers = new ArrayList<String>();
for (String child : children) {
byte[] data = zk.getData(parentNode + "/" + child, false, null);
servers.add(new String(data));
}
System.out.println(servers);
}
public void business() throws InterruptedException {
System.out.println("Client is working ...");
Thread.sleep(Long.MAX_VALUE);
}
public void close() throws InterruptedException {
zk.close();
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeClient client = new DistributeClient();
client.getConnect();
client.getServerList();
client.business();
client.close();
}
}
3.在ZooKeeper集群中创建节点servers
[root@hadoop102 bin]# pwd
/opt/module/zookeeper-3.4.10/bin
[root@hadoop102 bin]# zkCli.sh
[zk: localhost:2181(CONNECTED) 0] create /servers "servers"
4.在IDEA中配置多个程序运行,在DistributeServer程序的Program arguments中添加参数,运行即可模拟。
版权声明:本文为weixin_41265887原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。