聊聊NacosConfigEnvironmentProcessor

63次阅读

共计 4719 个字符,预计需要花费 12 分钟才能阅读完成。

本文主要研究一下 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

  • NacosConfigEnvironmentProcessor

正文完
 0