源码构建Apollo以及改造

下载release版本

本次构建的是1.4.0的版本

初始化数据库信息

数据库表信息

修改注册中心配置

初始化数据库表后,需要修改 ApolloConfigDB.ServerConfig表中的注册中心信息,apollo在启动的时候回读取该表的信息然后将服务注册上去。

初始化配置环境信息

修改ApolloPortalDB.serverConfig表的apollo.portal.envs
VClTsA.png

修改对应数据库配置

修改打包脚本

  1. 位置:scripts/build.sh
  2. 修改配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    # 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库。

  1. 执行脚本
    1
    ./build.sh

注意:修改dev_meta的信息要与实际启动的机器相同

上传压缩包

对用户来说,实际有用的包就是三个:configService,adminService,portalService。执行完脚本后可以看到
VCMX0P.png
VCMOmt.png
VCMqOI.png

  1. 解压相应的带github标签的包
  2. 修改相关配置
    1. portal,admin包不需要修改
    2. config包修改
1
2
3
4
5
6
7
8
9
10
11
├── 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步骤设置的启动脚本一致

启动

修改完后数据库配置信息,和启动脚本的端口后,直接运行启动脚本。

效果图

VCQxD1.md.png
左侧就能看到对应的不同环境的配置信息了。

项目依赖

在运行build.sh脚本的时候,会将apollo运行和依赖相关的jar包打包。apollo服务端运行的话就只需要adminservice,configservice,portal三个模块。如果其它项目需要使用这个配置中心就需要将打包好的client包依赖进去。

当其它项目想使用这个配置中心,传统的做法是需要在application.yml中添加apollo.meata=xxxx.xxx的配置信息来告诉项目此时该连接哪个配置中心下载哪些配置中心的配置。但是可以优化这个操作,具体步骤如下

在core模块添加配置中心配置信息

VC1fwq.png

具体配置信息要跟build.sh脚本指定的一致

1
2
3
4
dev.meta=http://node3:8080
#fat_meta=http://fill-in-fat-meta-server:8080
uat.meta=http://node3:8082
pro.meta=http://node1:8083

修改core的pom文件打包方式,将配置文件打包进jar中

VC3QXj.md.png

将打包好的client,core上传到私服

具体使用和改造

如果就这样引入客户端还是无法读到相关配置的,需要修改core模块的相关代码。

经调试,如果客户端中没有配置apollo.meta=xxx的配置,他会默认返回http://apollo.meta,具体的实现在LegacyMetaServerProvider中,需要做一下改造,来根据实际环境连接读取相应的configservice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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"));
}

配置文件与环境相匹配

1
2
3
4
5
@Override
public String getMetaServerAddress(Env targetEnv) {
String metaServerAddress = domains.get(targetEnv);
return metaServerAddress == null ? null : metaServerAddress.trim();
}

新特性

自动更新配置

具体实现在AutoUpdateConfigChangeListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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);
}
}