承接前文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. Cocos2d-x 实战

    跨平台商业项目实战:攻城大作战游戏创意触发点:做什么样的游戏?分析当前主流的游戏:经典游戏(俄罗斯方块).大众化的游戏(卡牌游戏.休闲游戏).重口味游戏. 游戏创意:生活当中 游戏开发流程:1.策划方 ...

  2. hadoop 2.x 简单实现wordCount

    简单实现hadoop程序,包括:hadoop2.x的实现写法 import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs ...

  3. Robot Framework自动化_Selenium2Library 关键字

    Robot Framework自动化_Selenium2Library 关键字 培训老师:肖能尤 2016/06/12 课程目的 一.Robot framework Selenium2Library ...

  4. vue 使用踩坑 note

    1. 如图,假如large那一行错写成 'large': item.ext_data.isLarge + '' === 'true',, 那么,编译不报错,控制台无提示,模板不输出. 2. vue的t ...

  5. 【转】Sentry--错误日志收集

    简介 Sentry是一个实时事件日志记录和汇集的日志平台,其专注于错误监控,以及提取一切事后处理所需的信息.他基于Django开发,目的在于帮助开发人员从散落在多个不同服务器上的日志文件里提取发掘异常 ...

  6. 优雅的玩PHP多进程

    proc_open (PHP 4 >= 4.3.0, PHP 5, PHP 7) proc_open — 执行一个命令,并且打开用来输入/输出的文件指针. 说明¶ resource proc_o ...

  7. C++/C实现各种排序算法(持续更新)--冒泡排序,选择排序,归并排序

    2018 3 17 今日总结一下C++中的排序算法: 1冒泡排序 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是 ...

  8. mvc上传图片

    长时间没有接触mvc,有点生疏了,这次mvc上传图片功能完成后,简单地总结下. 我围绕这三块介绍,首先是前台form表单: <style> #file_name { width: 400p ...

  9. 7-14 backbone源码

    _.extend = function(obj) { // each循环参数中的一个或多个对象 each(slice.call(arguments, 1), function(source) { // ...

  10. javascript系列1--把字符串当代码来执行

    转发请标明来源:http://www.cnblogs.com/johnhou/p/javascript.html  请尊重笔者的劳动成果  --John Hou 在javascript中有多种方法可以 ...