ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 5000, null); String lockPath = zooKeeper.create("/locks/lock-", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); List<String> children = zooKeeper.getChildren("/locks", false); boolean isMinNode = true; for(String child : children) { if(child.compareTo(lockPath) < 0) { isMinNode = false; break; } } if(!isMinNode) { Stat stat = zooKeeper.exists(lockPath, new LockWatcher()); synchronized (lockWatcher) { lockWatcher.wait(); } } ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 5000, null); Stat stat = zooKeeper.exists("/config", false); if(stat == null) { zooKeeper.create("/config", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } byte[] newConfig = "newConfigData".getBytes(); zooKeeper.setData("/config", newConfig, -1); byte[] configData = zooKeeper.getData("/config", false, null); String config = new String(configData);


上一篇:
下一篇:
切换中文