最近在學習使用apollo,發現有不少優點,使用這個産品可以取代之前查表的方式去擷取生産環境中動态設定的系統參數。這樣就減少了資料庫的通路次數。
同時apollo也在本機如下位置生成一個存儲配置值的檔案。該檔案名由appid+cluster+namespace三者的名字拼接而成。

使用方式多種多樣,而且較為友善。我是采用的這種屬性擷取方式。
@Value("${poolSize:100}")
public String pollSize;
以下是用來測試的main方法
public static void main(String[] args) {
System.setProperty(“env”, “DEV”);
System.setProperty(“app.id”, “2019080101”);
System.setProperty(“apollo.cluster”, “default”);
System.setProperty(“apollo.meta”, “http://192.168.8.129:8080”);
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
String someKey = “dataSize”;
String someDefaultValue = “1000”;
String value = config.getProperty(someKey, someDefaultValue);
System.out.println(“poolSize大小是:”+value);
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()));
}
}
});
// while (true) {
//
// }
}