引入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()); } }