9Nacos配置中心之加载配置客户端和服务端的处理
一 客户端配置中心之加载配置
prepareContext()方法
applyInitializers()方法
PropertySourceBootstrapConfiguration的initialize()方法
NacosPropertySourceLocator的locate()方法

loadNacosData()方法
ClientWork的getServerConfig()方法
9Nacos配置中心之加载配置客户端和服务端的处理
一 客户端配置中心之加载配置
prepareContext()方法
applyInitializers()方法
PropertySourceBootstrapConfiguration的initialize()方法
NacosPropertySourceLocator的locate()方法
loadNacosData()方法
ClientWork的getServerConfig()方法
二 服务端/v1/cs/configs接口的处理
总结
9Nacos配置中心之加载配置客户端和服务端的处理
一 客户端配置中心之加载配置
我们接着上个文章说的,Springboot启动的时候调用prep在reEnvironment()进行环境准备,
prepareEnvironment()进行环境准备,在启动类执行完prepareEnvironment后,执行prepareContext进行刷新应用上下文件的准备
代码如下:
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //环境准备 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; }
我们看一下prepareContext()准备上下文方法做了什么
prepareContext()方法
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } // Load the sources Set