SkyWalking 初始化配置项
做了部分注释,源码如下:
public static void initialize(String agentOptions) throws ConfigNotFoundException, AgentPackageNotFoundException {
InputStreamReader configFileStream;
try {
// 通过agent.config 配置文件 对应的属性值
configFileStream = loadConfig();
Properties properties = new Properties();
properties.load(configFileStream);
for (String key : properties.stringPropertyNames()) {
String value = (String)properties.get(key);
//replace the key's value. properties.replace(key,value) in jdk8+
properties.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, properties));
}
// 获取到的配置值,组装 config对象
ConfigInitializer.initialize(properties, Config.class);
} catch (Exception e) {
logger.error(e, "Failed to read the config file, skywalking is going to run in default config.");
}
try {
// 通过系统环境变量、覆盖相应变量
overrideConfigBySystemProp();
} catch (Exception e) {
logger.error(e, "Failed to read the system properties.");
}
if (!StringUtil.isEmpty(agentOptions)) {
try {
agentOptions = agentOptions.trim();
logger.info("Agent options is {}.", agentOptions);
// 解析agent 参数 覆盖相应参数
overrideConfigByAgentOptions(agentOptions);
} catch (Exception e) {
logger.error(e, "Failed to parse the agent options, val is {}.", agentOptions);
}
}
if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) {
throw new ExceptionInInitializerError("`agent.service_name` is missing.");
}
if (StringUtil.isEmpty(Config.Collector.BACKEND_SERVICE)) {
throw new ExceptionInInitializerError("`collector.backend_service` is missing.");
}
IS_INIT_COMPLETED = true;
}
图解
相对SkyWalking 配置加载相对比较简单。这个优先级规则还是需要在使用过程中记住的原则。