09、Eureka教程-EurekaServerConfig创建过程

EurekaServerConfig创建过程

这一篇我们来看看EurekaServerConfig是如何创建的。只看重点~

EurekaServerConfig eurekaServerConfig = new DefaultEurekaServerConfig();

这里创建了一个DefaultEurekaServerConfig实现类,点进去看看~


private static final String ARCHAIUS_DEPLOYMENT_ENVIRONMENT = "archaius.deployment.environment";

public DefaultEurekaServerConfig() {

    init();
}

private void init() {

    String env = ConfigurationManager.getConfigInstance().getString(
            EUREKA_ENVIRONMENT, TEST);
    ConfigurationManager.getConfigInstance().setProperty(
            ARCHAIUS_DEPLOYMENT_ENVIRONMENT, env);

    String eurekaPropsFile = EUREKA_PROPS_FILE.get();
    try {

        ConfigurationManager
                .loadCascadedPropertiesFromResources(eurekaPropsFile);
    } catch (IOException e) {

        logger.warn(
                "Cannot find the properties specified : {}. This may be okay if there are other environment "
                        + "specific properties or the configuration is installed with a different mechanism.",
                eurekaPropsFile);
    }
}

可以看到这里先拿到了Eureka的环境,这里是从环境初始化的方法里面设置的,然后又把env设置到了archaius.deployment.environment参数中。接着又从配置中拿到了Eureka配置文件的文件名,最后又调用了ConfigurationManager .loadCascadedPropertiesFromResources(eurekaPropsFile);方法加载配置,我们继续跟进。

static volatile AbstractConfiguration instance = null;

public static void loadCascadedPropertiesFromResources(String configName) throws IOException {

    Properties props = loadCascadedProperties(configName);
    if (instance instanceof AggregatedConfiguration) {

        ConcurrentMapConfiguration config = new ConcurrentMapConfiguration();
        config.loadProperties(props);
        ((AggregatedConfiguration) instance).addConfiguration(config, configName);
    } else {

        ConfigurationUtils.loadProperties(props, instance);
    }
}

这里可以看到它是调用了loadCascadedProperties(configName)方法加载到了一个java原生的Properties类,然后根据AbstractConfiguration类的具体实现把加载到的配置封装了一下,这里应该不是重点,就不看了,重点关注loadCascadedProperties(configName)方法,继续跟进~

private static Properties loadCascadedProperties(String configName) throws IOException {

    String defaultConfigFileName = configName + ".properties";
    if (instance == null) {

        instance = getConfigInstance();
    }
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(defaultConfigFileName);
    if (url == null) {

        throw new IOException("Cannot locate " + defaultConfigFileName + " as a classpath resource.");
    }
    Properties props = getPropertiesFromFile(url);
    String environment = getDeploymentContext().getDeploymentEnvironment();
    if (environment != null && environment.length() > 0) {

        String envConfigFileName = configName + "-" + environment + ".properties";
        url = loader.getResource(envConfigFileName);
        if (url != null) {

            Properties envProps = getPropertiesFromFile(url);
            if (envProps != null) {

                props.putAll(envProps);
            }
        }
    }
    return props;
}

这里可以看到,它是拼接了文件名,然后通过java底层的API去读取了配置文件里面的属性。
那么到这里,整个EurekaServerConfig就创建完毕了~

二、总结

EurekaServerConfig的创建过程也是比较简单的,那还是继续上个图吧~