springcloud情操陶冶-springcloud config server(三)
承接前文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(三)的更多相关文章
- springcloud情操陶冶-springcloud config server(二)
承接前文springcloud情操陶冶-springcloud config server(一),本文将在前文的基础上讲解config server的涉外接口 前话 通过前文笔者得知,cloud co ...
- springcloud情操陶冶-springcloud config server(一)
承接前文springcloud情操陶冶-springcloud context(二),本文将在前文基础上浅析下ConfigServer的工作原理 前话 根据前文得知,bootstrapContext引 ...
- springcloud情操陶冶-初识springcloud
许久之前便听到了springcloud如雷贯耳的大名,但是不曾谋面,其主要应用于微服务的相关架构.笔者对微服务并不是很了解,但其既然比较出众,遂也稍微接触研究下 springcloud特性 sprin ...
- springcloud情操陶冶-bootstrapContext(三)
本文则将重点阐述context板块的自动配置类,观察其相关的特性并作相应的总结 自动配置类 直接查看cloudcontext板块下的spring.factories对应的EnableAutoConfi ...
- springcloud情操陶冶-bootstrapContext(二)
承接前文监听器对bootstrapContext创建的引导,笔者了解到其主要入口类为BootstrapImportSelectorConfiguration.本文将基于此类进行简单的分析 Bootst ...
- springcloud情操陶冶-bootstrapContext(一)
基于前文对springcloud的引导,本文则从源码角度查阅下cloud的context板块的运行逻辑 前言 springcloud是基于springboot开发的,所以读者在阅读此文前最好已经了解了 ...
- SpringCloud实战之初级入门(三)— spring cloud config搭建git配置中心
目录 1.环境介绍 2.配置中心 2.1 创建工程 2.2 修改配置文件 2.3 在github中加入配置文件 2.3 修改启动文件 3. 访问配置中心 1.环境介绍 上一篇文章中,我们介绍了如何利用 ...
- SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client
在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...
- SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支
Spring Cloud Config服务器支持一个Git仓库URL,其中包含{application}和{profile}(以及{label})的占位符. 1.各个占位符所代表的含义 applica ...
随机推荐
- 利用Python脚本悄无声息的遥控室友电脑开机密码!
整蛊一下室友就行了,切勿用于非法用途! 利用python脚本控制室友windows系统电脑的开机密码.利用random()生成随机数(密码),天知地知,密码只有你自己知道! Python代码分为cli ...
- java动态绑定与静态绑定【转】
程序绑定的概念: 绑定指的是一个方法的调用与方法所在的类(方法主体)关联起来.对java来说,绑定分为静态绑定和动态绑定:或者叫做前期绑定和后期绑定.静态绑定: 在程序执行前方法已经被绑定(也就是说在 ...
- 用Java制作一个简单的图片验证码
//Java实现简单验证码功能 package project; import java.awt.Color; import java.awt.Font;import java.awt.Graphic ...
- Java并发之CountDownLatch工具类
一.CountDownLatch工具类介绍 CountDownLatch类是Java并发工具常用的四大工具之一,CountDownLatch允许一个或者多个线程等待其他线程完成工作.假设我们有这样的一 ...
- 关于java多线程关键字volatile的理解
volatile关键字的作用是强制从公共堆栈中取得变量的值,而不是从线程私有数据栈中取得变量的值. 使用volition关键字增加了实例变量在多个线程间的可见性.但volition有个致命的缺点就是不 ...
- springmvc中只接受固定提交内容类型的请求
springmvc中的@RequestMapping注解是用来处理请求地址映射的,如果某个接口我们只接受请求的提交内容类型(Content-Type)为application/json或text/ht ...
- spring security oauth2
https://connect.qq.com/manage.html#/ http://wiki.connect.qq.com/%E7%BD%91%E7%AB%99%E5%BA%94%E7%94%A8 ...
- 系列博文-LowPoly风格的图像生成器
https://github.com/Ovilia/Polyvia http://www.zhihu.com/question/29856775/answer/57668656 http://zhan ...
- 跟我学ASP.NET MVC之三:完整的ASP.NET MVC程序-PartyInvites
摘要: 在这篇文章中,我将在一个例子中实际地展示MVC. 场景 假设一个朋友决定举办一个新年晚会,她邀请我创建一个用来邀请朋友参加晚会的WEB程序.她提出了四个注意的需求: 一个首页展示这个晚会 一个 ...
- pymongo连接MongoDB
导语 pymongo 是目前用的相对普遍一个python用来连接MongoDB的库,是工作中各种基本需求都能满足具体api可以参考 pymongo APIpymongo github 安装 Mongo ...