ZooKeeper典型案例-------------------分布式服务器上下线动态感知系统

分为服务器端和客户端,原理如下:
客户端:
//1.连接zookeeper

//2.查询在线服务器,并注册监听

//3.挑选服务器请求业务

服务器端:

//1.获取zk连接

//2.注册服务器信息

//3.等待请求,处理业务
客户端:

public class Consume {
    List<String> onlineServers;
    private static final String connectString = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
     ZooKeeper zk = null;
  public static void main(String[] args) throws Exception {
      Consume consume = new Consume();
    //1.连接zookeeper
      consume.connectZK();
     //2.查询在线服务器,并注册监听
      consume.getOnlineServers();
      //3.挑选服务器请求业务
      consume.requestServers();
}

  public void connectZK() throws Exception {
      zk = new ZooKeeper(connectString, 2000, new Watcher() {

        public void process(WatchedEvent event) {
            // TODO Auto-generated method stub
            try {
                getOnlineServers();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
 }

  public void getOnlineServers() throws Exception, InterruptedException {
      List<String> serverList = new ArrayList<String>();//这里面放一个List防止用户挑选服务器的时候这边还一直在运行
      List<String> children = zk.getChildren("/servers", true);
      for (String child : children) {
          byte[] data = zk.getData("/servers/"+child, true, null);
          String server = new String(data);
          serverList.add(server);
    }
      onlineServers = serverList;//将这一阶段的值放入总的里面
  }

  public void requestServers() throws Exception {
      Random random = new Random();
      while(true) {
          Thread.sleep(2000);
          if(onlineServers.size()==0) {
              System.out.println("还没有在线的服务器");
              continue;
          }
          String server = onlineServers.get(random.nextInt(onlineServers.size()));
          System.out.println("本次挑选了服务器"+server);

      }
  }
}
客户端
    private static final String connectString = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
     ZooKeeper zk = null;
    public static void main(String[] args) throws Exception {
        Provider provider = new Provider();


        //1.获取zk连接
        provider.connectZK();


        //2.注册服务器信息

        provider.registServerInfo();

        //3.等待请求,处理业务
        provider.handleService();
    }
     public void connectZK() throws Exception {
          zk = new ZooKeeper(connectString, 2000, new Watcher() {

            public void process(WatchedEvent event) {
                // TODO Auto-generated method stub

            }
        });
     }

     public void registServerInfo() throws Exception {
         String hostName = InetAddress.getLocalHost().getHostName();

         if(zk.exists("/servers", false)==null) {
             zk.create("/servers", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
         }
         String path = zk.create("/servers/server", (hostName+":"+8080).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
         System.out.println(hostName+"成功注册了一个节点"+path);

     }

     public void handleService() throws Exception {
         ServerSocket ss = new ServerSocket(8080);
         while(true) {
             Socket sc = ss.accept();
         }
     }

}

猜你喜欢

转载自blog.csdn.net/qq_41166135/article/details/82356415