public void addChildWatcher(String path) throws Exception { // path=/webServiceCenter/userCenter/tc.dd.test.do // 对path下的子节点进行监控,即就是对ip监控 final PathChildrenCache cache = new PathChildrenCache(this.client, path, true); cache.start(StartMode.POST_INITIALIZED_EVENT); // ppt中需要讲StartMode // System.out.println(cache.getCurrentData().size()); cache .getListenable() .addListener( new PathChildrenCacheListener() { public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { if (event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)) { System.out.println("客户端子节点cache初始化数据完成"); // System.out.println("size="+cache.getCurrentData().size()); } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) { // System.out.println("添加子节点:"+event.getData().getPath()); // System.out.println("修改子节点数据:"+new String(event.getData().getData())); updateLocalService(event.getData().getPath(), 0); } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)) { System.out.println("删除子节点:" + event.getData().getPath()); // if(services.containsKey(event.getData().getPath())) // servicesByP.containsKey(event.getData().) updateLocalService(event.getData().getPath(), 1); } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) { // System.out.println("修改子节点数据:"+event.getData().getPath()); // System.out.println("修改子节点数据:"+new String(event.getData().getData())); } } }); }
public static void main(String[] args) throws Exception { CuratorFramework client = null; PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); processCommands(client, cache); } finally { Closeables.closeQuietly(cache); Closeables.closeQuietly(client); } }
/** * 监听某个节点下的直接一级节点 孩子节点的创建,删除,更新 * * @throws Exception */ @Test public void testWatcherPath() throws Exception { // 2.Register watcher PathChildrenCache watcher = new PathChildrenCache( client, ZK_PATH, true // if cache data ); watcher .getListenable() .addListener( new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { ChildData data = event.getData(); if (data == null) { System.out.println("No data in event[" + event + "]"); } else { System.out.println( "Receive event: " + "type=[" + event.getType() + "]" + ", path=[" + data.getPath() + "]" + ", data=[" + new String(data.getData()) + "]" + ", stat=[" + data.getStat() + "]"); } } }); watcher.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); System.out.println("Register zk watcher successfully!"); Thread.sleep(Integer.MAX_VALUE); }
@LifecycleStart public void start() throws IOException { synchronized (lock) { if (started) { return; } log.info("Starting zkCoordinator for server[%s]", me.getName()); final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName()); final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName()); final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName()); loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, loadingExec); try { curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient()); curator .newNamespaceAwareEnsurePath(servedSegmentsLocation) .ensure(curator.getZookeeperClient()); curator .newNamespaceAwareEnsurePath(liveSegmentsLocation) .ensure(curator.getZookeeperClient()); loadLocalCache(); loadQueueCache .getListenable() .addListener( new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { final ChildData child = event.getData(); switch (event.getType()) { case CHILD_ADDED: final String path = child.getPath(); final DataSegmentChangeRequest request = jsonMapper.readValue(child.getData(), DataSegmentChangeRequest.class); log.info("New request[%s] with zNode[%s].", request.asString(), path); try { request.go( getDataSegmentChangeHandler(), new DataSegmentChangeCallback() { boolean hasRun = false; @Override public void execute() { try { if (!hasRun) { curator.delete().guaranteed().forPath(path); log.info("Completed request [%s]", request.asString()); hasRun = true; } } catch (Exception e) { try { curator.delete().guaranteed().forPath(path); } catch (Exception e1) { log.error( e1, "Failed to delete zNode[%s], but ignoring exception.", path); } log.error(e, "Exception while removing zNode[%s]", path); throw Throwables.propagate(e); } } }); } catch (Exception e) { try { curator.delete().guaranteed().forPath(path); } catch (Exception e1) { log.error( e1, "Failed to delete zNode[%s], but ignoring exception.", path); } log.makeAlert(e, "Segment load/unload: uncaught exception.") .addData("node", path) .addData("nodeProperties", request) .emit(); } break; case CHILD_REMOVED: log.info("zNode[%s] was removed", event.getData().getPath()); break; default: log.info("Ignoring event[%s]", event); } } }); loadQueueCache.start(); } catch (Exception e) { Throwables.propagateIfPossible(e, IOException.class); throw Throwables.propagate(e); } started = true; } }
@LifecycleStart public void start() throws IOException { log.info("Starting zkCoordinator for server[%s]", me); synchronized (lock) { if (started) { return; } final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName()); final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName()); final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName()); loadQueueCache = new PathChildrenCache( curator, loadQueueLocation, true, true, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ZkCoordinator-%s").build()); try { config.getInfoDir().mkdirs(); curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient()); curator .newNamespaceAwareEnsurePath(servedSegmentsLocation) .ensure(curator.getZookeeperClient()); curator .newNamespaceAwareEnsurePath(liveSegmentsLocation) .ensure(curator.getZookeeperClient()); loadCache(); loadQueueCache .getListenable() .addListener( new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { final ChildData child = event.getData(); switch (event.getType()) { case CHILD_ADDED: final String path = child.getPath(); final DataSegmentChangeRequest segment = jsonMapper.readValue(child.getData(), DataSegmentChangeRequest.class); log.info("New node[%s] with segmentClass[%s]", path, segment.getClass()); try { segment.go(ZkCoordinator.this); curator.delete().guaranteed().forPath(path); log.info("Completed processing for node[%s]", path); } catch (Exception e) { try { curator.delete().guaranteed().forPath(path); } catch (Exception e1) { log.info( e1, "Failed to delete node[%s], but ignoring exception.", path); } log.makeAlert(e, "Segment load/unload: uncaught exception.") .addData("node", path) .addData("nodeProperties", segment) .emit(); } break; case CHILD_REMOVED: log.info("%s was removed", event.getData().getPath()); break; default: log.info("Ignoring event[%s]", event); } } }); loadQueueCache.start(); } catch (Exception e) { Throwables.propagateIfPossible(e, IOException.class); throw Throwables.propagate(e); } started = true; } }