4.1 Java API操作

  1. import org.apache.zookeeper.*;
  2. import org.apache.zookeeper.data.Stat;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import java.io.IOException;
  6. import java.util.List;
  7. public class TestZookeepr {
  8. private String connect = "192.168.209.131:2181";
  9. private int timeOut = 2000;
  10. private ZooKeeper zkClient;
  11. @Before
  12. public void init() throws IOException {
  13. zkClient = new ZooKeeper(connect, timeOut, new Watcher() {
  14. @Override
  15. public void process(WatchedEvent watchedEvent) {
  16. }
  17. });
  18. }
  19. //创建结点
  20. @Test
  21. public void createNode () throws KeeperException, InterruptedException {
  22. String path = zkClient.create("/zbc","hellozbc".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  23. System.out.println(path);
  24. }
  25. //获取子结点,并监控数据的变化
  26. @Test
  27. public void getDataAndWatch() throws KeeperException, InterruptedException {
  28. List<String> nodes = zkClient.getChildren("/",true);
  29. nodes.forEach(System.out::println);
  30. }
  31. //判断节点是否存在
  32. @Test
  33. public void exist() throws KeeperException, InterruptedException {
  34. Stat stat = zkClient.exists("/zbc",false);
  35. System.out.println(stat == null ? "not exits":"exist");
  36. }
  37. }

4.2 动态获取节点

  1. import org.apache.zookeeper.*;
  2. import java.io.IOException;
  3. public class DistributeServer {
  4. private ZooKeeper zkServer = null;
  5. public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
  6. DistributeServer distributeServer = new DistributeServer();
  7. //1. 链接zookeeper集群
  8. distributeServer.getConnect();
  9. //2. 注册节点
  10. distributeServer.regist(args[0]);
  11. //3.业务逻辑处理
  12. distributeServer.business();
  13. }
  14. private void business() throws InterruptedException {
  15. Thread.sleep(Long.MAX_VALUE);
  16. }
  17. private void regist(String hostNmae) throws KeeperException, InterruptedException {
  18. zkServer.create("/servers/server",hostNmae.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
  19. }
  20. private void getConnect() throws IOException {
  21. // String connectString = "ip:port,ip:port";
  22. String connectString = "";
  23. int timeout = 2000;
  24. new ZooKeeper(connectString, timeout, new Watcher() {
  25. @Override
  26. public void process(WatchedEvent watchedEvent) {
  27. }
  28. });
  29. }
  30. }
  1. import org.apache.zookeeper.*;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class DistributeClient {
  6. private ZooKeeper zkServer = null;
  7. public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
  8. DistributeClient distributeClient = new DistributeClient();
  9. //1. 链接集群
  10. distributeClient.getConnect();
  11. //2. 注册监听
  12. distributeClient.getChildren();
  13. //3. 业务处理
  14. distributeClient.business();
  15. }
  16. private void getChildren() throws KeeperException, InterruptedException {
  17. List<String> childres = zkServer.getChildren("/servers",true);
  18. // 储存服务器节点主机名称
  19. List<String> hots = new ArrayList<String>();
  20. for (String children:childres) {
  21. byte[] data = zkServer.getData("/servers/"+children,false,null);
  22. hots.add(new String(data));
  23. }
  24. // 将所有主机名称打印到控制台
  25. System.out.println(hots);
  26. }
  27. private void business() throws InterruptedException {
  28. Thread.sleep(Long.MAX_VALUE);
  29. }
  30. private void getConnect() throws IOException {
  31. // String connectString = "ip:port,ip:port";
  32. String connectString = "";
  33. int timeout = 2000;
  34. new ZooKeeper(connectString, timeout, new Watcher() {
  35. @Override
  36. public void process(WatchedEvent watchedEvent) {
  37. try {
  38. getChildren();
  39. } catch (KeeperException | InterruptedException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. });
  44. }
  45. }

4.3 企业面试真题

4.3.1 请简述Zookeeper的选举机制
半数机制
4.3.2 Zookeeper监听原理是什么
4.3.3 Zookeeper部分方式有几种?集群中的角色有哪些,集群最少需要几天机器

  1. 部署方式有单机部署和集群部署
  2. 角色:Leader和Follower
  3. 集群最少需要3太机器

    4.3.4 Zookeeper 的常用命令