/// <summary> /// HttpProxy 配置,主要配置那些微服务可以提供外部api服务 /// </summary> /// <param name="proxyName">proxyName</param> /// <param name="updateAction">updateAction</param> /// <returns>HttpProxy</returns> public static Configuration GetHttpProxy(string proxyName, Action <Configuration> updateAction) { switch (configCenter.ConfigType) { case ConfigType.Local: case ConfigType.HttpFile: return(config); // 直接返回本地配置 case ConfigType.Zookeeper: Action <Configuration> callBack = (Configuration cfgValue) => { GetHttpProxy(proxyName, updateAction); // 调用一次方法,挂载回调 updateAction(cfgValue); // 重连之后要执行回调,做变更 }; callBackList[proxyName] = callBack; // 断线重连之后,要把当前方法封装起来,作为回调 var router = new ConfigWatcher(); router.OnChange += (string path) => { if (updateAction != null && !string.IsNullOrEmpty(path)) { var proxyStr = GrantZookeeperManager.GetNodeData(path, router); if (string.IsNullOrEmpty(proxyStr)) { return; } var httpProxy = Newtonsoft.Json.JsonConvert.DeserializeObject <Configuration>(proxyStr); config.HttpProxy = httpProxy.HttpProxy; updateAction(config); } }; string p = GrantZookeeperManager.getConfigPath(proxyName); var cfg = GrantZookeeperManager.GetNodeData(p + "/" + GrantHttpProxy.HttpProxy, router); if (string.IsNullOrEmpty(cfg)) { logger.LogWarning("获取代理层配置为空,这个代理层将无法提供代理服务 ServerSetting.GetHttpProxy is Null"); } else { var proxy = Newtonsoft.Json.JsonConvert.DeserializeObject <Configuration>(cfg); config.HttpProxy = proxy.HttpProxy; } return(config); } return(null); }
private static void initlizeData(string appName) { // 检查标准配置,第一次可能zk是空 // 检查标准配置节点,帮助初始化 var standConfig = getStandConfig(); GrantZookeeperManager.CheckConfig(appName, standConfig); // 拉取当前AppName的配置,需要注册watcher var dataWatcher = new ConfigWatcher(); dataWatcher.OnChange += (string path) => { string configData = GrantZookeeperManager.GetNodeData(path, dataWatcher); if (string.IsNullOrEmpty(configData)) { return; } UpdateZookeeper(path, configData); }; List <string> childrens = GrantZookeeperManager.GetConfigChildren(appName, null); // 配置是整个获取节点,分别获取配置和分别增加watcher if (childrens != null && childrens.Count > 0) { string root = GrantZookeeperManager.getConfigPath(appName); if (string.IsNullOrEmpty(root)) { return; } foreach (var item in childrens) { // 需要根据节点路径来判断是哪个节点变化了 string path = root + "/" + item; string configData = GrantZookeeperManager.GetNodeData(path, dataWatcher); UpdateZookeeper(path, configData); } } }