/// <summary>
        /// 节点更新回调
        /// </summary>
        private void CallBack()
        {
            ConfigMetadataApiResult metadata = new ConfigMetadataApiResult
            {
                Name = _nodeName,
                Type = _disconfNodeType
            };

            ConfigStorageManager.ReloadConfigItem(metadata);
        }
Beispiel #2
0
        /// <summary>
        /// 刷新本地配置值
        /// </summary>
        /// <param name="metadata">配置项信息</param>
        internal static void ReloadConfigItem(ConfigMetadataApiResult metadata)
        {
            if (metadata == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(metadata.UpdateTime))
            {
                try
                {
                    ConfigMetadataApiResult result = _disconfWebApi.GetConfigMetadata(metadata.Name);
                    if (result != null)
                    {
                        metadata.UpdateTime = result.UpdateTime;
                    }
                }
                catch (Exception ex)
                {
                    LogManager.GetLogger().Warn(string.Format("获取配置项元数据信息时出现异常!配置项:{0},AppId:{1},Evn:{2},Version:{3}", metadata.Name, DisconfClientSettings.AppId, DisconfClientSettings.Environment, DisconfClientSettings.Version), ex);
                }
            }
            try
            {
                ConfigStorageItem configStorageItem = null;
                if (ConfigStorage.ContainsKey(metadata.Name))
                {
                    configStorageItem = ConfigStorage[metadata.Name];
                }

                ConfigStorageItem newConfigStorageItem = CreateConfigStorageItem(metadata.Name, metadata.Type, metadata.UpdateTime);

                if (configStorageItem == null)
                {
                    configStorageItem = newConfigStorageItem;
                }
                else
                {
                    configStorageItem.Data           = newConfigStorageItem.Data;
                    configStorageItem.Version        = newConfigStorageItem.Version;
                    configStorageItem.LastUpdateTime = DateTime.Now;
                }
                ConfigStorage[metadata.Name] = configStorageItem;
                configStorageItem.Callback();
                _disconfServerIsActive = true;
                SaveLocalConfig();
                LogManager.GetLogger().Info(string.Format("刷新本地配置项,{0}", JsonConvert.SerializeObject(configStorageItem)));
            }
            catch (Exception ex)
            {
                _disconfServerIsActive = false;
                LogManager.GetLogger().Error(string.Format("刷新本地配置值时出现异常!配置项:{0},AppId:{1},Evn:{2},Version:{3}", metadata.Name, DisconfClientSettings.AppId, DisconfClientSettings.Environment, DisconfClientSettings.Version), ex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 从配置中心加载该应用下的全部配置信息
        /// </summary>
        private static void LoadConfigItemsFromServer()
        {
            IList <ConfigMetadataApiResult> list = _disconfWebApi.GetConfigMetadatas();

            if (list == null || list.Count <= 0)
            {
                throw new Exception(string.Format("配置中心没有为应用:{0}配置过任何配置项!", DisconfClientSettings.AppId));
            }

            //获取所有文件类型的配置信息
            IList <Task> tasks = new List <Task>();

            foreach (ConfigMetadataApiResult item in list)
            {
                if (item == null || item.Type == DisconfNodeType.Item)
                {
                    continue;
                }
                ConfigMetadataApiResult configMetadata = item;
                Task task = Task.Factory.StartNew(() =>
                {
                    ConfigStorageItem configStorageItem = CreateConfigStorageItem(configMetadata.Name, configMetadata.Type, configMetadata.UpdateTime);
                    if (!ConfigStorage.ContainsKey(configStorageItem.Name))
                    {
                        ConfigStorage.Add(configStorageItem.Name, configStorageItem);
                    }
                });
                tasks.Add(task);
            }
            //获取所有配置项类型的配置信息
            Task task1 = Task.Factory.StartNew(() =>
            {
                IList <ConfigItemContentApiResult> items = _disconfWebApi.GetAllConfigItemContent();
                if (items != null)
                {
                    foreach (ConfigItemContentApiResult item in items)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        ConfigStorageItem configStorageItem = null;
                        if (ConfigStorage.ContainsKey(item.Name))
                        {
                            configStorageItem = ConfigStorage[item.Name];
                        }
                        if (configStorageItem == null)
                        {
                            string name = item.Name;
                            ConfigMetadataApiResult apiResult = list.FirstOrDefault(m => m.Name == name);
                            string version = null;
                            if (apiResult != null)
                            {
                                version = apiResult.UpdateTime;
                            }
                            configStorageItem = CreateConfigStorageItem(item.Name, DisconfNodeType.Item, version, item.Value);
                        }
                        else
                        {
                            configStorageItem.Data = item.Value;
                        }

                        ConfigStorage[configStorageItem.Name] = configStorageItem;
                    }
                }
            });

            tasks.Add(task1);
            Task.WaitAll(tasks.ToArray());
            SaveLocalConfig();
            _disconfServerIsActive = true;
        }