本文主要研究一下NacosConfigEnvironmentProcessor

NacosConfigEnvironmentProcessor

nacos-spring-boot-project/nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/autoconfigure/NacosConfigEnvironmentProcessor.java

public class NacosConfigEnvironmentProcessor implements EnvironmentPostProcessor, Ordered {

    private NacosConfigProperties nacosConfigProperties;

    private final LinkedList<NacosConfigUtils.DeferNacosPropertySource> deferPropertySources = new LinkedList<>();

    private Function<Properties, ConfigService> builder = properties -> {
        try {
            // TODO And prevent to create a large number of ConfigService optimization point is given
            return NacosFactory.createConfigService(properties);
        } catch (NacosException e) {
            throw new NacosBootConfigException("ConfigService can't be created with properties : " + properties, e);
        }
    };

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        application.addInitializers(new NacosConfigApplicationContextInitializer(this));
        nacosConfigProperties = NacosConfigPropertiesUtils.buildNacosConfigProperties(environment);
        if (enable()) {
            System.out.println("[Nacos Config Boot] : The preload log configuration is enabled");
            loadConfig(environment);
        }
    }

    private void loadConfig(ConfigurableEnvironment environment) {
        NacosConfigUtils configUtils = new NacosConfigUtils(nacosConfigProperties, environment, builder);
        configUtils.loadConfig();
        // set defer NacosPropertySource
        deferPropertySources.addAll(configUtils.getNacosPropertySources());
    }

    boolean enable() {
        return nacosConfigProperties != null && nacosConfigProperties.getBootstrap().isLogEnable();
    }

    LinkedList<NacosConfigUtils.DeferNacosPropertySource> getDeferPropertySources() {
        return deferPropertySources;
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
  • NacosConfigEnvironmentProcessor实现了EnvironmentPostProcessor, Ordered接口;其getOrder方法返回的是Ordered.LOWEST_PRECEDENCE
  • 其postProcessEnvironment方法往SpringApplication添加了NacosConfigApplicationContextInitializer;同时使用NacosConfigPropertiesUtils.buildNacosConfigProperties创建了nacosConfigProperties
  • 之后判断是否preload log configuration,是的话则执行loadConfig方法,该方法通过NacosConfigUtils.loadConfig加载配置,最后将configUtils.getNacosPropertySources()添加到deferPropertySources

NacosConfigApplicationContextInitializer

nacos-config-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/nacos/config/autoconfigure/NacosConfigApplicationContextInitializer.java

public class NacosConfigApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private final Logger logger = LoggerFactory.getLogger(NacosConfigApplicationContextInitializer.class);

    private ConfigurableEnvironment environment;

    private final NacosConfigEnvironmentProcessor processor;

    private NacosConfigProperties nacosConfigProperties;

    public NacosConfigApplicationContextInitializer(NacosConfigEnvironmentProcessor configEnvironmentProcessor) {
        this.processor = configEnvironmentProcessor;
    }

    @Override
    public void initialize(ConfigurableApplicationContext context) {
        CacheableEventPublishingNacosServiceFactory singleton = CacheableEventPublishingNacosServiceFactory.getSingleton();
        singleton.setApplicationContext(context);
        environment = context.getEnvironment();
        nacosConfigProperties = NacosConfigPropertiesUtils.buildNacosConfigProperties(environment);
        if (!enable()) {
            logger.info("[Nacos Config Boot] : The preload configuration is not enabled");
        } else {
            Function<Properties, ConfigService> builder = properties -> {
                try {
                    return singleton.createConfigService(properties);
                } catch (NacosException e) {
                    throw new NacosBootConfigException("ConfigService can't be created with properties : " + properties, e);
                }
            };
            NacosConfigUtils configUtils = new NacosConfigUtils(nacosConfigProperties, environment, builder);

            // If it opens the log level loading directly will cache DeferNacosPropertySource release
            if (processor.enable()) {
                configUtils.addListenerIfAutoRefreshed(processor.getDeferPropertySources());
            } else {
                configUtils.loadConfig();
                configUtils.addListenerIfAutoRefreshed();
            }
        }
    }

    private boolean enable() {
        return processor.enable() || nacosConfigProperties.getBootstrap().isEnable();
    }

}
  • NacosConfigApplicationContextInitializer实现了ApplicationContextInitializer接口;其initialize方法在开启preload configuration的时候会创建NacosConfigUtils,若processor也开启了preload configuration则会执行configUtils.addListenerIfAutoRefreshed(processor.getDeferPropertySources()),否则执行configUtils.loadConfig()及configUtils.addListenerIfAutoRefreshed()

小结

  • NacosConfigEnvironmentProcessor实现了EnvironmentPostProcessor, Ordered接口;其getOrder方法返回的是Ordered.LOWEST_PRECEDENCE
  • 其postProcessEnvironment方法往SpringApplication添加了NacosConfigApplicationContextInitializer;同时使用NacosConfigPropertiesUtils.buildNacosConfigProperties创建了nacosConfigProperties
  • 之后判断是否preload log configuration,是的话则执行loadConfig方法,该方法通过NacosConfigUtils.loadConfig加载配置,最后将configUtils.getNacosPropertySources()添加到deferPropertySources

doc

03-05 18:01