Java Code Examples for org.I0Itec.zkclient.ZkClient#getChildren()

The following examples show how to use org.I0Itec.zkclient.ZkClient#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpServiceDiscovery.java    From soul with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    Boolean register = env.getProperty("soul.http.register", Boolean.class, false);
    if (!register) {
        return;
    }
    String zookeeperUrl = env.getProperty("soul.http.zookeeperUrl", "");
    if (StringUtils.isNoneBlank(zookeeperUrl)) {
        zkClient = new ZkClient(zookeeperUrl, 5000, 2000);
        boolean exists = zkClient.exists(ROOT);
        if (!exists) {
            zkClient.createPersistent(ROOT, true);
        }
        contextPathList = zkClient.getChildren(ROOT);
        updateServerNode(contextPathList);
        zkClient.subscribeChildChanges(ROOT, (parentPath, childs) -> {
            final List<String> addSubscribePath = addSubscribePath(contextPathList, childs);
            updateServerNode(addSubscribePath);
            contextPathList = childs;
        });
    }
}
 
Example 2
Source File: ConfigStateHolder.java    From jeesuite-config with Apache License 2.0 6 votes vote down vote up
private static void syncConfigIfMiss(String profile,ZkClient zkClient){

	if(!zkClient.exists(ZK_ROOT_PATH)){
		zkClient.createPersistent(ZK_ROOT_PATH, true);
		return;
	}
	
	String parentPath = ZK_ROOT_PATH + "/" + profile;
	if(!zkClient.exists(parentPath)){
		zkClient.createPersistent(parentPath, true);
		return;
	}
	List<String> apps = zkClient.getChildren(parentPath);
	for (String app : apps) {
		parentPath = ZK_ROOT_PATH + "/" + profile + "/" + app + "/nodes";
		
		int activeNodeCount = zkClient.countChildren(parentPath);
		if(activeNodeCount == 0)continue;
		List<ConfigState> localCacheConfigs = configStates.get(app + "#" + profile);
		
		if(activeNodeCount > 0 && (localCacheConfigs == null || localCacheConfigs.size() < activeNodeCount)){
			zkClient.writeData(parentPath, NOTIFY_UPLOAD_CMD);
			logger.info("send cmd[{}] on path[{}]",NOTIFY_UPLOAD_CMD,parentPath);
		}
	}
}
 
Example 3
Source File: ZkRegister.java    From netty-pubsub with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	ZkClient client=new ZkClient(new ZkConnection(CONNECT_ADDR_SINGLE),SESSION_OUTTIME);
	List<String> children = client.getChildren("/broker_active");
	children.forEach((rs)->{
		Object readData = client.readData("/broker_active/"+rs);
		System.out.println(readData);
	});	
}
 
Example 4
Source File: ZkUtil.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * 获取子节点
 * @param zk
 * @param path
 * @return
 */
public static List<String> getChildNode(ZkClient zk,String path){
	List<String> list=null;
	if(zk.exists(path)){
		list=zk.getChildren(path);
	}
	return list;
}
 
Example 5
Source File: ZKClientTest.java    From hermes with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	ZkClient zkClient = new ZkClient("");
	String basePath = "/consumers";
	for (String consumerId : zkClient.getChildren(basePath)) {
		String offsetPath = basePath + "/" + consumerId + "/offsets";
		List<String> zkTopics = zkClient.getChildren(offsetPath);
		System.out.println(zkTopics);
	}
}
 
Example 6
Source File: NodeDAO.java    From shepher with Apache License 2.0 5 votes vote down vote up
public List<String> getChildren(String cluster, String path) throws ShepherException {
    ZkClient zkClient = ZkPool.getZkClient(cluster);
    try {
        if (zkClient == null) {
            return Collections.emptyList();
        }
        return zkClient.getChildren(path);
    } catch (Exception e) {
        LOGGER.warn("Fail to get children, Exception:", e);
        throw ShepherException.createUnknownException();
    } finally {
        ZkPool.releaseZkClient(cluster, zkClient);
    }
}
 
Example 7
Source File: ZkTest.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {

        String zkHost = "192.168.88.196:2182,192.168.88.197:2182,192.168.88.198:2182";

        // 进行监控
        ZkClient zkClient = new ZkClient(zkHost, 60000, 5000);

        System.out.println("你妹的: " + zkClient.readData(Constants.ROOT_PATH_PERSISTENT + Constants.SLASH + "app-test"));



        List<String> apps = zkClient.getChildren(Constants.ROOT_PATH_EPHEMERAL);
        for (String app : apps) {
            zkClient.subscribeChildChanges(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app, new AppChildChangeListener());
            List<String> hosts = zkClient.getChildren(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app);
            for (String host : hosts) {
                zkClient.subscribeDataChanges(Constants.ROOT_PATH_EPHEMERAL + Constants.SLASH + app + Constants.SLASH + host, new HostDataChangeListener());
                System.out.println();
            }
        }

        synchronized (ZkTest.class) {
            while (true) {
                try {
                    ZkTest.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
Example 8
Source File: ZclientNormal.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: zkClient主要做了两件事情。
 * @see:一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连。
 * @see:一件是将一次性watcher包装为持久watcher。
 * @see:后者的具体做法是简单的在watcher回调中,重新读取数据的同时再注册相同的watcher实例。
 */
private static void test() {
	final ZkClient zc = new ZkClient("123.56.118.135:2181");
	// 创建根节点
	/* Persistent与Ephemeral 就是持久化保存到本地和不持久化的区别 ,不能再临时节点下面创建子节点 */
	
	zc.createPersistent("/testroot");
	// zc.createEphemeral("/testroot");
	// 创建子节点
	zc.create("/testroot/node1", "node1", CreateMode.EPHEMERAL);
	zc.createPersistent("/testroot/node2");
	zc.create("/testroot/node2/test", "node1", CreateMode.EPHEMERAL);
	List<String> children = zc.getChildren("/");
	log.info("根节点下面的字节点个数" + children.size());
	
	// 获得子节点个数
	
	int chidrenNumbers = zc.countChildren("/testroot");
	log.info("子节点个数" + chidrenNumbers);
	
	zc.writeData("/testroot/node2/test", "给节点写数据");
	
	// 删除节点
	zc.delete("/testroot/node2/test");
	zc.delete("/testroot/node2");
	zc.delete("/testroot/node1");
	zc.delete("/testroot");
}
 
Example 9
Source File: ZkConfigSaver.java    From learning-hadoop with Apache License 2.0 5 votes vote down vote up
private static void saveConfigs(ZkClient client, String rootNode,
		File confDir) {
	List<String> configs = client.getChildren(rootNode);
	for (String config : configs) {
		String content = (String) client.readData(rootNode + "/" + config);
		File confFile = new File(confDir, config);
		try {
			FileUtils.writeStringToFile(confFile, content, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("配置成功保存到本地: " + confFile.getAbsolutePath());
	}
}