Java 客户端使用指南

环境准备

  • JDK8+
  • Guava 20.0+Apollo 客户端默认会引入 Guava 29

必选配置

Apollo 客户端依赖于 AppIdApollo Meta Server 等环境信息来读取配置。

AppId

普通应用(非 Spring Boot 应用):推荐使用 VM Options 进行配置:

1
-Dapp.id=YOUR-APP-ID

Spring Boot 应用:推荐在 application.properties 配置文件中配置:

1
app.id=YOUR-APP-ID

Apollo Meta Server

Apollo 支持同一个 AppId(应用)在不同环境下有不同的配置,所以需要提供给 Apollo 客户端在当前环境下运行的 Apollo Meta Server 地址。默认情况下,Meta ServerConfig Service 部署在同一个 JVM 进程中,所以 Meta Server 地址就是 Config Service 的地址。

普通应用(非 Spring Boot 应用):推荐使用 VM Options 配置:

1
-Dapollo.meta=http://config-service-url

Spring Boot 应用:

  • 推荐一:使用 VM Options 配置:

    首先配置环境 env,如果当前 envDEV,则配置 dev_meta

    1
    -Denv=DEV -Ddev_meta=http://config-service-url
  • 推荐二:使用 apollo-env.properties 文件:

Spring Boot 应用的 resources 目录下(classpath 下)创建 apollo-env.properties 文件,配置形式如下:

1
2
3
4
dev.meta=http://apollo-dev.xxx.com
fat.meta=http://apollo-fat.xxx.com
uat.meta=http://apollo-uat.xxx.com
pro.meta=http://apollo-pro.xxx.com

然后配置环境 env,如果当前是 DEV 环境,则在 VM Options 中指定 -Denv=DEV

跳过 Apollo Meta Server 服务发现

实际企业开发中,Config Service 很可能部署在 Docker 环境下,注册到 Meta Server 的是内网地址,本地开发环境无法直连 Meta Server。针对这种场景,我们可以跳过 Meta Server 的服务发现,直接指定 Config Service 的地址进行直连。

推荐使用 VM Options 配置:

1
-Dapollo.config-service=http://config-service-url

可选配置

Environment 环境

推荐使用 VM Options 配置:

1
-Denv=DEV

env 取值(大小写不敏感)可参考 Apollo 源码的 com.ctrip.framework.apollo.core.enums.Env.java 枚举类。

Cluster 集群

推荐使用 VM Options 配置:

1
-Dapollo.cluster=hangzhou-cluster

maven 坐标

1
2
3
4
5
6
<!-- apollo客户端 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.9.2</version>
</dependency>

API 使用

user-service 为例,首先需要在控制台初始化一些配置以供读取。

创建标准 maven 工程 apollo-samples,编写 main 方法,添加 VM Options 参数配置:

1
-Dapp.id=user-service -Denv=DEV -Ddev_meta=http://localhost:8080

下面是相关 API 使用示例:

读取默认 namespace(application)下的配置项

1
2
3
4
5
6
private static void readDefaultNamespaceConfig() {
Config appConfig = ConfigService.getAppConfig();
String defaultValue = "default value";
String value = appConfig.getProperty("simple-config", defaultValue);
System.out.println(value);
}

读取指定 namespace 下的配置项

1
2
3
4
5
private static void readSpecifyNamespaceConfig() {
Config db = ConfigService.getConfig("db");
String driverClassName = db.getProperty("spring.datasource.driver-class-name", null);
System.out.println(driverClassName);
}

读取公共 namespace 下的配置项

1
2
3
4
5
private static void readCommonNamespaceConfig() {
Config config = ConfigService.getConfig("Dev.spring-common");
String value = config.getProperty("server.servlet.context-path", null);
System.out.println(value);
}

读取 yaml/yml 格式的 namespace 下的配置项

1
2
3
4
5
private static void readYamlFormatNamespaceConfig() {
Config config = ConfigService.getConfig("application.yml");
String value = config.getProperty("yml.simple-config", null);
System.out.println(value);
}

读取非 yaml/yml 格式的 namespace 配置内容

1
2
3
4
5
private static void readNotYamlFormatNamespace() {
ConfigFile file = ConfigService.getConfigFile("application", ConfigFileFormat.TXT);
String content = file.getContent();
System.out.println(content);
}

监听配置项变更事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static void listenConfigChangeEvent() {
Config db = ConfigService.getConfig("db");
db.addChangeListener(configChangeEvent -> {
System.out.println("listened config change event, the namespace is " + configChangeEvent.getNamespace());
for (String changedKey : configChangeEvent.changedKeys()) {
ConfigChange change = configChangeEvent.getChange(changedKey);
System.out.printf("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s%n", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType());
}
});
try {// 阻塞测试事件监听
Thread.sleep(10000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}