05.ZooKeeper应用案例

服务器状态感知

1.DistributedServer.java

  1. public class DistributedServer {
  2. private static final String connectString = "mini1:2181,mini2:2181,mini3:2181";
  3. private static final int sessionTimeout = 2000;
  4. private static final String parentNode = "/servers";
  5. private ZooKeeper zk = null;
  6. /**
  7. * 创建到zk的客户端连接
  8. *
  9. * @throws Exception
  10. */
  11. public void getConnect() throws Exception {
  12. zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  13. @Override
  14. public void process(WatchedEvent event) {
  15. // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
  16. System.out.println(event.getType() + "---" + event.getPath());
  17. try {
  18. zk.getChildren("/", true);
  19. } catch (Exception e) {
  20. }
  21. }
  22. });
  23. }
  24. /**
  25. * 向zk集群注册服务器信息
  26. *
  27. * @param hostname
  28. * @throws Exception
  29. */
  30. public void registerServer(String hostname) throws Exception {
  31. String create = zk.create(parentNode + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
  32. System.out.println(hostname + "is online.." + create);
  33. }
  34. /**
  35. * 业务功能
  36. *
  37. * @throws InterruptedException
  38. */
  39. public void handleBussiness(String hostname) throws InterruptedException {
  40. System.out.println(hostname + "start working.....");
  41. Thread.sleep(Long.MAX_VALUE);
  42. }
  43. public static void main(String[] args) throws Exception {
  44. // 获取zk连接
  45. DistributedServer server = new DistributedServer();
  46. server.getConnect();
  47. // 利用zk连接注册服务器信息
  48. server.registerServer(args[0]);
  49. // 启动业务功能
  50. server.handleBussiness(args[0]);
  51. }
  52. }

2.DistributedClient.java

  1. public class DistributedClient {
  2. private static final String connectString = "mini1:2181,mini2:2181,mini3:2181";
  3. private static final int sessionTimeout = 2000;
  4. private static final String parentNode = "/servers";
  5. // 注意:加volatile的意义何在?
  6. private volatile List<String> serverList;
  7. private ZooKeeper zk = null;
  8. /**
  9. * 创建到zk的客户端连接
  10. *
  11. * @throws Exception
  12. */
  13. public void getConnect() throws Exception {
  14. zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  15. @Override
  16. public void process(WatchedEvent event) {
  17. // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
  18. try {
  19. //重新更新服务器列表,并且注册了监听
  20. getServerList();
  21. } catch (Exception e) {
  22. }
  23. }
  24. });
  25. }
  26. /**
  27. * 获取服务器信息列表
  28. *
  29. * @throws Exception
  30. */
  31. public void getServerList() throws Exception {
  32. // 获取服务器子节点信息,并且对父节点进行监听
  33. List<String> children = zk.getChildren(parentNode, true);
  34. // 先创建一个局部的list来存服务器信息
  35. List<String> servers = new ArrayList<String>();
  36. for (String child : children) {
  37. // child只是子节点的节点名
  38. byte[] data = zk.getData(parentNode + "/" + child, false, null);
  39. servers.add(new String(data));
  40. }
  41. // 把servers赋值给成员变量serverList,已提供给各业务线程使用
  42. serverList = servers;
  43. //打印服务器列表
  44. System.out.println(serverList);
  45. }
  46. /**
  47. * 业务功能
  48. *
  49. * @throws InterruptedException
  50. */
  51. public void handleBussiness() throws InterruptedException {
  52. System.out.println("client start working.....");
  53. Thread.sleep(Long.MAX_VALUE);
  54. }
  55. public static void main(String[] args) throws Exception {
  56. // 获取zk连接
  57. DistributedClient client = new DistributedClient();
  58. client.getConnect();
  59. // 获取servers的子节点信息(并监听),从中获取服务器信息列表
  60. client.getServerList();
  61. // 业务线程启动
  62. client.handleBussiness();
  63. }
  64. }
posted @ 2017-03-10 21:47  Wesly186  阅读(125)  评论(0编辑  收藏  举报