以下文章来源于猿天地 ,作者李伟超
1.1 配置的基本概念
Environment 是 Spring 容器中对于应用环境两个关键因素(profile & properties)的一个抽象。
profile 是一个逻辑的分组,当 bean 向容器中注册的时候,仅当配置激活时生效。
## 配置文件使用spring.profiles.active=xxx## 硬编码注解形式使用@org.springframework.context.annotation.ProfileProperties 在几乎所有应用程序中都扮演着重要的角色,并且可能来自各种各样的来源:properties 文件、JVM系统属性、系统环境变量、JNDI、Servlet Context 参数、ad-hoc Properties 对象、Map 等等。Environment 与 Properties 的关系是为用户提供一个方便的服务接口,用于配置属性源并从它们中解析属性。
Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。
Apollo 支持4个维度管理 Key-Value 格式的配置:
这个很好理解,就是实际使用配置的应用,Apollo 客户端在运行时需要知道当前应用是谁,从而可以去获取对应的配置。每个应用都需要有唯一的身份标识,我们认为应用身份是跟着代码走的,所以需要在代码中配置,具体信息请参见 Java 客户端使用指南。
配置对应的环境,Apollo 客户端在运行时需要知道当前应用处于哪个环境,从而可以去获取应用的配置。我们认为环境和代码无关,同一份代码部署在不同的环境就应该能够获取到不同环境的配置,所以环境默认是通过读取机器上的配置(server.properties中的env属性)指定的,不过为了开发方便,我们也支持运行时通过 System Property 等指定,具体信息请参见Java客户端使用指南。
一个应用下不同实例的分组,比如典型的可以按照数据中心分,把上海机房的应用实例分为一个集群,把北京机房的应用实例分为另一个集群。对不同的cluster,同一个配置可以有不一样的值,如 zookeeper 地址。集群默认是通过读取机器上的配置(server.properties中的idc属性)指定的,不过也支持运行时通过 System Property 指定,具体信息请参见Java客户端使用指南。
一个应用下不同配置的分组,可以简单地把 namespace 类比为文件,不同类型的配置存放在不同的文件中,如数据库配置文件,RPC配置文件,应用自身的配置文件等。应用可以直接读取到公共组件的配置 namespace,如 DAL,RPC 等。应用也可以通过继承公共组件的配置 namespace 来对公共组件的配置做调整,如DAL的初始数据库连接数。
同时,Apollo 基于开源模式开发,开源地址:https://github.com/ctripcorp/apollo
如下即是Apollo的基础模型:

Apollo 项目本身就使用了 Spring Boot & Spring Cloud 开发。

上图简要描述了Apollo的总体设计,我们可以从下往上看:

长连接实现上是使用的异步+轮询实现
在 Apollo 架构说明中我们提到过 client 和 portal 都是在客户端负载均衡,根据 ip+port 访问服务,所以 config service 和 admin service 是无状态的,可以水平扩展的,portal service 根据使用 slb 绑定多台服务器达到切换,meta server 同理。
场景 | 影响 | 降级 | 原因 |
|---|---|---|---|
某台config service下线 | 无影响 | Config service无状态,客户端重连其它config service | |
所有config service下线 | 客户端无法读取最新配置,Portal无影响 | 客户端重启时,可以读取本地缓存配置文件 | |
某台admin service下线 | 无影响 | Admin service无状态,Portal重连其它admin service | |
所有admin service下线 | 客户端无影响,portal无法更新配置 | ||
某台portal下线 | 无影响 | Portal域名通过slb绑定多台服务器,重试后指向可用的服务器 | |
全部portal下线 | 客户端无影响,portal无法更新配置 | ||
某个数据中心下线 | 无影响 | 多数据中心部署,数据完全同步,Meta Server/Portal域名通过slb自动切换到其它存活的数据中心 |
在 Spring Boot & Spring Cloud 中使用。

以下代码是扩展 apollo 应用标识使用 spring.application.name,并增加监控配置,监控一般是基础架构团队提供的功能,从基础框架硬编码上去,业务侧做到完全无感知。
import com.ctrip.framework.apollo.ConfigService;import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;import com.ctrip.framework.foundation.internals.io.BOMInputStream;import com.ctrip.framework.foundation.internals.provider.DefaultApplicationProvider;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.SpringApplicationRunListener;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.core.annotation.Order;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.EnumerablePropertySource;import org.springframework.core.env.PropertiesPropertySource;import org.springframework.util.StringUtils; import java.io.InputStream;import java.io.InputStreamReader;import java.nio.charset.StandardCharsets;import java.util.Properties;import java.util.Set;
/** * ApolloSpringApplicationRunListener * <p> * SpringApplicationRunListener 接口说明 * https://blog.csdn.net/u011179993/article/details/51555690https://blog.csdn.net/u011179993/article/details/51555690 * * @author Weichao Li (liweichao0102@gmail.com) * @since 2019-08-15 */@Order(value = ApolloSpringApplicationRunListener.APOLLO_SPRING_APPLICATION_RUN_LISTENER_ORDER)@Slf4jpublic class ApolloSpringApplicationRunListener implements SpringApplicationRunListener {
public static final int APOLLO_SPRING_APPLICATION_RUN_LISTENER_ORDER = 1; private static final String APOLLO_APP_ID_KEY = "app.id"; private static final String SPRINGBOOT_APPLICATION_NAME = "spring.application.name"; private static final String CONFIG_CENTER_INFRA_NAMESPACE = "infra.monitor";
public ApolloSpringApplicationRunListener(SpringApplication application, String[] args) {
}
/** * 刚执行run方法时 */ @Override public void starting() {
}
/** * 环境建立好时候 * * @param env 环境信息 */ @Override public void environmentPrepared(ConfigurableEnvironment env) { Properties props = new Properties(); props.put(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, true); props.put(PropertySourcesConstants.APOLLO_BOOTSTRAP_EAGER_LOAD_ENABLED, true); env.getPropertySources().addFirst(new PropertiesPropertySource("apolloConfig", props)); // 初始化appId this.initAppId(env); // 初始化基础架构提供的默认配置,需在项目中关联公共 namespaces this.initInfraConfig(env); }
/** * 上下文建立好的时候 * * @param context 上下文 */ @Override public void contextPrepared(ConfigurableApplicationContext context) {
}
/** * 上下文载入配置时候 * * @param context 上下文 */ @Override public void contextLoaded(ConfigurableApplicationContext context) {
}
@Override public void started(ConfigurableApplicationContext context) {
}
@Override public void running(ConfigurableApplicationContext context) {
}
@Override public void failed(ConfigurableApplicationContext context, Throwable exception) {
}
/** * 初始化 apollo appId * * @param env 环境信息 */ private void initAppId(ConfigurableEnvironment env) { String apolloAppId = env.getProperty(APOLLO_APP_ID_KEY); if (StringUtils.isEmpty(apolloAppId)) { //此处需要判断一下 meta-inf 下的文件中的 app id apolloAppId = getAppIdByAppPropertiesClasspath(); if (StringUtils.isEmpty(apolloAppId)) { String applicationName = env.getProperty(SPRINGBOOT_APPLICATION_NAME); if (!StringUtils.isEmpty(applicationName)) { System.setProperty(APOLLO_APP_ID_KEY, applicationName); } else { throw new IllegalArgumentException("config center must config app.id in " + DefaultApplicationProvider.APP_PROPERTIES_CLASSPATH); } } else { System.setProperty(APOLLO_APP_ID_KEY, apolloAppId); } } else { System.setProperty(APOLLO_APP_ID_KEY, apolloAppId); } }
/** * 初始化基础架构提供的配置 * * @param env 环境信息 */ private void initInfraConfig(ConfigurableEnvironment env) { com.ctrip.framework.apollo.Config apolloConfig = ConfigService.getConfig(CONFIG_CENTER_INFRA_NAMESPACE); Set<String> propertyNames = apolloConfig.getPropertyNames(); if (propertyNames != null && propertyNames.size() > 0) { Properties properties = new Properties(); for (String propertyName : propertyNames) { properties.setProperty(propertyName, apolloConfig.getProperty(propertyName, null)); } EnumerablePropertySource enumerablePropertySource = new PropertiesPropertySource( CONFIG_CENTER_INFRA_NAMESPACE, properties); env.getPropertySources().addLast(enumerablePropertySource); } }
/** * 从 apollo 默认配置文件中取 app.id 的值,调整优先级在 spring.application.name 之前 * * @return apollo app id */ private String getAppIdByAppPropertiesClasspath() { try { InputStream in = Thread.currentThread().getContextClassLoader() .getResourceAsStream(DefaultApplicationProvider.APP_PROPERTIES_CLASSPATH); if (in == null) { in = DefaultApplicationProvider.class .getResourceAsStream(DefaultApplicationProvider.APP_PROPERTIES_CLASSPATH); } Properties properties = new Properties(); if (in != null) { try { properties.load(new InputStreamReader(new BOMInputStream(in), StandardCharsets.UTF_8)); } finally { in.close(); } } if (properties.containsKey(APOLLO_APP_ID_KEY)) { String appId = properties.getProperty(APOLLO_APP_ID_KEY); log.info("App ID is set to {} by app.id property from {}", appId, DefaultApplicationProvider.APP_PROPERTIES_CLASSPATH); return appId; } } catch (Throwable ignore) { } }}支持 Apollo 配置自动刷新类型,支持 @Value @RefreshScope @ConfigurationProperties 以及日志级别的动态刷新。具体代码查看下文链接。
@Value Apollo 本身就支持了动态刷新,需要注意的是如果@Value 使用了 SpEL 表达式,动态刷新会失效。
支持动态刷新
@Value("${simple.xxx}")private String simpleXxx;不支持动态刷新
@Value("#{'${simple.xxx}'.split(',')}")private List<String> simpleXxxs;RefreshScope(org.springframework.cloud.context.scope.refresh)是 Spring Cloud 提供的一种特殊的 scope 实现,用来实现配置、实例热加载。
动态实现过程:
配置变更时,调用 refreshScope.refreshAll() 或指定 bean。提取标准参数(System,jndi,Servlet)之外所有参数变量,把原来的Environment里的参数放到一个新建的 Spring Context 容器下重新加载,完事之后关闭新容器。提取更新过的参数(排除标准参数) ,比较出变更项,发布环境变更事件,RefreshScope 用新的环境参数重新生成Bean。重新生成的过程很简单,清除 refreshscope 缓存幷销毁 Bean,下次就会重新从 BeanFactory 获取一个新的实例(该实例使用新的配置)。
apollo 默认是不支持 ConfigurationProperties 刷新的,这块需要配合 EnvironmentChangeEvent 刷新的。
apollo 默认是不支持日志级别刷新的,这块需要配合 EnvironmentChangeEvent 刷新的。
当观察到 EnvironmentChangeEvent 时,它将有一个已更改的键值列表,应用程序将使用以下内容:1,重新绑定上下文中的任何 @ConfigurationProperties bean,代码见org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder。2,为logging.level.*中的任何属性设置记录器级别,代码见 org.springframework.cloud.logging.LoggingRebinder。
支持动态刷新
import com.ctrip.framework.apollo.model.ConfigChangeEvent;import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.BeansException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.context.environment.EnvironmentChangeEvent;import org.springframework.cloud.context.scope.refresh.RefreshScope;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.annotation.Configuration; /** * LoggerConfiguration * * @author Weichao Li (liweichao0102@gmail.com) * @since 2019/11/14 */@Configuration@Slf4jpublic class ApolloRefreshConfiguration implements ApplicationContextAware { private ApplicationContext applicationContext; @Autowired private RefreshScope refreshScope; @ApolloConfigChangeListener private void onChange(ConfigChangeEvent changeEvent) { applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); refreshScope.refreshAll(); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }}注意原有配置如果有日志级别需要初始化。
import com.ctrip.framework.apollo.Config;import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.logging.LogLevel;import org.springframework.boot.logging.LoggingSystem;import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct;import java.util.Set;
/** * logging 初始化 * * @author Weichao Li (liweichao0102@gmail.com) * @since 2019/11/14 */@Configuration@Slf4jpublic class LoggingConfiguration {
private static final String LOGGER_TAG = "logging.level.";
private static final String DEFAULT_LOGGING_LEVEL = "info";
@Autowired private LoggingSystem loggingSystem;
@ApolloConfig private Config config;
@PostConstruct public void changeLoggingLevel() { Set<String> keyNames = config.getPropertyNames(); for (String key : keyNames) { if (containsIgnoreCase(key, LOGGER_TAG)) { String strLevel = config.getProperty(key, DEFAULT_LOGGING_LEVEL); LogLevel level = LogLevel.valueOf(strLevel.toUpperCase()); loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level); } } }
private static boolean containsIgnoreCase(String str, String searchStr) { if (str == null || searchStr == null) { return false; } int len = searchStr.length(); int max = str.length() - len; for (int i = 0; i <= max; i++) { if (str.regionMatches(true, i, searchStr, 0, len)) { return true; } } return false; }}由于配置能改变程序的行为,不正确的配置甚至能引起灾难,所以对配置的修改必须有比较完善的权限控制。应用和配置的管理都有完善的权限管理机制,对配置的管理还分为了编辑和发布两个环节,从而减少人为的错误。所有的操作都有审计日志,可以方便地追踪问题

项目创建完,默认没有分配配置的编辑和发布权限,需要项目管理员进行授权。
1 点击application这个namespace的授权按钮

2 分配修改权限

3 分配发布权限

Namespace 权限分类
apollo 获取权限分类分为私有的和公共的。
private权限的Namespace,只能被所属的应用获取到。一个应用尝试获取其它应用private的Namespace,Apollo会报“404”异常。
public权限的Namespace,能被任何应用获取。
Namespace 的分类
Namespace 有三种类型,私有类型,公共类型,关联类型(继承类型)。
Apollo 私有类型 Namespace 使用说明
私有类型的 Namespace 具有 private 权限。例如服务默认的“application” Namespace 就是私有类型。
1 使用场景
2 如何使用私有类型 Namespace
一个应用下不同配置的分组,可以简单地把namespace类比为文件,不同类型的配置存放在不同的文件中,如数据库配置文件,业务属性配置,配置文件等
Apollo 公共类型 Namespace 使用说明
公共类型的 Namespace 具有 public 权限。公共类型的 Namespace 相当于游离于应用之外的配置,且通过 Namespace 的名称去标识公共 Namespace,所以公共的 Namespace 的名称必须全局唯一。
1 使用场景
2 如何使用公共类型 Namespace
@EnableApolloConfig({"application", "poizon-infra.jaeger"})# will inject 'application' namespace in bootstrap phaseapollo.bootstrap.enabled = true# will inject 'application', 'poizon-infra.jaeger' namespaces in bootstrap phaseapollo.bootstrap.namespaces = application,poizon-infra.jaegerApollo 关联类型 Namespace 使用说明
关联类型又可称为继承类型,关联类型具有 private 权限。关联类型的 Namespace 继承于公共类型的 Namespace,用于覆盖公共 Namespace 的某些配置。
使用建议