一、前言
无论是原生Eureka还是SpringCloud中对Eureka的整合,逻辑是一样的,只是在SpringCloud中将很多组件的创建移到AutoConfiguration中去了,如果有设计到的地方,笔者在以后的学习笔记中会提到。
二、contextInitialized方法
承接我们发现的contextInitialized
方法,我们来看看其内部做了什么。大致可以发现,它是从ConfigurationManager获取到了一个ConfigInstance的配置类,然后从中拿到dataCenter和environment,再判断他们否为空,分别在做一些配置的设置。
protected void initEurekaEnvironment() throws Exception {
logger.info("Setting the eureka configuration..");
String dataCenter = ConfigurationManager.getConfigInstance().getString(EUREKA_DATACENTER);
if (dataCenter == null) {
logger.info("Eureka data center value eureka.datacenter is not set, defaulting to default");
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, DEFAULT);
} else {
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, dataCenter);
}
String environment = ConfigurationManager.getConfigInstance().getString(EUREKA_ENVIRONMENT);
if (environment == null) {
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, TEST);
logger.info("Eureka environment value eureka.environment is not set, defaulting to test");
}
}
三、创建AbstractConfiguration
从代码中可以看到,initEurekaEnvironment
方法第一步是通过ConfigurationManager
获取到了一个AbstractConfiguration
实例,并且获取到了一个数据中心的名字(猜测)。我们点进去看看~
public static final String DISABLE_DEFAULT_CONFIG = "archaius.dynamicProperty.disableDefaultConfig";
public static AbstractConfiguration getConfigInstance() {
if (instance == null) {
synchronized (ConfigurationManager.class) {
if (instance == null) {
instance = getConfigInstance(Boolean.getBoolean(DynamicPropertyFactory.DISABLE_DEFAULT_CONFIG));
}
}
}
return instance;
}
这里可以看到Eureka使用了double check的方式创建了一个AbstractConfiguration
,并且从配置文件中获取了是否禁用默认的配置。我们继续点进getConfigInstance(Boolean.getBoolean(DynamicPropertyFactory.DISABLE_DEFAULT_CONFIG));
private static AbstractConfiguration getConfigInstance(boolean defaultConfigDisabled) {
if (instance == null && !defaultConfigDisabled) {
instance = createDefaultConfigInstance();
registerConfigBean();
}
return instance;
}
可以看到这里判断了一下,如果Config的实例为空,并且没有禁用默认配置,就会创建一个实例,否则返回一个空的实例。在条件成立的情况下,会先创建一个实例,在注册这个实例。那我们先看一下createDefaultConfigInstance()方法。
private static AbstractConfiguration createDefaultConfigInstance() {
ConcurrentCompositeConfiguration config = new ConcurrentCompositeConfiguration();
try {
DynamicURLConfiguration defaultURLConfig = new DynamicURLConfiguration();
config.addConfiguration(defaultURLConfig, URL_CONFIG_NAME);
} catch (Throwable e) {
logger.warn("Failed to create default dynamic configuration", e);
}
if (!Boolean.getBoolean(DISABLE_DEFAULT_SYS_CONFIG)) {
SystemConfiguration sysConfig = new SystemConfiguration();
config.addConfiguration(sysConfig, SYS_CONFIG_NAME);
}
if (!Boolean.getBoolean(DISABLE_DEFAULT_ENV_CONFIG)) {
EnvironmentConfiguration envConfig = new EnvironmentConfiguration();
config.addConfiguration(envConfig, ENV_CONFIG_NAME);
}
ConcurrentCompositeConfiguration appOverrideConfig = new ConcurrentCompositeConfiguration();
config.addConfiguration(appOverrideConfig, APPLICATION_PROPERTIES);
config.setContainerConfigurationIndex(config.getIndexOfConfiguration(appOverrideConfig));
return config;
}
这里可以看到,它创建了一堆的配置类,具体是什么,这里就没有过多关注了,笔者想想无非就是读取一堆配置文件,然后封装一下吧~那我们回退到上一个步骤,看一下registerConfigBean()
方法干了什么。
public static final String ENABLE_JMX = "archaius.dynamicPropertyFactory.registerConfigWithJMX";
private static void registerConfigBean() {
if (Boolean.getBoolean(DynamicPropertyFactory.ENABLE_JMX)) {
try {
configMBean = ConfigJMXManager.registerConfigMbean(instance);
} catch (Exception e) {
logger.error("Unable to register with JMX", e);
}
}
}
这里可以看到,它就是先判断一下有没有配置要把bean注册到java原生的jmx里面,如果配置了就注册进去,这里我们就不关注了,不是主要的~那么到这里整个环节初始化就完成了^_^
protected void initEurekaEnvironment() throws Exception {
logger.info("Setting the eureka configuration..");
String dataCenter = ConfigurationManager.getConfigInstance().getString(EUREKA_DATACENTER);
if (dataCenter == null) {
logger.info("Eureka data center value eureka.datacenter is not set, defaulting to default");
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, DEFAULT);
} else {
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, dataCenter);
}
String environment = ConfigurationManager.getConfigInstance().getString(EUREKA_ENVIRONMENT);
if (environment == null) {
ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, TEST);
logger.info("Eureka environment value eureka.environment is not set, defaulting to test");
}
}
四、总结
其实初始化环节还是挺简单的,里面有些非主流程的细节就没有看了。这里来个到目前为止的流程图吧: