apollo應用配置集成及使用
1. 開發環境Apollo地址
用戶名:apollo 密碼:admin
開發環境Apollo管理台地址:http://localhost:8070/
開發環境Apollo Eureka地址:http://localhost:8080/
2. pom.xml引用客戶端
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
3. springboot項目集成
- properties配置:
//配置唯一appid既項目創建時的唯一id
app.id=110000001
//指定配置中心Eureka地址既metaservice
apollo.meta=@apollo.meta.service@
//啟動的bootstrap階段,向Spring容器注入
apollo.bootstrap.enabled = true
//指定該項目關聯的相關作用域
apollo.bootstrap.namespaces=application,TEST1.jdbc,部門.作用域
4. spring項目集成
創建/META-INF/app.properties 內容app.id=110000001
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<!-- application 的優先級別 -->
<apollo:config order="2"/>
<!-- 這個是最復雜的配置形式,指示Apollo注入TEST1.jdbc和TEST1.xxljob namespace的配置到Spring環境中,並且順序在application前面 -->
<apollo:config namespaces="TEST1.xxljob,TEST1.jdbc" order="1"/>
</beans>
5. 使用及應用
//=========== springboot start ===========
// 啟動類加上
@Configuration
@EnableApolloConfig
//=========== springboot end ===========
//=========== start ===========
// 獲取注入某個作用域的配置
// 如TEST1.jdbc
@ApolloConfig("部門.作用域")
private Config config;
// 獲取某個值
@Value("${key}")
private String value;
// 監聽多個或單個作用域
@ApolloConfigChangeListener({"application", "TEST1.jdbc"})
private void anotherOnChange(ConfigChangeEvent changeEvent) {
System.out.println("===================");
// 獲取key為test的change屬性 包括新值老值等
ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
System.out.println("===================");
}
//監聽所有作用域
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
//如果值發生修改了 獲取屬性
if (changeEvent.isChanged("batch")) {
batch = config.getIntProperty("batch", 100);
}
}
//獲取指定類型屬性
public int getTimeout() {
return config.getIntProperty("timeout", 200);
}
//=========== end ===========
6. 注意項
- XXXXXXConfigurer類
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer如使用以上類請替換成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。
另外Spring 3.1以后就不建議使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。
- 多環境eureka地址動態切換
1. VM options修改eureka地址
-apollo.meta.service=XXXXX.XXX.XXX
2. 使用maven profile
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<apollo.meta.service>
http://localhost:8080
</apollo.meta.service>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<apollo.meta.service>
http://devhost:38080
</apollo.meta.service>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<apollo.meta.service>
http://prohost:38080
</apollo.meta.service>
</properties>
</profile>
</profiles>