上一讲主要讲了Apollo配置中心如何通过client与之交互,演示了java如何接入的方式,接下来介绍的是Apollo界面的一些常规操作。
方式一:在ApolloPortalDB的ServerConfig中直接修改key=organizations的value值。
方式二:在Portal界面上选择管理员工具-系统参数
部门选择“技术研发部”,项目负责人选择“jeff”
回到首页进入JeffApp项目就可以看到,信息已经更新了
添加public类型的Namespace
添加配置项
发布一下即可。
代码在ApolloConfigDemo
package com.jeff.apollo.demo;
import com.ctrip.framework.apollo.*;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.model.ConfigFileChangeEvent;
import com.ctrip.framework.foundation.Foundation;
import com.google.common.base.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Jason Song([email protected])
*/
public class ApolloConfigDemo {
private static final Logger logger = LoggerFactory.getLogger(ApolloConfigDemo.class);
private String DEFAULT_VALUE = "undefined";
private Config config;
private Config yamlConfig;
private Config publicConfig;
private ConfigFile publicConfigFile;
private ConfigFile applicationConfigFile;
private ConfigFile xmlConfigFile;
private YamlConfigFile yamlConfigFile;
public ApolloConfigDemo() {
ConfigChangeListener changeListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
logger.info("Changes for namespace {}", changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
change.getPropertyName(), change.getOldValue(), change.getNewValue(),
change.getChangeType());
}
}
};
config = ConfigService.getAppConfig();
config.addChangeListener(changeListener);
yamlConfig = ConfigService.getConfig("application.yaml");
yamlConfig.addChangeListener(changeListener);
publicConfig = ConfigService.getConfig("TECH.public");
publicConfig.addChangeListener(changeListener);
applicationConfigFile = ConfigService.getConfigFile("application", ConfigFileFormat.Properties);
publicConfigFile = ConfigService.getConfigFile("TECH.public", ConfigFileFormat.Properties);
// datasources.xml
xmlConfigFile = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
xmlConfigFile.addChangeListener(new ConfigFileChangeListener() {
@Override
public void onChange(ConfigFileChangeEvent changeEvent) {
logger.info(changeEvent.toString());
}
});
// application.yaml
yamlConfigFile = (YamlConfigFile) ConfigService.getConfigFile("application", ConfigFileFormat.YAML);
}
private String getConfig(String key) {
String result = config.getProperty(key, DEFAULT_VALUE);
if (DEFAULT_VALUE.equals(result)) {
result = publicConfig.getProperty(key, DEFAULT_VALUE);
}
if (DEFAULT_VALUE.equals(result)) {
result = yamlConfig.getProperty(key, DEFAULT_VALUE);
}
logger.info(String.format("Loading key : %s with value: %s", key, result));
return result;
}
private void print(String namespace) {
switch (namespace) {
case "TECH.public":
print(publicConfigFile);
return;
case "application":
print(applicationConfigFile);
return;
case "xml":
print(xmlConfigFile);
return;
case "yaml":
printYaml(yamlConfigFile);
return;
}
}
private void print(ConfigFile configFile) {
if (!configFile.hasContent()) {
System.out.println("No config file content found for " + configFile.getNamespace());
return;
}
System.out.println("=== Config File Content for " + configFile.getNamespace() + " is as follows: ");
System.out.println(configFile.getContent());
}
private void printYaml(YamlConfigFile configFile) {
System.out.println("=== Properties for " + configFile.getNamespace() + " is as follows: ");
System.out.println(configFile.getContent());
}
private void printEnvInfo() {
String message = String.format("AppId: %s, Env: %s, DC: %s, IP: %s", Foundation.app()
.getAppId(), Foundation.server().getEnvType(), Foundation.server().getDataCenter(),
Foundation.net().getHostAddress());
System.out.println(message);
}
public static void main(String[] args) throws IOException {
ApolloConfigDemo apolloConfigDemo = new ApolloConfigDemo();
apolloConfigDemo.printEnvInfo();
System.out.println(
"Apollo Config Demo. Please input key to get the value.");
while (true) {
System.out.print("> ");
String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
if (input == null || input.length() == 0) {
continue;
}
input = input.trim();
try {
if (input.equalsIgnoreCase("TECH.public")) {
apolloConfigDemo.print("TECH.public");
continue;
}
if (input.equalsIgnoreCase("application")) {
apolloConfigDemo.print("application");
continue;
}
if (input.equalsIgnoreCase("xml")) {
apolloConfigDemo.print("xml");
continue;
}
if (input.equalsIgnoreCase("yaml") || input.equalsIgnoreCase("yml")) {
apolloConfigDemo.print("yaml");
continue;
}
if (input.equalsIgnoreCase("quit")) {
System.exit(0);
}
apolloConfigDemo.getConfig(input);
} catch (Throwable ex) {
logger.error("some error occurred", ex);
}
}
}
}
启动后在控制台输入TECH.public,可以打印出公共命名空间中所有的配置项,这里只有一个是poolsize,然后输入poolsize,取值逻辑如下
private String getConfig(String key) {
String result = config.getProperty(key, DEFAULT_VALUE);
if (DEFAULT_VALUE.equals(result)) {
result = publicConfig.getProperty(key, DEFAULT_VALUE);
}
if (DEFAULT_VALUE.equals(result)) {
result = yamlConfig.getProperty(key, DEFAULT_VALUE);
}
logger.info(String.format("Loading key : %s with value: %s", key, result));
return result;
}
解释:先从默认的namespace中获取如果没有占到则从公共命名空间中找。
> TECH.public
=== Config File Content for TECH.public.properties is as follows:
poolsize=60
> poolsize
10:32:40.533 [main] INFO com.jeff.apollo.demo.ApolloConfigDemo - Loading key : poolsize with value: 60
>
如果此时我们想要在application这个私有namespace中自己定义poolsize:
那么现在输入poolsize时会打印多少呢?可以看到打印了30.自定义的poolsize覆盖了公共namespace。
poolsize
10:41:42.829 [main] INFO com.jeff.apollo.demo.ApolloConfigDemo - Loading key : poolsize with value: 30
我们再再appId为JeffApp的项目下创建一个xml格式的私有命名空间并提交发布。
继续输入xml
> xml
=== Config File Content for datasources.xml is as follows:
<a>I am datasource</a>
> application
=== Config File Content for application.properties is as follows:
batch=78
timeout=90
>
其实我们创建的xml私有空间、public公共空间及私有默认空间都会在本地缓存文件。
可以看到在appId=JeffApp,默认集群default下三个命名空间:application、xml、public