internal void Load(String json)
        {
            JsonConfigurationParser parser = new JsonConfigurationParser();

            try
            {
                Data = parser.Parse(json);
            }
            catch (JsonReaderException e)
            {
                string errorLine = string.Empty;
                throw new FormatException($"json 配置文本格式错误:行号:{e.LineNumber}。", e);
            }
        }
示例#2
0
        private async Task LoadConfigurationDataAsync(ZookeeperClient client, bool firstLoad, string path)
        {
            try
            {
                var data = (await client.GetDataAsync(path)).ToArray();
                if (firstLoad)
                {
                    await client.SubscribeDataChange(path, this.OnNodeDataChangeAsync);
                }
                if (data != null)
                {
                    LoadKey();

                    data = OpenSSL.RSADecrypt(data, _key);


                    //data = OpenSSL.RSADecrypt(data, _key);

                    string json = String.Empty;
                    if (data.Length <= 3)
                    {
                        throw new Exception("配置中心返回了错误的配置数据");
                    }
                    if (data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
                    {
                        json = new UTF8Encoding(false).GetString(data, 3, data.Length - 3);
                    }
                    else
                    {
                        json = Encoding.UTF8.GetString(data);
                    }
                    var parser = new JsonConfigurationParser();

                    //配置中心不能影响本地的用于构造配置节点路径的配置,否则将造成拉取错误配置。
                    this.Data = parser.Parse(json,
                                             ConfigurationPath.Combine("Schubert", "Group"),
                                             ConfigurationPath.Combine("Schubert", "AppSystemName"),
                                             ConfigurationPath.Combine("Schubert", "Version"),
                                             ConfigurationPath.Combine("Schubert", "Env"),
                                             ConfigurationPath.Combine("Schubert", "Configuration"));
                    OnReload();
                    //Microsoft.Extensions.Configuration.FileConfigurationProvider的源码有这个代码OnReload()
                    //OnReload会让GetReloadToken通知ChangeToken.OnChange去调用changeTokenConsumer的委托
                    //changeTokenConsumer的委托中如果走load等同逻辑的代码,机会再次调用OnReload,然后就挂
                    //ChangeToken.OnChange方案的用处是用来做外置事件源的处理,不能做内部事件调用
                    //Action reload = OnReload;
                    //reload();
                    //不加OnReload,完全不起作用,但是为什么叫OnReload
                }
            }
            catch (TimeoutException ex)
            {
                string error = $"连接到配置中心(zk_server: {client.Options.ConnectionString })超时。";
                HandleError(firstLoad, error, ex);
            }
            catch (FormatException ex)
            {
                string error = $"从配置中心加载配置发生错误,可能由于配置文件格式错误(zk_server: {client.Options.ConnectionString }, paht:{path})超时。";
                HandleError(firstLoad, error, ex);
            }
        }