连不上Zookeeper
老师测试一下
package com.imooc.zkjavaapi.watcher; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; /** * 描述: 连接Watcher */ public class ConnectWatcher implements Watcher { @Override public void process(WatchedEvent watchedEvent) { System.out.println("ConnectWatcher的process被调用了"); if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { System.out.println("连接成功"); } if (watchedEvent.getState() == Event.KeeperState.Closed) { System.out.println("连接关闭"); } } }
package com.imooc.zkjavaapi; import com.imooc.zkjavaapi.watcher.ConnectWatcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.ZooKeeper.States; import java.io.IOException; /** * 描述: 连接到ZK */ public class FirstConnect { public static void main(String[] args) throws IOException, InterruptedException { ConnectWatcher connectWatcher = new ConnectWatcher(); ZooKeeper zk = new ZooKeeper(ZKConstant.ZK_HOST, ZKConstant.CONNECT_TIMEOUT, connectWatcher); System.out.println("客户端开始连接ZK服务器"); States state = zk.getState(); System.out.println(state); Thread.sleep(2000); state = zk.getState(); System.out.println(state); Thread.sleep(2000); zk.close(); } }
package com.imooc.zkjavaapi; /** * 描述: ZK常量类 */ public class ZKConstant { public static final String ZK_HOST = "192.168.172.134:2181"; public static final Integer CONNECT_TIMEOUT = 3000; }
5
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星