linux阿波罗配置文件放在哪,Apollo阿波罗配置中心
一、準(zhǔn)備工作
1.2.1 AppId classpath:/META-INF/app.properties ?內(nèi)容app.id=YOUR-APP-ID
1.2.2 Environment 對(duì)于Mac/Linux,文件位置為/opt/settings/server.properties 例如env=DEV 詳細(xì)見(jiàn)文檔。
1.2.3 本地緩存路徑 /opt/data/{appId}/config-cache (權(quán)限)
二、Maven Dependency
com.ctrip.framework.apollo
apollo-client
0.7.0
三、客戶(hù)端用法
3.1 API使用方式
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
String someKey = "someKeyFromDefaultNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);3.1.2 監(jiān)聽(tīng)配置變化事件
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()));
}
}
});3.1.3 獲取公共Namespace的配置
String somePublicNamespace = "CAT";
Config config = ConfigService.getConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
String someKey = "someKeyFromPublicNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);3.2 Spring整合方式
3.2.1 基于XML的配置
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">
3.2.3 Spring Annotation支持Apollo同時(shí)還增加了兩個(gè)新的Annotation來(lái)簡(jiǎn)化在Spring環(huán)境中的使用。
@ApolloConfig
用來(lái)自動(dòng)注入Config對(duì)象
@ApolloConfigChangeListener
用來(lái)自動(dòng)注冊(cè)ConfigChangeListener
public class TestApolloAnnotationBean {
@ApolloConfig
private Config config; //inject config for namespace application
@ApolloConfig("application")
private Config anotherConfig; //inject config for namespace application
@ApolloConfig("FX.apollo")
private Config yetAnotherConfig; //inject config for namespace FX.apollo
@Value("${batch:100}")
private int batch;
//config change listener for namespace application
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
//update injected value of batch if it is changed in Apollo
if (changeEvent.isChanged("batch")) {
batch = config.getIntProperty("batch", 100);
}
}
//config change listener for namespace application
@ApolloConfigChangeListener("application")
private void anotherOnChange(ConfigChangeEvent changeEvent) {
//do something
}
//config change listener for namespaces application and FX.apollo
@ApolloConfigChangeListener({"application", "FX.apollo"})
private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
//do something
}
//example of getting config from Apollo directly
//this will always return the latest value of timeout
public int getTimeout() {
return config.getIntProperty("timeout", 200);
}
//example of getting config from injected value
//the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above
public int getBatch() {
return this.batch;
}
}轉(zhuǎn)載文章:
Apollo配置
總結(jié)
以上是生活随笔為你收集整理的linux阿波罗配置文件放在哪,Apollo阿波罗配置中心的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux数据污点标记,Taints和T
- 下一篇: 安装Linux显示内部错误,Ubuntu