Search in sources :

Example 61 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project brpc-java by baidu.

the class StargateZookeeperNamingService method doSubscribe.

@Override
public void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener) throws Exception {
    final String path = buildParentNodePath(resolveGroup(subscribeInfo), subscribeInfo.getInterfaceName(), resolveVersion(subscribeInfo));
    PathChildrenCache cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            ChildData data = event.getData();
            // 子节点信息,将监听的父节点信息擦除
            String childNodePath = data.getPath().replace(path + "/", "");
            // 如果是客户端上线,不做处理
            if (StargateConstants.ZK_CONSUMER_DIR.equals(childNodePath)) {
                return;
            }
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        ServiceInstance endPoint = new ServiceInstance(childNodePath);
                        listener.notify(Collections.singletonList(endPoint), Collections.<ServiceInstance>emptyList());
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        ServiceInstance endPoint = new ServiceInstance(childNodePath);
                        listener.notify(Collections.<ServiceInstance>emptyList(), Collections.singletonList(endPoint));
                        break;
                    }
                case CHILD_UPDATED:
                default:
                    break;
            }
        }
    });
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    subscribeCacheMap.putIfAbsent(subscribeInfo, cache);
    log.info("stargate subscribe success from {}", url);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 62 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project brpc-java by baidu.

the class ZookeeperNamingService method doSubscribe.

@Override
public void doSubscribe(SubscribeInfo subscribeInfo, final NotifyListener listener) throws Exception {
    String path = getSubscribePath(subscribeInfo);
    PathChildrenCache cache = new PathChildrenCache(client, path, true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            ChildData data = event.getData();
            switch(event.getType()) {
                case CHILD_ADDED:
                    {
                        ServiceInstance instance = GsonUtils.fromJson(new String(data.getData()), ServiceInstance.class);
                        listener.notify(Collections.singletonList(instance), Collections.<ServiceInstance>emptyList());
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        ServiceInstance instance = GsonUtils.fromJson(new String(data.getData()), ServiceInstance.class);
                        listener.notify(Collections.<ServiceInstance>emptyList(), Collections.singletonList(instance));
                        break;
                    }
                case CHILD_UPDATED:
                    break;
                default:
                    break;
            }
        }
    });
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    subscribeCacheMap.putIfAbsent(subscribeInfo, cache);
    log.info("subscribe success from {}", url);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceInstance(com.baidu.brpc.client.channel.ServiceInstance) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 63 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project sofa-rpc by sofastack.

the class ZookeeperRegistry method subscribeConfig.

/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) {
            // 初始化
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                if (LOGGER.isDebugEnabled(config.getAppName())) {
                    LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
                }
                switch(event.getType()) {
                    case // 新增接口级配置
                    CHILD_ADDED:
                        configObserver.addConfig(config, configPath, event.getData());
                        break;
                    case // 删除接口级配置
                    CHILD_REMOVED:
                        configObserver.removeConfig(config, configPath, event.getData());
                        break;
                    case // 更新接口级配置
                    CHILD_UPDATED:
                        configObserver.updateConfig(config, configPath, event.getData());
                        break;
                    default:
                        break;
                }
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_CONFIG, EXT_NAME), e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 64 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project np by liuhouer.

the class Client1 method main.

public static void main(String[] args) throws Exception {
    Client1 cto = new Client1();
    System.out.println("client1 启动成功...");
    final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, CONFIG_NODE_PATH, true);
    childrenCache.start(StartMode.BUILD_INITIAL_CACHE);
    // 添加监听事件
    childrenCache.getListenable().addListener(new PathChildrenCacheListener() {

        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            // 监听节点变化
            if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {
                String configNodePath = event.getData().getPath();
                if (configNodePath.equals(CONFIG_NODE_PATH + SUB_PATH)) {
                    System.out.println("监听到配置发生变化,节点路径为:" + configNodePath);
                    // 读取节点数据
                    String jsonConfig = new String(event.getData().getData());
                    System.out.println("节点" + CONFIG_NODE_PATH + "的数据为: " + jsonConfig);
                    // 从json转换配置
                    RedisConfig redisConfig = null;
                    if (StringUtils.isNotBlank(jsonConfig)) {
                        redisConfig = (RedisConfig) JsonUtil.json2object(jsonConfig, RedisConfig.class);
                    }
                    // 配置不为空则进行相应操作
                    if (redisConfig != null) {
                        String type = redisConfig.getType();
                        String url = redisConfig.getUrl();
                        String remark = redisConfig.getRemark();
                        // 判断事件
                        if (type.equals("add")) {
                            System.out.println("监听到新增的配置,准备下载...");
                            // ... 连接ftp服务器,根据url找到相应的配置
                            Thread.sleep(500);
                            System.out.println("开始下载新的配置文件,下载路径为<" + url + ">");
                            // ... 下载配置到你指定的目录
                            Thread.sleep(1000);
                            System.out.println("下载成功,已经添加到项目中");
                        // ... 拷贝文件到项目目录
                        } else if (type.equals("update")) {
                            System.out.println("监听到更新的配置,准备下载...");
                            // ... 连接ftp服务器,根据url找到相应的配置
                            Thread.sleep(500);
                            System.out.println("开始下载配置文件,下载路径为<" + url + ">");
                            // ... 下载配置到你指定的目录
                            Thread.sleep(1000);
                            System.out.println("下载成功...");
                            System.out.println("删除项目中原配置文件...");
                            Thread.sleep(100);
                            // ... 删除原文件
                            System.out.println("拷贝配置文件到项目目录...");
                        // ... 拷贝文件到项目目录
                        } else if (type.equals("delete")) {
                            System.out.println("监听到需要删除配置");
                            System.out.println("删除项目中原配置文件...");
                        }
                    // TODO 视情况统一重启服务
                    }
                }
            }
        }
    });
    countDown.await();
    cto.closeZKClient();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) RedisConfig(cn.northpark.zookeeper.RedisConfig)

Example 65 with PathChildrenCacheEvent

use of org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent in project np by liuhouer.

the class Client2 method main.

public static void main(String[] args) throws Exception {
    Client2 cto = new Client2();
    System.out.println("client2 启动成功...");
    final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, CONFIG_NODE_PATH, true);
    childrenCache.start(StartMode.BUILD_INITIAL_CACHE);
    // 添加监听事件
    childrenCache.getListenable().addListener(new PathChildrenCacheListener() {

        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            // 监听节点变化
            if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {
                String configNodePath = event.getData().getPath();
                if (configNodePath.equals(CONFIG_NODE_PATH + SUB_PATH)) {
                    System.out.println("监听到配置发生变化,节点路径为:" + configNodePath);
                    // 读取节点数据
                    String jsonConfig = new String(event.getData().getData());
                    System.out.println("节点" + CONFIG_NODE_PATH + "的数据为: " + jsonConfig);
                    // 从json转换配置
                    RedisConfig redisConfig = null;
                    if (StringUtils.isNotBlank(jsonConfig)) {
                        redisConfig = (RedisConfig) JsonUtil.json2object(jsonConfig, RedisConfig.class);
                    }
                    // 配置不为空则进行相应操作
                    if (redisConfig != null) {
                        String type = redisConfig.getType();
                        String url = redisConfig.getUrl();
                        String remark = redisConfig.getRemark();
                        // 判断事件
                        if (type.equals("add")) {
                            System.out.println("监听到新增的配置,准备下载...");
                            // ... 连接ftp服务器,根据url找到相应的配置
                            Thread.sleep(500);
                            System.out.println("开始下载新的配置文件,下载路径为<" + url + ">");
                            // ... 下载配置到你指定的目录
                            Thread.sleep(1000);
                            System.out.println("下载成功,已经添加到项目中");
                        // ... 拷贝文件到项目目录
                        } else if (type.equals("update")) {
                            System.out.println("监听到更新的配置,准备下载...");
                            // ... 连接ftp服务器,根据url找到相应的配置
                            Thread.sleep(500);
                            System.out.println("开始下载配置文件,下载路径为<" + url + ">");
                            // ... 下载配置到你指定的目录
                            Thread.sleep(1000);
                            System.out.println("下载成功...");
                            System.out.println("删除项目中原配置文件...");
                            Thread.sleep(100);
                            // ... 删除原文件
                            System.out.println("拷贝配置文件到项目目录...");
                        // ... 拷贝文件到项目目录
                        } else if (type.equals("delete")) {
                            System.out.println("监听到需要删除配置");
                            System.out.println("删除项目中原配置文件...");
                        }
                    // TODO 视情况统一重启服务
                    }
                }
            }
        }
    });
    countDown.await();
    cto.closeZKClient();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) RedisConfig(cn.northpark.zookeeper.RedisConfig)

Aggregations

PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)68 CuratorFramework (org.apache.curator.framework.CuratorFramework)58 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)52 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)36 ChildData (org.apache.curator.framework.recipes.cache.ChildData)21 IOException (java.io.IOException)15 KeeperException (org.apache.zookeeper.KeeperException)11 Test (org.junit.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 DataSegment (org.apache.druid.timeline.DataSegment)5 Map (java.util.Map)4 ZKPaths (org.apache.curator.utils.ZKPaths)4 RedisConfig (cn.northpark.zookeeper.RedisConfig)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 ServiceInstance (com.baidu.brpc.client.channel.ServiceInstance)3 RpcException (com.baidu.brpc.exceptions.RpcException)3 DataSegment (io.druid.timeline.DataSegment)3 Releasable (com.ctrip.xpipe.api.lifecycle.Releasable)2 RegistryPackage (com.polyu.blockchain.common.wrapper.RegistryPackage)2 ServiceDTO (io.fabric8.gateway.ServiceDTO)2