Search in sources :

Example 1 with RedisConfig

use of cn.northpark.zookeeper.RedisConfig in project np by liuhouer.

the class Client3 method main.

public static void main(String[] args) throws Exception {
    Client3 cto = new Client3();
    System.out.println("client3 启动成功...");
    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 2 with RedisConfig

use of cn.northpark.zookeeper.RedisConfig 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 3 with RedisConfig

use of cn.northpark.zookeeper.RedisConfig 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

RedisConfig (cn.northpark.zookeeper.RedisConfig)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)3 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)3