Apollo(阿波羅)是攜程框架部門研發的分布式配置中心,能夠集中化管理應用不同環境、不同集群的配置,配置修改后能夠實時推送到應用端,並且具備規范的權限、流程治理等特性,適用於微服務配置管理場景。
具體介紹可參照github:https://github.com/ctripcorp/apollo
這是需要添加的依賴。
<!--apollo-->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
圖為apollo的頁面詳情;

用戶在用java連接apollo時,需要創建app.properties在META-INF的文件夾下,而META-INF必須在resources下面,另外還需要創建aopollo-env.properties。

首先介紹app.properties
在apollo配置中心中存在AppId,這是每一個項目的唯一標識,因此app.properties內需要確定,你需要使用哪個項目的配置信息


因為apollo在java中也需要遠程連接apollo配置信息的數據,因此可以加入apollo.meta={ip}:8080,因為apollo一般是8070的端口,但我們獲取數據的端口一般是8080,這個具體需要與運維了解他們的配置的apollo的端口。這種方式也可以獲取apollo的具體數據,這似乎我們已經可以連接apollo獲取數據了,感覺之前圖中的apollo-env.properties,好像沒有什么作用了,但是在實際開發中,我們不可能只有一個環境,開發有開發環境,測試有測試環境,生產有生產環境,因此我們一般不會寫死apollo.meta,采用中間的圖的app.id,不同環境僅是服務器不同,但我們保持app.id一致。
apollo-env.properties中就是用來配置不同環境訪問不同的apollo配置中心的,apollo允許用戶配置四個不同的環境,分別是dev-開發環境,fat-功能測試環境,uat-用戶測試環境, pro-生產環境。

這似乎好像還有一個問題,服務器如何知道自己是處於什么環境因此還需要一個配置告訴apollo自己的電腦是處於什么環境
,因此開發人員需要在C:\opt\settings 的文件夾下配置server.properties,

在server.properties中告訴apollo自己在什么環境,

apollo存在四種環境,如果是開發人員那就老老實實配置DEV環境吧
在能獲取到遠程配置后,需要配置到spring中才能使用
public class AppPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
try {
//從apollo中獲取所有配置信息
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
Set<String> fieldnames = config.getPropertyNames();
//遍歷配置信息
for(String fieldname : fieldnames){
String attributeName=fieldname;
String attributeValue = config.getProperty(fieldname,"");
System.out.println("attributeName:"+attributeName + "; attributeValue:" + attributeValue );
props.put(attributeName,attributeValue);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("獲取apollo配置失敗");
}
super.processProperties(beanFactoryToProcess, props);
}
}
這樣就能將得到的所有配置存儲到spring容器中了,不過不要忘了配置xml文件
<!-- 這個是最簡單的配置形式,一般應用用這種形式就可以了,用來指示Apollo注入application namespace的配置到Spring環境中 -->
<!--<apollo:config />-->
<apollo:config order="11" />
<bean id="appPropertyPlaceholderConfigurer"
class="com.yudianbank.ApolloConfigCenter.AppPropertyPlaceholderConfigurer"></bean>
至於復雜,有多個namespace的情況,還在學習中,有所了解在記錄自己的學習
-----------------------------------------------------------------------------------------------------------
來源:CSDN
原文:https://blog.csdn.net/l695914494/article/details/86237150
