介紹
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