承接前文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有初步的了解。