コード例 #1
0
        /// <summary>
        /// 获取一个监视管理器
        /// </summary>
        /// <param name="webApi">配置中心服务接口</param>
        /// <returns></returns>
        public static WatcherManager GetWatcherManager(IDisconfWebApi webApi)
        {
            if (webApi == null)
            {
                throw new ArgumentNullException("webApi");
            }
            string         host           = webApi.GetZooKeeperHost();
            string         rootNode       = webApi.GetZooKeeperRootNode();
            WatcherManager watcherManager = new WatcherManager();

            watcherManager.Init(host, rootNode);
            return(watcherManager);
        }
コード例 #2
0
        /// <summary>
        /// 创建本地存储配置项
        /// </summary>
        /// <param name="name">配置项名称</param>
        /// <param name="disconfNodeType">配置项类型</param>
        /// <param name="version">版本号(对应配置中的中的配置项的最后一次更新时间值)</param>
        /// <param name="data">配置项值</param>
        /// <returns></returns>
        private static ConfigStorageItem CreateConfigStorageItem(string name, DisconfNodeType disconfNodeType, string version, string data = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(data))
            {
                if (disconfNodeType == DisconfNodeType.File)
                {
                    data = _disconfWebApi.GetConfigFileContent(name);
                }
                else
                {
                    data = _disconfWebApi.GetConfigItemContent(name);
                }
            }
            ConfigStorageItem configStorageItem = new ConfigStorageItem
            {
                Name           = name,
                Data           = data,
                Version        = version,
                LastUpdateTime = DateTime.Now
            };

            if (!DisconfClientSettings.DisableZooKeeper)
            {
                ThreadPool.QueueUserWorkItem((obj) =>
                {
                    try
                    {
                        WatcherManager watcherManager = new WatcherManager(_zooKeeperClient);
                        watcherManager.WatchPath(name, data, disconfNodeType);
                    }
                    catch (Exception ex)
                    {
                        LogManager.GetLogger()
                        .Error(
                            string.Format(
                                "DisconfClient.ConfigStorageManager.CreateConfigStorageItem.WatchPath,Exception:{0}",
                                ex));
                    }
                });
            }

            return(configStorageItem);
        }
コード例 #3
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="disconfWebApi">配置中心接口服务</param>
        /// <param name="assemblies">配置类所在的程序集</param>
        public static void Init(IDisconfWebApi disconfWebApi = null, params Assembly[] assemblies)
        {
            if (disconfWebApi == null)
            {
                disconfWebApi = new DisconfWebApi();
            }
            _disconfWebApi = disconfWebApi;
            if (!DisconfClientSettings.DisableZooKeeper)
            {
                _zooKeeperClient = new ZooKeeperClient(_disconfWebApi);
                //向Zookeeper发送一个Watch,以便近快的拿到SyncConnected状态
                WatcherManager watcherManager = new WatcherManager(_zooKeeperClient);
                watcherManager.WatchPath("test", "test", DisconfNodeType.Item);
            }
            DisconfClientSettings.Verify();
            if (DisconfClientSettings.OnlyLoadLocalConfig)
            {
                LoadLocalConfig();
            }
            else
            {
                try
                {
                    LoadConfigItemsFromServer();
                }
                catch (Exception ex)
                {
                    string localConfigPath     = GetLocalConfigPath();
                    bool   isExistsLocalConfig = File.Exists(localConfigPath);
                    if (!isExistsLocalConfig)
                    {
                        throw;
                    }
                    LoadLocalConfig();
                    LogManager.GetLogger().Error(string.Format("从配置中心获取数据加载全量数据失败,已从本地副本中加载成功({0})!", localConfigPath), ex);
                }
            }

            AssemblyScan(assemblies);
            AutoRefresh();
            StartFileSystemWatcher();
        }