static ConfigService()
        {
            try
            {
                ApolloConfigSection = ApolloConfigSettingHelper.GetApolloConfigSettings();

                ComponentsConfigurator.DefineComponents();
                s_configManager = ComponentLocator.Lookup <IConfigManager>();

                // 初始化 namespace
                var namespaceSection = (ApolloConfigNameSpacesSection)ConfigurationManager.GetSection(ApolloConfigNameSpacesSection.CONFIG_SECTION_NAME);
                if (namespaceSection != null && namespaceSection.Namespaces != null && namespaceSection.Namespaces.Count > 0)
                {
                    foreach (var namespaceEle in namespaceSection.Namespaces)
                    {
                        if (namespaceEle is NamespaceElement namespaceElement && !string.IsNullOrWhiteSpace(namespaceElement.Value) && !ConfigNamespaces.Contains(namespaceElement.Value.Trim()))
                        {
                            ConfigNamespaces.Add(namespaceElement.Value.Trim());
                        }
                    }
                }
                if (ConfigNamespaces == null || ConfigNamespaces.Count == 0)
                {
                    ConfigNamespaces.Add(ConfigConsts.NAMESPACE_APPLICATION);
                }

                foreach (var namesp in ConfigNamespaces)
                {
                    var config = GetConfig(namesp);
                    s_configManager.Configs.TryAdd(namesp, config);
                }
            }
            catch (Exception ex)
            {
                ApolloConfigException exception = new ApolloConfigException("Init ConfigService failed", ex);
                logger.Error(exception);
                throw exception;
            }
        }
Exemple #2
0
        /// <summary>
        /// 设置本地缓存文件基础路径
        /// </summary>
        /// <param name="dir"></param>
        public static void SetBaseDir(string dir)
        {
            if (string.IsNullOrWhiteSpace(dir))
            {
                throw new ArgumentNullException(nameof(dir));
            }
            ApolloConfigSection.BaseDir = dir;

            var localConfigDir = ApolloConfigSettingHelper.GetApolloConfigSettings().LocalConfigDir;

            var path = Path.Combine(localConfigDir, LocalFileConfigRepository.CONFIG_DIR);

            // LocalConfigDir 为相对路径
            if (localConfigDir.StartsWith(DIR_PREFIX))
            {
                path = Path.Combine(ApolloConfigSection.BaseDir, path.Substring(DIR_PREFIX.Length));
            }

            // 不存在时,尝试创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            // 尝试读写操作
            var fileName = $"{TXT_TEST}_{ DateTime.Now.Ticks }";
            var fullName = Path.Combine(path, fileName);

            File.WriteAllText(fullName, TXT_TEST);

            var txt = File.ReadAllText(fullName);

            if (!TXT_TEST.Equals(txt))
            {
                throw new InvalidOperationException("本地缓存文件读写验证失败,读取到的值与写入的不一致。");
            }
            File.Delete(fullName);
        }