// 从配置中心拉取配置privateDynamicConfigurationprepareEnvironment(ConfigCenterConfigconfigCenter){if(configCenter.isValid()){...DynamicConfigurationdynamicConfiguration;try{// 获取动态配置dynamicConfiguration=getDynamicConfiguration(configCenter.toUrl());}catch(Exceptione){...}ApplicationModelapplicationModel=getApplicationModel();// 配置文件就是 keyif(StringUtils.isNotEmpty(configCenter.getConfigFile())){// 获取配置内容, 全局级别的StringconfigContent=dynamicConfiguration.getProperties(configCenter.getConfigFile(),configCenter.getGroup());if(StringUtils.isNotEmpty(configContent)){logger.info(String.format("Got global remote configuration from config center with key-%s and group-%s: \n %s",configCenter.getConfigFile(),configCenter.getGroup(),configContent));}StringappGroup=getApplication().getName();StringappConfigContent=null;StringappConfigFile=null;if(isNotEmpty(appGroup)){appConfigFile=isNotEmpty(configCenter.getAppConfigFile())?configCenter.getAppConfigFile():configCenter.getConfigFile();// 获取配置内容, 应用级别的appConfigContent=dynamicConfiguration.getProperties(appConfigFile,appGroup);if(StringUtils.isNotEmpty(appConfigContent)){logger.info(String.format("Got application specific remote configuration from config center with key %s and group %s: \n %s",appConfigFile,appGroup,appConfigContent));}}try{// 解析配置Map<String,String>configMap=parseProperties(configContent);Map<String,String>appConfigMap=parseProperties(appConfigContent);// 更新配置environment.updateExternalConfigMap(configMap);environment.updateAppExternalConfigMap(appConfigMap);...}catch(IOExceptione){thrownewIllegalStateException("Failed to parse configurations from Config Center.",e);}}returndynamicConfiguration;}returnnull;}
@Overridepublicvoidinitialize()throwsIllegalStateException{if(initialized.compareAndSet(false,true)){// 属性配置,后面会说this.propertiesConfiguration=newPropertiesConfiguration(scopeModel);// 系统配置this.systemConfiguration=newSystemConfiguration();// 环境变量this.environmentConfiguration=newEnvironmentConfiguration();// 配置中心的全局配置this.externalConfiguration=newInmemoryConfiguration("ExternalConfig");// 配置中心的应用配置this.appExternalConfiguration=newInmemoryConfiguration("AppExternalConfig");// 本地的应用配置this.appConfiguration=newInmemoryConfiguration("AppConfig");loadMigrationRule();}}// external config, such as config-center global/default configprivateInmemoryConfigurationexternalConfiguration;// external app config, such as config-center app configprivateInmemoryConfigurationappExternalConfiguration;// local app config , such as Spring Environment/PropertySources/application.propertiesprivateInmemoryConfigurationappConfiguration;