一,思路整理
SpringApplication這個(gè)類,大家應(yīng)該都比較清楚了。這個(gè)類是微服務(wù)啟動(dòng)的核心類,我們啟動(dòng)一個(gè)微服務(wù)后,就會(huì)執(zhí)行SpringApplication.run()。Spring Boot因?yàn)榧闪撕芏嘟M件,配置文件也比較豐富,為了對(duì)不同的配置文件進(jìn)行解析,Spring提供了很多解析類。但是這些解析類并不是都能用上的,為了支持可插拔,Spring Boot提供了初始化器的機(jī)制。
如果我們需要新增一個(gè)初始化器,那么這樣做就可以了:
SpringApplication.addInitializers(new PropertySourceBootstrapConfiguration());
然后我們就可以根據(jù)需要,在微服務(wù)啟動(dòng)階段加載配置。
微服務(wù)從Spring Cloud Config配置中心獲取到配置信息的類是哪個(gè)呢?
答:ConfigServicePropertySourceLocator
這個(gè)類,通過(guò)發(fā)送http請(qǐng)求的方式,從Spring Cloud Config遠(yuǎn)程配置中心獲取配置信息。從遠(yuǎn)程配置中心獲取的配置信息R被CompositePropertySource包裝后,放到environment中,并且還有個(gè)響亮的名字,叫bootstrapProperties。
二,源碼解析
1,new SpringApplication(sources)
SpringApplication的實(shí)例化主要做了以下幾件事:
第一步:設(shè)置系統(tǒng)參數(shù)默認(rèn)值。
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.headless = true;
this.registerShutdownHook = true;
第二步:設(shè)置初始化器。一共6個(gè)。
this.setInitializers();
第三步:設(shè)置監(jiān)聽(tīng)器。一共13個(gè)。
this.setLisreners();
這些監(jiān)聽(tīng)器會(huì)一直監(jiān)聽(tīng)Spring項(xiàng)目的啟動(dòng)流程,每當(dāng)Spring容器狀態(tài)發(fā)生改變,就會(huì)出發(fā)響應(yīng)的事件。
2,SpringApplication.run()
SpringApplication.run()方法進(jìn)來(lái)以后會(huì)進(jìn)行環(huán)境配置等一系列操作,然后執(zhí)行下面的邏輯。
第一步:
ConfigurableEnvironment environment = this. prepareEnvironment(listeners,applicationArguments);
prepareEnvironment(listeners,applicationArguments)方法主要設(shè)置下面4個(gè)系統(tǒng)屬性,這4個(gè)屬性,是Spring Boot 系統(tǒng)環(huán)境ConfigurableEnvironment的重要成員:
servletConfigInitParams:初始化為空。
servletContextInitParams:初始化為空。
systemProperties:系統(tǒng)參數(shù),59個(gè)。注意這59個(gè)參數(shù),是我斷點(diǎn)調(diào)試的具體值,不同項(xiàng)目配置可能不同??梢酝ㄟ^(guò)Java提供的System類直接獲取到。如下:
System.getProperties("os.name");
systemProperties最終是調(diào)用native方法來(lái)獲取的。
systemEnvironment:系統(tǒng)參數(shù),46個(gè)(我的項(xiàng)目)。systemEnvironment是調(diào)用System.getenv()方法獲取的,最終也是調(diào)用native方法來(lái)獲取的。
第二步:this.configEnvironment(environment,applicationArguments.getSourceArus())
配置環(huán)境變量,主要配置了2個(gè)屬性,一個(gè)是defaultProperties,另一個(gè)是當(dāng)前環(huán)境使用的配置文件activeProfiles。
第三步:listeners.environmentPrepared(environment)
這個(gè)步驟主要初始化5個(gè)屬性:
bootstrap:這個(gè)屬性主要是初始化spring.config.name,默認(rèn)值bootstrap。
random:生成隨機(jī)數(shù),目前不知道干啥的。
applicationConfigurationProperties:解析微服務(wù)系統(tǒng)配置文件bootstrap.properties,解析以后生成的鍵值對(duì)就存放在這個(gè)對(duì)象中。
defaultProperties:這個(gè)屬性有2個(gè)配置,
一個(gè)是spring. aop. proxyTargetClass,默認(rèn)值為true,另一個(gè)是logging.pattern.level。
springCloudClientHostInfo:這個(gè)屬性包含主機(jī)名和主機(jī)IP地址,對(duì)應(yīng)的key分別為:spring.cloud.client.hostname和spring.cloud.client.ipAddress。
3,創(chuàng)建IOC容器
context = this.createApplicationContext();
創(chuàng)建IOC容器以后,接下來(lái)會(huì)把剛才配置的環(huán)境變量設(shè)置到IOC容器中。
4,設(shè)置應(yīng)用程序上下文
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 設(shè)置環(huán)境變量
context. setEnvironment(environment);
// 執(zhí)行Spring配置的初始化器
this.applyInitializers(context);
初始化器會(huì)根據(jù)配置,執(zhí)行一些列的初始化操作,比如加載遠(yuǎn)程配置文件,調(diào)整配置項(xiàng)優(yōu)先級(jí)等等。
Spring Cloud微服務(wù)如何從遠(yuǎn)程配置中心Config拉取配置信息?關(guān)鍵就在這里!
如何執(zhí)行的呢?看源碼:
ApplicationContextInitializer.intialize();
ApplicationContextInitializerApplicationContextIntializer是一個(gè)接口,具體實(shí)現(xiàn)有很多個(gè),而加載配置中心遠(yuǎn)程配置文件的實(shí)現(xiàn)是:
PropertySourceBootstrapConfiguration
進(jìn)入這個(gè)類的initialize()方法,原來(lái)是通過(guò)發(fā)送Http請(qǐng)求的方式,從遠(yuǎn)程配置中心Spring Cloud Config拉取的配置信息。
關(guān)鍵代碼是這一行:
source = locator.locate(environment);
locator是一個(gè)資源定位器接口,接口名:
PropertySourceLocator.class
具體實(shí)現(xiàn)是:
ConfigServicePropertySourceLocator.class
微服務(wù)從Spring Cloud Config配置中心獲取到配置信息后,存放在哪里呢?
答:環(huán)境變量environment中。從遠(yuǎn)程配置中心獲取的屬性被CompositePropertySource包裝后,放到environment中,并且還有個(gè)響亮的名字,叫bootstrapProperties。
到目前為止,我們來(lái)看一下environment都包含哪些屬性:
servletConfigInitParams
servletContextInitParams
systemProperties
systemEnvironment
bootstrap
random
applicationConfigurationProperties
defaultProperties
springCloudClientHostInfo
bootstrapProperties
這些屬性會(huì)隨著environment,被保存在Spring Cloud上下文ConfigurableApplicationContext中,一直為應(yīng)用程序提供基礎(chǔ)支持。
三,SpringApplication.run()為什么會(huì)執(zhí)行2次?
我在調(diào)試微服務(wù)啟動(dòng)流程的源碼時(shí),發(fā)現(xiàn)SpringApplication.run()會(huì)執(zhí)行兩次,但是至今還沒(méi)搞清楚第二次是如何觸發(fā)的?
Spring Cloud有些地方是會(huì)顯示調(diào)用SpringApplication.run(),比如ContextRefresher.refresh()就會(huì)調(diào)用。
有清楚這個(gè)問(wèn)題的,請(qǐng)不吝賜教!