1. 下載release版本
本次構建的是1.4.0的版本
2. 初始化數據庫信息
2.1 修改注冊中心配置
初始化數據庫表后,需要修改 ApolloConfigDB.ServerConfig
表中的注冊中心信息,apollo在啟動的時候回讀取該表的信息然后將服務注冊上去。
2.2 初始化配置環境信息
修改ApolloPortalDB.serverConfig表的apollo.portal.envs
3. 修改對應數據庫配置
3.1 修改打包腳本
- 位置:
scripts/build.sh
- 修改配置
# apollo config db info
apollo_config_db_url=jdbc:mysql://yun2:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=root
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://yun2:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=root
# meta server url, different environments should have different meta server addresses
# 這里是對應的是各個環境中的configService地址
dev_meta=http://localhost:8080
#fat_meta=http://fill-in-fat-meta-server:8080
uat_meta=http://localhost:8082
pro_meta=http://localhost:8083
從以上腳本可以看出,需要有3個configService和3個adminService。所以需要初始化3個不同的ApolloConfigDB庫。
3. 執行腳本
./build.sh
注意:修改dev_meta
的信息要與實際啟動的機器相同
3.2 上傳壓縮包
對用戶來說,實際有用的包就是三個:configService
,adminService
,portalService
。執行完腳本后可以看到
- 解壓相應的帶github標簽的包
- 修改相關配置
- portal
- admin,config包修改,兩者數據庫配置信息要一致
admin 包配置
.
├── apollo-adminservice-1.4.0.jar
├── apollo-adminservice-1.4.0-sources.jar
├── apollo-adminservice.conf
├── apollo-adminservice_dataserveradmin.pid
├── apollo-adminservice.jar
├── config
│ ├── application-github.properties -->數據庫配置信息,與config一致
│ └── app.properties
└── scripts
├── shutdown.sh
└── startup.sh
config 包配置
├── apollo-configservice-1.4.0.jar
├── apollo-configservice-1.4.0-sources.jar
├── apollo-configservice.conf
├── apollo-configservice_dataserverapollo-configservice.pid
├── apollo-configservice.jar
├── config
│ ├── application-github.properties -->config模塊的數據庫連接信息
│ └── app.properties
└── scripts
├── shutdown.sh
└── startup.sh --->啟動端口信息
所以config模塊主要修改壓縮包解壓后兩個部分: config下的數據庫配置信息和啟動腳本,啟動端口要與3.2步驟設置的啟動腳本一致
3.3 啟動
修改完后數據庫配置信息,和啟動腳本的端口后,直接運行啟動腳本。
./admin/scripts/shutdown.sh
./config/scripts/shutdown.sh
./admin/scripts/startup.sh
./config/scripts/startup.sh
4.效果圖
左側就能看到對應的不同環境的配置信息了。
5. 項目依賴
在運行build.sh
腳本的時候,會將apollo運行和依賴相關的jar包打包。apollo服務端運行的話就只需要adminservice
,configservice
,portal
三個模塊。如果其它項目需要使用這個配置中心就需要將打包好的client包依賴進去。
當其它項目想使用這個配置中心,傳統的做法是需要在application.yml
中添加apollo.meata=xxxx.xxx
的配置信息來告訴項目此時該連接哪個配置中心下載哪些配置中心的配置。但是可以優化這個操作,具體步驟如下
5.1 在core模塊添加配置中心配置信息
具體配置信息要跟build.sh腳本指定的一致
dev.meta=http://node3:8080
#fat_meta=http://fill-in-fat-meta-server:8080
uat.meta=http://node3:8082
pro.meta=http://node1:8083
5.2 修改core的pom文件打包方式,將配置文件打包進jar中
5.3 將打包好的client,core上傳到私服
6 具體使用和改造
如果就這樣引入客戶端還是無法讀到相關配置的,需要修改core模塊的相關代碼。
經調試,如果客戶端中沒有配置apollo.meta=xxx
的配置,他會默認返回http://apollo.meta
,具體的實現在LegacyMetaServerProvider
中,需要做一下改造,來根據實際環境連接讀取相應的configservice
public LegacyMetaServerProvider() {
initialize();
}
private void initialize() {
Properties prop = new Properties();
prop = ResourceUtils.readConfigFile("apollo-env.properties", prop);
domains.put(Env.LOCAL, getMetaServerAddress(prop, "local_meta", "local.meta"));
domains.put(Env.DEV, getMetaServerAddress(prop, "dev_meta", "dev.meta"));
domains.put(Env.FAT, getMetaServerAddress(prop, "fat_meta", "fat.meta"));
domains.put(Env.UAT, getMetaServerAddress(prop, "uat_meta", "uat.meta"));
domains.put(Env.LPT, getMetaServerAddress(prop, "lpt_meta", "lpt.meta"));
domains.put(Env.PRO, getMetaServerAddress(prop, "pro_meta", "pro.meta"));
}
配置文件與環境相匹配
@Override
public String getMetaServerAddress(Env targetEnv) {
String metaServerAddress = domains.get(targetEnv);
return metaServerAddress == null ? null : metaServerAddress.trim();
}
7 新特性
7.1 自動更新配置
具體實現在AutoUpdateConfigChangeListener
@Override
public void onChange(ConfigChangeEvent changeEvent) {
Set<String> keys = changeEvent.changedKeys();
if (CollectionUtils.isEmpty(keys)) {
return;
}
for (String key : keys) {
// 1. check whether the changed key is relevant
Collection<SpringValue> targetValues = springValueRegistry.get(beanFactory, key);
if (targetValues == null || targetValues.isEmpty()) {
continue;
}
// 2. update the value
for (SpringValue val : targetValues) {
updateSpringValue(val);
}
}
}
private void updateSpringValue(SpringValue springValue) {
try {
Object value = resolvePropertyValue(springValue);
springValue.update(value);
logger.info("Auto update apollo changed value successfully, new value: {}, {}", value,
springValue);
} catch (Throwable ex) {
logger.error("Auto update apollo changed value failed, {}", springValue.toString(), ex);
}
}