示例#1
0
        private async Task Refresh(AppInfo info, IDictionary <ConfigType, IDictionary <string, string> > configs, bool recoverFromLocal)
        {
            var builders = configs.Select(c =>
            {
                var builder = new ZkTreeBuilder(info, c.Key, this._setting.ZookeeperBasePath, this._setting.ClientInfo.IgnoreCase);
                IRuleCollection <Rule> rule = null;
                if (this._ruleDictionary.ContainsKey(c.Key))
                {
                    rule = this._ruleDictionary[c.Key];
                }
                foreach (var kv in c.Value)
                {
                    builder.GetOrAddZnodeName(kv.Key);
                    rule?.For(kv.Key);
                    this._fallBackPolicy.Execute(() =>
                    {
                        this.TryNoticeChanged(c.Key, kv.Key, kv.Value);
                    }, new Dictionary <string, object>
                    {
                        { "Action", "TryNoticeChangedOnRefresh" },
                        { "ConfigType", c.Key },
                        { kv.Key, kv.Value }
                    });
                }
                return(builder);
            }).ToArray();

            await this.AddWatcher(builders).ConfigureAwait(false);
        }
示例#2
0
        private async Task Refresh(AppInfo info, IDictionary <ConfigType, IDictionary <string, string> > configs)
        {
            var builders = configs.Select(c =>
            {
                var builder      = new ZkTreeBuilder(info, c.Key, this._setting.ZookeeperBasePath, this._setting.ClientInfo.IgnoreCase);
                var needRegister = c.Key == ConfigType.File;
                foreach (var kv in c.Value)
                {
                    builder.GetOrAddZnodeName(kv.Key);
                    if (needRegister)
                    {
                        this.FileRules.For(kv.Key);
                    }
                    this._fallBackPolicy.Execute(() =>
                    {
                        this.TryNoticeChanged(c.Key, kv.Key, kv.Value);
                    }, new Dictionary <string, object>
                    {
                        { "Action", "TryNoticeChangedOnRefresh" },
                        { "ConfigType", c.Key },
                        { kv.Key, kv.Value }
                    });
                }
                return(builder);
            }).ToArray();

            await this.AddWatcher(builders).ConfigureAwait(false);
        }
示例#3
0
        /// <summary>
        /// 同步zookeeper服务并序列化成配置文件到本地
        /// 初始化
        /// </summary>
        public void InitClient()
        {
            var task = Task.Run(() =>
            {
                this._handler.Execute(() => {
                    StringBuilder logBuilder = new StringBuilder();

                    #region  步配置到本地
                    if (!Directory.Exists(ClientPath))
                    {
                        Directory.CreateDirectory(ClientPath);
                    }

                    logBuilder.AppendLine("同步zookeeper数据到本地\r\n");
                    using (MaintainWatcher mk = new MaintainWatcher(config.Host, 1000))
                    {
                        ZooKeeper zk = mk.ZooKeeper;
                        //本地站点的名称
                        string name = config.ClientInfo.AppName;
                        var stat    = zk.Exists($"/{name}", false);
                        if (stat == null)
                        {
                            return;
                        }
                        if (!System.IO.Directory.Exists(Path.Combine(ClientPath, name)))
                        {
                            System.IO.Directory.CreateDirectory(Path.Combine(ClientPath, name));
                        }

                        var children = zk.GetChildren($"/{name}", false);//取不到节点会报错
                        foreach (var child in children)
                        {
                            stat             = new Stat();
                            var tmpdata      = zk.GetData($"/{name}/{child}", false, stat);
                            string tmpString = System.Text.Encoding.UTF8.GetString(tmpdata);
                            ZNode znode      = Deserialize(tmpString);
                            if (znode == null)
                            {
                                continue;
                            }
                            znode.Version = stat.Version;

                            //看下本地的配置文件版本号,需要不需要更新
                            string file  = Path.Combine(ClientPath, name, child + ".txt");
                            string text  = FileHelper.ReadFile(file);
                            ZNode zLocal = Deserialize(text);

                            if (zLocal == null || zLocal.Version < znode.Version)
                            {
                                FileHelper.WriteFile(file, znode.ToString());
                                logBuilder.AppendLine($"下载节点:{$"/{name}/{child}"},值:{JsonHelper.ToNewtonJsonString(znode)}");
                            }
                        }
                    }
                    #endregion

                    #region 加载配置到内存
                    var files = Directory.GetFiles(Path.Combine(ClientPath, config.ClientInfo.AppName), "*.txt");
                    IZkTreeBuilder itemBuilder = new ZkTreeBuilder(config.ClientInfo.AppName);
                    foreach (string file in files)
                    {
                        string key  = Path.GetFileNameWithoutExtension(file);
                        string text = FileHelper.ReadFile(file);
                        ZNode znode = Deserialize(text);
                        if (znode == null)
                        {
                            continue;
                        }
                        itemBuilder.GetOrAddZnodeName($"/{config.ClientInfo.AppName}/{key}", znode.Value);
                    }
                    this._itemWatcher              = new NodeWatcher(config.Host, 30000, itemBuilder);
                    this._itemWatcher.NodeChanged += _itemWatcher_NodeChanged;
                    #endregion

                    LogHelper.WriteCustom(logBuilder.ToString(), "zookeeper\\", false);
                }, string.Empty);
            });

            task.Wait();
        }