4.1 Java API操作
import org.apache.zookeeper.*;import org.apache.zookeeper.data.Stat;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.util.List;public class TestZookeepr {private String connect = "192.168.209.131:2181";private int timeOut = 2000;private ZooKeeper zkClient;@Beforepublic void init() throws IOException {zkClient = new ZooKeeper(connect, timeOut, new Watcher() {@Overridepublic void process(WatchedEvent watchedEvent) {}});}//创建结点@Testpublic void createNode () throws KeeperException, InterruptedException {String path = zkClient.create("/zbc","hellozbc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println(path);}//获取子结点,并监控数据的变化@Testpublic void getDataAndWatch() throws KeeperException, InterruptedException {List<String> nodes = zkClient.getChildren("/",true);nodes.forEach(System.out::println);}//判断节点是否存在@Testpublic void exist() throws KeeperException, InterruptedException {Stat stat = zkClient.exists("/zbc",false);System.out.println(stat == null ? "not exits":"exist");}}
4.2 动态获取节点
import org.apache.zookeeper.*;import java.io.IOException;public class DistributeServer {private ZooKeeper zkServer = null;public static void main(String[] args) throws IOException, KeeperException, InterruptedException {DistributeServer distributeServer = new DistributeServer();//1. 链接zookeeper集群distributeServer.getConnect();//2. 注册节点distributeServer.regist(args[0]);//3.业务逻辑处理distributeServer.business();}private void business() throws InterruptedException {Thread.sleep(Long.MAX_VALUE);}private void regist(String hostNmae) throws KeeperException, InterruptedException {zkServer.create("/servers/server",hostNmae.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);}private void getConnect() throws IOException {// String connectString = "ip:port,ip:port";String connectString = "";int timeout = 2000;new ZooKeeper(connectString, timeout, new Watcher() {@Overridepublic void process(WatchedEvent watchedEvent) {}});}}
import org.apache.zookeeper.*;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class DistributeClient {private ZooKeeper zkServer = null;public static void main(String[] args) throws IOException, KeeperException, InterruptedException {DistributeClient distributeClient = new DistributeClient();//1. 链接集群distributeClient.getConnect();//2. 注册监听distributeClient.getChildren();//3. 业务处理distributeClient.business();}private void getChildren() throws KeeperException, InterruptedException {List<String> childres = zkServer.getChildren("/servers",true);// 储存服务器节点主机名称List<String> hots = new ArrayList<String>();for (String children:childres) {byte[] data = zkServer.getData("/servers/"+children,false,null);hots.add(new String(data));}// 将所有主机名称打印到控制台System.out.println(hots);}private void business() throws InterruptedException {Thread.sleep(Long.MAX_VALUE);}private void getConnect() throws IOException {// String connectString = "ip:port,ip:port";String connectString = "";int timeout = 2000;new ZooKeeper(connectString, timeout, new Watcher() {@Overridepublic void process(WatchedEvent watchedEvent) {try {getChildren();} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}});}}
4.3 企业面试真题
4.3.1 请简述Zookeeper的选举机制
半数机制
4.3.2 Zookeeper监听原理是什么
4.3.3 Zookeeper部分方式有几种?集群中的角色有哪些,集群最少需要几天机器
- 部署方式有单机部署和集群部署
- 角色:Leader和Follower
集群最少需要3太机器
4.3.4 Zookeeper 的常用命令
