网上大部分教程是springboot的,但我自己项目是ssm的,并且需要接入配置中心统一管理配置项,查资料很少,所以特地记录一下,只记录怎么接入,Apollo安装请自行查找
1. 依赖项,普通项目进入Maven仓库下载jar包导入就行
我的spring版本是5.3.26, 你如果是其他版本需要自行降低版本或提高jar包的版本
<!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-core -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/failureaccess -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>failureaccess</artifactId>
<version>1.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
</dependency>
2. app.properties配置文件
springboot可以直接集成在application.properties
配置文件中
新建的app.properties一定要放在resource
目录下, 不然Apollo识别不到
2024-11-22 发现部署测试环境东方通目录下有问题, 经排查,发现在本地环境运行时IDEA会自动将resources
目录下文件自动复制一份到classes
目录下,所以可以正常访问,但是打包后部署测试环境时有可能会有访问不到配置文件的情况。
只需要将META-INF
这个放在服务器上项目的classes
目录下即可,本地环境也可以将配置文件目录直接挪到src
目录下
配置文件内容
#接入Apollo的项目ID
app.id=APPID
#配置中心地址
apollo.meta=http://config-service-url
#注入默认项目空间application
apollo.bootstrap.enabled = true
#如果希望把日志相关的配置(如logging.level.root=info或logback-spring.xml中的参数)
#也放在Apollo管理使Apollo的加载顺序放到日志系统加载之前
apollo.bootstrap.eagerLoad.enabled=true
#集群
apollo.cluster=default
#访问密钥
apollo.access-key.secret=
#自定义配置文件的缓存路径,确保有权限可以操作目录
apollo.cache-dir=/data/config
3. 代码访问
API方式是最简单、高效使用Apollo配置的方式,不依赖Spring框架即可使用。
//从默认的配置空间中获取参数, 需要设置一个默认值,再获取不到配置中心中的值时返回
Config config = ConfigService.getAppConfig();
String key = "key";
String val = "val";
String value = config.getProperty(key, val);
//从指定的配置空间中获取值.例如dev.yml,注意要全称
Config config = ConfigService.getConfig("dev.yml");
String key = "key";
String val = "val";
String value = config.getProperty(key, val);
//非yaml/yml格式的namespace 获取时需要使用ConfigService.getConfigFile接口并指定Format,如ConfigFileFormat.XML。
String someNamespace = "test";
ConfigFile configFile = ConfigService.getConfigFile("test", ConfigFileFormat.XML);
String content = configFile.getContent();
4. 监听配置变化事件
监听配置变化事件只在应用真的关心配置变化,需要在配置变化时得到通知时使用,比如:数据库连接串变化后需要重建连接等。
如果只是希望每次都取到最新的配置的话,只需要按照上面的例子,调用config.getProperty
即可。
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
System.out.println("Changes for namespace " + changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
//对于变化的值, 做重新赋值操作
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}
});
Apollo官方文档
https://www.apolloconfig.com/#/zh/client/java-sdk-user-guide