承接前文springcloud情操陶冶-springcloud config server(二),本文就不讲述server了,就简单阐述下client的应用

前话

config server在引入的时候也依赖config client的JAR包,也就是说本身的配置服务也集成了客户端的功能。在前文的分析中,笔者了解到默认client功能是关闭的。因为在ConfigServerBootstrapApplicationListener指定了spring.cloud.config.enabled=false(默认)

spring.factories

笔者直接翻阅了cloud config client板块中的spring.factories文件

# 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

按照步骤来分析下上述的这些类

BootstrapConfiguration对应的组件类

先分析基于bootstrapContext的组件类,看下会进行如何的组装引入

ConfigServiceBootstrapConfiguration

直接观察其源码把,还是很简单的

	@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,则不会创建上述的对象。

那么这个Bean对象到底是干啥的呢?

观察源码发现其是常见的PropertySourceLocator接口的实现类,它会在程序一启动的时候通过RestTemplate以HTTP方式请求config server从而获取配置文件,具体的读者可自行去翻阅。

DiscoveryClientConfigServiceBootstrapConfiguration

再观察另外一个组件,单看名称,应该是注册自动发现客户端的功能。这个算是一个模块了,笔者就不在此处进行分析了。后续专门开辟一个系列来探究,有兴趣的读者可自行去挖掘

EnableAutoConfiguration对应的组件类

轮到基于用户级context的组件分析了,不过其下就一个组件ConfigClientAutoConfiguration。内部的源码也比较简单就直接贴出来

@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有初步的了解。

springcloud情操陶冶-springcloud config server(三)的更多相关文章

  1. springcloud情操陶冶-springcloud config server(二)

    承接前文springcloud情操陶冶-springcloud config server(一),本文将在前文的基础上讲解config server的涉外接口 前话 通过前文笔者得知,cloud co ...

  2. springcloud情操陶冶-springcloud config server(一)

    承接前文springcloud情操陶冶-springcloud context(二),本文将在前文基础上浅析下ConfigServer的工作原理 前话 根据前文得知,bootstrapContext引 ...

  3. springcloud情操陶冶-初识springcloud

    许久之前便听到了springcloud如雷贯耳的大名,但是不曾谋面,其主要应用于微服务的相关架构.笔者对微服务并不是很了解,但其既然比较出众,遂也稍微接触研究下 springcloud特性 sprin ...

  4. springcloud情操陶冶-bootstrapContext(三)

    本文则将重点阐述context板块的自动配置类,观察其相关的特性并作相应的总结 自动配置类 直接查看cloudcontext板块下的spring.factories对应的EnableAutoConfi ...

  5. springcloud情操陶冶-bootstrapContext(二)

    承接前文监听器对bootstrapContext创建的引导,笔者了解到其主要入口类为BootstrapImportSelectorConfiguration.本文将基于此类进行简单的分析 Bootst ...

  6. springcloud情操陶冶-bootstrapContext(一)

    基于前文对springcloud的引导,本文则从源码角度查阅下cloud的context板块的运行逻辑 前言 springcloud是基于springboot开发的,所以读者在阅读此文前最好已经了解了 ...

  7. SpringCloud实战之初级入门(三)— spring cloud config搭建git配置中心

    目录 1.环境介绍 2.配置中心 2.1 创建工程 2.2 修改配置文件 2.3 在github中加入配置文件 2.3 修改启动文件 3. 访问配置中心 1.环境介绍 上一篇文章中,我们介绍了如何利用 ...

  8. SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client

    在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...

  9. SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支

    Spring Cloud Config服务器支持一个Git仓库URL,其中包含{application}和{profile}(以及{label})的占位符. 1.各个占位符所代表的含义 applica ...

随机推荐

  1. nfs 、ftp 和samba都有什么区别?

    NFS:Network File System 是已故的Sun公司制定的用于分布式访问的文件系统,它的本质是文件系统.主要在Unix系列操作系统上使用,基于TCP/IP协议层,可以将远程的计算机磁盘挂 ...

  2. Python 基础【二】 下

    str()的方法 字符串练习 1.str.capitalize str.capitalize #返回首字母大写,其他字母小写的字符串 >>> a = 'gwdsr' >> ...

  3. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 99: invalid continuation byte

    Traceback (most recent call last): File "/Users/c2apple/PycharmProjects/easyToPython/fileMethod ...

  4. 去重是distinct还是group by?

    distinct简单来说就是用来去重的,而group by的设计目的则是用来聚合统计的,两者在能够实现的功能上有些相同之处,但应该仔细区分,因为用错场景的话,效率相差可以倍计. 单纯的去重操作使用di ...

  5. idea快捷

    IntelliJ Idea 常用快捷键 列表(实战终极总结!!!!) 1. -----------自动代码-------- 常用的有fori/sout/psvm+Tab即可生成循环.System.ou ...

  6. 实现pc端信纸留言板

    效果如图: 我好像在哪里见过这样的形式,但却从来没有想过怎么实现,有种莫名的兴奋感.怎么控制什么时候换行,怎么控制中间的线条,这些视乎都是CSS无法实现的,我陷入了死局.寻找JS的做法,JS的挺复杂的 ...

  7. Oracle-10:分析函数

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 分析函数: 分析函数,可以修改排序的规则!!!!!!!!!!!!!!!!!!!!!!!! 首先把数据库脚本给放 ...

  8. 一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot

    6: 这是一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot. Giraphe是基于Spring Boot的CMS框架. https://github.co ...

  9. 超实用的JavaScript代码段 Item1 --倒计时效果

    现今团购网.电商网.门户网等,常使用时间记录重要的时刻,如时间显示.倒计时差.限时抢购等,本文分析不同倒计时效果的计算思路及方法,掌握日期对象Date,获取时间的方法,计算时差的方法,实现不同的倒时计 ...

  10. logrotate 进行nginx日志分割

    http://www.williamsang.com/archives/1254.html 日志分割常用方法: 自己写脚本分割 使用linux自带的logrotate 前者灵活,可以应对各种需求,自定 ...