介绍
Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。
Apollo支持4个维度管理Key-Value格式的配置:
- application (应用)
- environment (环境)
- cluster (集群)
- namespace (命名空间)
Apollo与Spring Cloud Config的功能对比
客户端配置
引入jar包
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.5.1</version>
</dependency>
<!-- apollo依赖包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
增加配置
app:
id: gateway-test # apollo中配置的appid
apollo:
meta: http://127.0.0.100:31515 #apollo配置中心地址
bootstrap:
enabled: true #向spring注入被托管的application.properties文件的配置信息
eagerLoad:
enabled: true #将Apollo配置加载提到初始化日志系统之前
mate地址
配置中心配置
创建项目
部门:应用所在部门
AppId: 应用唯一表示,对应client配置的app.id
应用名称:在配置中心显示的名称,用于直观了解应用用途
添加配置
- key:spring配置文件中application配置文件中对应的参数key
- value:key对应的值
- 选择集群:在不同的环境中是否可以读取到,对应client的mate地址。
发布配置
效果
优先使用apollo中的配置信息。
应用中application配置如下:
server:
port: 8808
...
13:01:55.330 [main] INFO o.s.boot.web.embedded.netty.NettyWebServer - Netty started on port(s): 8809
13:01:55.331 [main] INFO o.s.c.n.e.s.EurekaAutoServiceRegistration - Updating port to 8809
参数格式
热更新
使用ApolloConfigChangeListener注解,当apollo重新发布更新时,调用被注解的方法,代码如下:
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
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.stereotype.Component;
/**
* 动态更新apollo配置
* @author: lizz
* @date: 2020/01/06 12:07
*/
@Component
public class ApolloConfig implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(ApolloConfig.class);
/**
* spring控制器
*/
private ApplicationContext applicationContext;
/**
* 热加载控制器
*/
private final RefreshScope refreshScope;
public ApolloConfig(RefreshScope refreshScope) {
this.refreshScope = refreshScope;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* "application"为apollo的namespace
* @param changeEvent 更新内容
*/
@ApolloConfigChangeListener("application")
//@ApolloConfigChangeListener({"application","application.yml"}) //监听多个namespace
private void configChangeListter(ConfigChangeEvent changeEvent) {
logger.info("**************Apollo动态修改配置**************");
for (String changedKey : changeEvent.changedKeys()) {
logger.info("changedKey :{}",changedKey);
logger.info("changedValue :{}",changeEvent.getChange(changedKey));
}
refreshGatewayProperties(changeEvent);
}
/**
* 更新SpringApplicationContext对象,
*
* @param changeEvent 更新内容
*/
private void refreshGatewayProperties(ConfigChangeEvent changeEvent) {
try {
//更新配置
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
//刷新后生效
for(String key :changeEvent.changedKeys()){
logger.info("**************刷新Apollo配置:{}**************",key);
refreshScope.refresh(key);
}
logger.info("**************Apollo动态修改配置成功**************");
}catch (Exception e){
logger.error("**************Apollo动态修改配置失败**************",e);
}
}
}
namespace名称application
多环境配置
多namespace
新增namespace配置
使用多namespace配置
通过namespace配置读取多个apollo配置,默认为properties格式,yml格式读取需要加后缀。
app:
id: gateway-test # apollo中配置的appid
apollo:
meta: http://xxx.xxx.xxx.xxx:31515 #apollo配置中心地址
bootstrap:
enabled: true #向spring注入被托管的application.properties文件的配置信息,默认true
eagerLoad:
enabled: true #将Apollo配置加载提到初始化日志系统之前
namespaces: application,max-route.yml #多配置环境
可以通过url查看发布的配置是否成功,单机环境clusters=default
格式:http://{meta地址}/configs/{appid}/{clusters}/namespace
如:
http://ip:31515/configs/gateway-test/default/max-route.yml
http://ip:31515/configs/gateway-test/default/application