work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Zookeeper Java API调用

Posted on 2018-11-05 22:27  work hard work smart  阅读(223)  评论(0编辑  收藏  举报

引入zookeeper-3.4.11.jar

public class ZooKeeperTest  implements Watcher{

    //public final static String zkServerPath = "12.45.67.80:2181";
    //集群,使用逗号分隔
    public final static String zkServerPath = "12.45.67.80:2181,12.45.67.80:2182,12.45.67.80:2183";
    public final  static  int timeout = 5000;
    public static void main(String[] args) throws  Exception{
        ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new ZooKeeperTest());

        System.out.println("客户端开始连接Zookeeper服务器。。。");
        System.out.println("连接状态:" +zk.getState());
        new Thread().sleep(5000);
        System.out.println("连接状态:" +zk.getState());


    }

    public void process(WatchedEvent event) {
        System.out.println("接收到watch通知:" + event.toString());
    }
}