安全组端口具体打开端口的步骤,与这个类似https://blog.csdn.net/IAMZTDSF/article/details/123654734?spm=1001.2014.3001.5501
3.导入依赖4.创建类baseZookeeper(将其中的端口改成你自己的)org.apache.zookeeper zookeeper3.4.6
5.测试启动,如图即为成功 6.修改main方法测试查询一条数据package zookeeper_demo; import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; public class baseZookeeper implements Watcher{private static ZooKeeper zookeeper;private static final int SESSION_TIME_OUT = 2000; private CountDownLatch countDownLatch = new CountDownLatch(1); public void process(WatchedEvent event) { if (event.getState() == KeeperState.SyncConnected) { System.out.println("Watch received event"); countDownLatch.countDown(); } } public void connectZookeeper(String host) throws Exception{ zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this); countDownLatch.await(); System.out.println("zookeeper connection success"); } public String createNode(String path,String data) throws Exception{ return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } public ListgetChildren(String path) throws KeeperException, InterruptedException{ List children = zookeeper.getChildren(path, false); return children; } public String getData(String path) throws KeeperException, InterruptedException{ byte[] data = zookeeper.getData(path, false, null); if (data == null) { return ""; } return new String(data); } public Stat setData(String path,String data) throws KeeperException, InterruptedException{ Stat stat = zookeeper.setData(path, data.getBytes(), -1); return stat; } public void deleteNode(String path) throws InterruptedException, KeeperException{ zookeeper.delete(path, -1); } public String getCTime(String path) throws KeeperException, InterruptedException{ Stat stat = zookeeper.exists(path, false); return String.valueOf(stat.getCtime()); } public Integer getChildrenNum(String path) throws KeeperException, InterruptedException{ int childenNum = zookeeper.getChildren(path, false).size(); return childenNum; } public void closeConnection() throws InterruptedException{ if (zookeeper != null) { zookeeper.close(); } } public static void main(String[] args) throws Exception { baseZookeeper zookeeper = new baseZookeeper(); zookeeper.connectZookeeper("192.168.126.128:2181"); //改端口 List children = zookeeper.getChildren("/"); System.out.println(children); } }



