承接前文springcloud情操陶冶-springcloud config server(二),本文就不講述server了,就簡單闡述下client的應用html
config server在引入的時候也依賴config client的JAR包,也就是說自己的配置服務也集成了客戶端的功能。在前文的分析中,筆者瞭解到默認client功能是關閉的。由於在ConfigServerBootstrapApplicationListener指定了spring.cloud.config.enabled=false(默認)java
筆者直接翻閱了cloud config client板塊中的spring.factories文件spring
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.config.client.ConfigClientAutoConfiguration # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration,\ org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration
按照步驟來分析下上述的這些類bootstrap
先分析基於bootstrapContext的組件類,看下會進行如何的組裝引入this
直接觀察其源碼把,仍是很簡單的code
@Autowired private ConfigurableEnvironment environment; // 以spring.cloud.config做爲前綴 @Bean public ConfigClientProperties configClientProperties() { ConfigClientProperties client = new ConfigClientProperties(this.environment); return client; } // 默認狀況下,若是引入了server板塊則spring.cloud.config.enabled默認爲false @Bean @ConditionalOnMissingBean(ConfigServicePropertySourceLocator.class) @ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true) public ConfigServicePropertySourceLocator configServicePropertySource(ConfigClientProperties properties) { // 加載外部源接口 ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator( properties); return locator; }
根據上述的源碼得知當只引入cloud config client的時候,spring.cloud.config.enabled即便不配置也會建立ConfigServicePropertySourceLocator對象;反之若是引入cloud config server,但不顯式的指定spring.cloud.config.enabled=true,則不會建立上述的對象。component
那麼這個Bean對象究竟是幹啥的呢?
觀察源碼發現其是常見的PropertySourceLocator接口的實現類,它會在程序一啓動的時候經過RestTemplate以HTTP方式請求config server從而獲取配置文件,具體的讀者可自行去翻閱。server
再觀察另一個組件,單看名稱,應該是註冊自動發現客戶端的功能。這個算是一個模塊了,筆者就不在此處進行分析了。後續專門開闢一個系列來探究,有興趣的讀者可自行去挖掘xml
輪到基於用戶級context的組件分析了,不過其下就一個組件ConfigClientAutoConfiguration。內部的源碼也比較簡單就直接貼出來htm
@Configuration public class ConfigClientAutoConfiguration { // 先判斷父級bootstrapContext上下文是否存在ConfigClientProperties,與前面的ConfigServiceBootstrapConfiguration相互照應;無則再建立 @Bean public ConfigClientProperties configClientProperties(Environment environment, ApplicationContext context) { if (context.getParent() != null && BeanFactoryUtils.beanNamesForTypeIncludingAncestors( context.getParent(), ConfigClientProperties.class).length > 0) { return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(), ConfigClientProperties.class); } ConfigClientProperties client = new ConfigClientProperties(environment); return client; } // 以health.config做爲前綴的健康屬性類 @Bean public ConfigClientHealthProperties configClientHealthProperties() { return new ConfigClientHealthProperties(); } // 只有spring.cloud.config.enabled=true或者不引入server板塊才進行健康檢查 @Configuration @ConditionalOnClass(HealthIndicator.class) @ConditionalOnBean(ConfigServicePropertySourceLocator.class) @ConditionalOnProperty(value = "health.config.enabled", matchIfMissing = true) protected static class ConfigServerHealthIndicatorConfiguration { // 建立健康提示類,具體就是經過PropertySourceLocator接口輪詢去請求config server以確保服務正常 @Bean public ConfigServerHealthIndicator clientConfigServerHealthIndicator( ConfigServicePropertySourceLocator locator, ConfigClientHealthProperties properties, Environment environment) { return new ConfigServerHealthIndicator(locator, environment, properties); } } // spring.cloud.refresh.enabled=true生效,方可有ContextRefresher對象 // spring.cloud.config.watch.enabled=true生效,默認不開啓 @Configuration @ConditionalOnClass(ContextRefresher.class) @ConditionalOnBean(ContextRefresher.class) @ConditionalOnProperty(value = "spring.cloud.config.watch.enabled") protected static class ConfigClientWatchConfiguration { @Bean public ConfigClientWatch configClientWatch(ContextRefresher contextRefresher) { return new ConfigClientWatch(contextRefresher); } } }
具體建立的何種對象以及對應的功能是啥上述的註釋解釋的差很少清楚了。筆者只須要清楚如下幾點
1.建立的ConfigClientProperties對象主要是應用於client和server服務的PropertySourceLocator接口
2.建立的ConfigClientWatch對象主要用於client服務的PropertySourceLocator在用Restful接口去請求server服務的時候,對返回結果的state進行判斷,通常是服務端採起Vault方式纔會被應用。上述的state有被更改,則刷新用戶級context
3.至於健康檢查Heath功能,也就是每隔必定的時間調用PropertySourceLocator接口去遠程server服務獲取Environment對象中的PropertySource。具體的調用是屬於spring-cloud-actuator板塊的,感興趣的也能夠去查閱
其中第二點和第三點的功能主要仍是創建在spring.cloud.config.enabled=true的狀況下才會生效,因此用戶若是想使用上述說起的功能則必須確保相應的配置已打開~
spring-cloud-client模塊比較簡單,只須要和前文的圖進行關聯便很容易理解。 其既能夠單獨使用,也能夠集成至server服務中。OK,基本上spring-cloud-config板塊的分析也就到此爲止了,但願藉此能對springcloud有初步的瞭解。