未验证 提交 9b1c6724 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Support setting override through system.properties and envs. (#997)

* Support setting override through system.properties and envs.

* Remove test case.
上级 4a8372b9
......@@ -16,11 +16,11 @@
*
*/
package org.apache.skywalking.apm.collector.boot.config;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.skywalking.apm.collector.core.module.ApplicationConfiguration;
......@@ -30,7 +30,13 @@ import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
/**
* @author peng-yongsheng
* Initialize collector settings with following sources.
* Use application.yml as primary setting,
* and fix missing setting by default settings in application-default.yml.
*
* At last, override setting by system.properties and system.envs if the key matches moduleName.provideName.settingKey.
*
* @author peng-yongsheng, wusheng
*/
public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfiguration> {
......@@ -42,6 +48,7 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
ApplicationConfiguration configuration = new ApplicationConfiguration();
this.loadConfig(configuration);
this.loadDefaultConfig(configuration);
this.overrideConfigBySystemEnv(configuration);
return configuration;
}
......@@ -94,4 +101,44 @@ public class ApplicationConfigLoader implements ConfigLoader<ApplicationConfigur
throw new ConfigFileNotFoundException(e.getMessage(), e);
}
}
private void overrideConfigBySystemEnv(ApplicationConfiguration configuration) {
Iterator<Map.Entry<Object, Object>> entryIterator = System.getProperties().entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<Object, Object> prop = entryIterator.next();
overrideModuleSettings(configuration, prop.getKey().toString(), prop.getValue().toString(), true);
}
Map<String, String> envs = System.getenv();
for (String envKey : envs.keySet()) {
overrideModuleSettings(configuration, envKey, envs.get(envKey), false);
}
}
private void overrideModuleSettings(ApplicationConfiguration configuration, String key, String value,
boolean isSystemProperty) {
int moduleAndConfigSeparator = key.indexOf('.');
if (moduleAndConfigSeparator <= 0) {
return;
}
String moduleName = key.substring(0, moduleAndConfigSeparator);
String providerSettingSubKey = key.substring(moduleAndConfigSeparator + 1);
ApplicationConfiguration.ModuleConfiguration moduleConfiguration = configuration.getModuleConfiguration(moduleName);
if (moduleConfiguration == null) {
return;
}
int providerAndConfigSeparator = providerSettingSubKey.indexOf('.');
if (providerAndConfigSeparator <= 0) {
return;
}
String providerName = providerSettingSubKey.substring(0, providerAndConfigSeparator);
String settingKey = providerSettingSubKey.substring(providerAndConfigSeparator + 1);
if (!moduleConfiguration.has(providerName)) {
return;
}
Properties providerSettings = moduleConfiguration.getProviderConfiguration(providerName);
providerSettings.put(settingKey, value);
logger.info("The setting has been override by key: {}, value: {}, in {} provider of {} module through {}",
settingKey, value, providerName, moduleName, isSystemProperty ? "System.properties" : "System.envs");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册