ZooKeeper
1.安装 https://www.cnblogs.com/mh-study/p/10368891.html
https://blog.csdn.net/qiunian144084/article/details/79192819 windows下集群
https://www.cnblogs.com/mustone/p/7373671.html
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; public class Demo1 { private ZooKeeper zookeeper; private String oldValue = ""; private String newValue = ""; public ZooKeeper zkConnect( ) throws IOException { String path = "127.0.0.1:2181"; zookeeper = new ZooKeeper(path, 20 * 1000, null); return zookeeper; } public void createZnode(String path, byte[] value, Watcher watcher, CreateMode node ) throws KeeperException, InterruptedException { zookeeper.create(path, value, ZooDefs.Ids.OPEN_ACL_UNSAFE, node); } public String getZnodeValue(final String path ) throws KeeperException, InterruptedException { byte[] data = zookeeper.getData(path, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { triggerWatch(path); } }, new Stat()); oldValue = new String(data); return new String(data); } public boolean triggerWatch (String path) { byte[] data = new byte[0]; try { data = zookeeper.getData(path, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { triggerWatch(path); } }, new Stat()); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } newValue = new String(data); if (oldValue.equals(newValue)) { System.out.println("on change"); return false; } else { System.out.println("oldvalue: " + oldValue + "new value: " + newValue); oldValue = newValue; return true; } } public static void main(String[] args) throws IOException, KeeperException, InterruptedException { //创建 Demo1 zookeeperWatchDemo = new Demo1(); ZooKeeper zooKeeper = zookeeperWatchDemo.zkConnect(); String path = "/root"; String value = "rootValue"; String child1value="child1 API测试"; String child1path = "/root/child1"; String child2value="child2 API测试"; String child2path = "/root/child2"; if (zooKeeper.exists(path, false) == null) { zookeeperWatchDemo.createZnode(path, value.getBytes(), null, CreateMode.PERSISTENT); zookeeperWatchDemo.createZnode(child1path, child1value.getBytes(), null, CreateMode.PERSISTENT); zookeeperWatchDemo.createZnode(child2path, child2value.getBytes(), null, CreateMode.PERSISTENT); } String znodeValue = zookeeperWatchDemo.getZnodeValue(path); String znodeValue1 = zookeeperWatchDemo.getZnodeValue(child1path); String znodeValue2 = zookeeperWatchDemo.getZnodeValue(child2path); System.out.println(znodeValue); System.out.println(znodeValue1); System.out.println(znodeValue2); Thread.sleep(1000 * 60 * 50); } }
我只是偶尔安静下来,对过去的种种思忖一番。那些曾经的旧时光里即便有过天真愚钝,也不值得谴责。毕竟,往后的日子,还很长。不断鼓励自己,
天一亮,又是崭新的起点,又是未知的征程(上校9)
逆水行舟,不进,则退!