使用Zookeeper -watcher動態更新N臺節點配置

maven配置

org。apache。zookeeper zookeeper 3。4。11 org。apache。curator curator-recipes 4。0。0 org。apache。curator curator-framework 4。0。0

更新節點程式碼

import com。alibaba。fastjson。JSON;import lombok。Data;import org。apache。commons。lang3。StringUtils;import org。apache。curator。RetryPolicy;import org。apache。curator。framework。CuratorFramework;import org。apache。curator。framework。CuratorFrameworkFactory;import org。apache。curator。framework。recipes。cache。PathChildrenCache;import org。apache。curator。framework。recipes。cache。PathChildrenCache。StartMode;import org。apache。curator。framework。recipes。cache。PathChildrenCacheEvent;import org。apache。curator。framework。recipes。cache。PathChildrenCacheListener;import org。apache。curator。retry。RetryNTimes;import java。util。concurrent。CountDownLatch;public class Client { public CuratorFramework client = null; /** * zk伺服器資訊,如果是叢集用,隔開 */ public static final String zkServerPath = “xxx。xxx。xxx。xxx:2181”; public Client() { RetryPolicy retryPolicy = new RetryNTimes(3, 5000); client = CuratorFrameworkFactory。builder() 。connectString(zkServerPath) 。sessionTimeoutMs(10000)。retryPolicy(retryPolicy) 。namespace(“workspace”)。build(); client。start(); } public void closeZKClient() { if (client != null) { this。client。close(); } } public final static String CONFIG_NODE_PATH = “/super/study”; public final static String SUB_PATH = “/redis-config”; public static CountDownLatch countDown = new CountDownLatch(1); public static void main(String[] args) throws Exception { Client cto = new Client(); System。out。println(“client 啟動成功。。。”); 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 = JSON。parseObject( 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(); } @Data public static class RedisConfig{ private String type; // add 新增配置 update 更新配置 delete 刪除配置 private String url; // 如果是add或update,則提供下載地址 private String remark; // 備註 }}

配置資訊

1、新增{“type”:“add”,“url”:“https://xxx。oss-cn-shenzhen。aliyuncs。com/config/redisConfig。json”,“remark”:“add”}2、修改{“type”:“update”,“url”:“https://xxx。oss-cn-shenzhen。aliyuncs。com/config/redisConfig。json”,“remark”:“add”}3、刪除{“type”:“delete”,“url”:“”,“remark”:“add”}

總結

我們可以透過Zookeeper-watcher監聽節點來完成配置的動態更新。

有收穫的話可以給我點個贊+關注,謝謝。

使用Zookeeper -watcher動態更新N臺節點配置