承接前文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. python new和init知识点

    __new__ 方法是什么?如果将类比喻为工厂,那么__init__()方法则是该工厂的生产工人,__init__()方法接受的初始化参 数则是生产所需原料,__init__()方法会按照方法中的语句 ...

  2. 计算机的Cache和Memory访问时Write-back,Write-through及write allocate的区别

    计算机的存储系统采用Register,Cache,Memory和I/O的方式来构成存储系统,无疑是一个性能和经济性的妥协的产物.Cache和Memory机制是计算机硬件的基础内容,这里就不再啰嗦.下面 ...

  3. Python(1)

    Python 学习 Part1 1. 斐波那契数序列 >>> a,b=0,1 >>> a 0 >>> b 1 >>> while ...

  4. redis的持久化之AOF

    AOF Redis 分别提供了 RDB 和 AOF 两种持久化机制: RDB 将数据库的快照(snapshot)以二进制的方式保存到磁盘中. AOF 则以协议文本的方式,将所有对数据库进行过写入的命令 ...

  5. HTTP相关:TCP/IP、DNS

    最近在看HTTP的书,看得有点慢,而且断断续续的,很多东西看完就忘了.知识点多且零散,感觉要多看几遍才能消化. TCP/IP协议族按层次分为4层: 应用层: 应用层决定了向用户提供应用服务时通信的活动 ...

  6. C++11 左值、右值、右值引用

    左值.右值 在C++11中所有的值必属于左值.右值两者之一,右值又可以细分为纯右值.将亡值.在C++11中可以取地址的.有名字的就是左值,反之,不能取地址的.没有名字的就是右值(将亡值或纯右值).举个 ...

  7. idea快捷

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

  8. java对象深复制、浅复制(深拷贝、浅拷贝)的理解

    先看一个例子 User user1 = new User(); user1.setId("111"); Map<String, User> map1 = new Has ...

  9. capwap学习笔记——初识capwap(四)(转)

    2.5.7 CAPWAP传输机制 WTP和AC之间使用标准的UDP客户端/服务器模式来建立通讯. CAPWAP协议支持UDP和UDP-Lite [RFC3828]. ¢ 在IPv4上,CAPWAP控制 ...

  10. centos6 安装配置ss笔记

    2018-05-17 centos6 安装配置ss笔记 操作环境:Centos 6 x86_64 bbr 服务器地址:美国 1.准备VPS 在https://www.bwh1.net可购买,购买时已默 ...