springcloud情操陶冶-springcloud config server(二)
承接前文springcloud情操陶冶-springcloud config server(一),本文将在前文的基础上讲解config server的涉外接口
前话
通过前文笔者得知,cloud config server提供了多种方式的外部源配置获取。当然也暴露了接口供外界调用,通用的方式是通过JMX接口来调用;笔者则比较关注其MVC方式,这也是本文分析的重点
ConfigServerAutoConfiguration
该自动配置类将server所涉及的功能安排的明明白白的。笔者先贴下它的源码
@Configuration
// 只有使用了@EnableConfigServer注解才会生效
@ConditionalOnBean(ConfigServerConfiguration.Marker.class)
@EnableConfigurationProperties(ConfigServerProperties.class)
@Import({ EnvironmentRepositoryConfiguration.class, CompositeConfiguration.class, ResourceRepositoryConfiguration.class,
ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class })
public class ConfigServerAutoConfiguration {
}
导入的类有五个,按照步骤来稍微分析下
EnvironmentRepositoryConfiguration-环境仓库配置
此类在前文已经提过了,也就是提供多种方式的获取远程配置,比如GIT、SVN、VAULT等,具体可查阅前文
CompositeConfiguration-多环境仓库Bean创建
与前者的EnvironmentRepository环境仓库搭配使用,内部源码也很简单
private List<EnvironmentRepository> environmentRepos = new ArrayList<>();
@Bean
@Primary
@ConditionalOnBean(SearchPathLocator.class)
public SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository() {
return new SearchPathCompositeEnvironmentRepository(environmentRepos);
}
@Bean
@Primary
@ConditionalOnMissingBean(SearchPathLocator.class)
public CompositeEnvironmentRepository compositeEnvironmentRepository() {
return new CompositeEnvironmentRepository(environmentRepos);
}
@Autowired
public void setEnvironmentRepos(List<EnvironmentRepository> repos) {
this.environmentRepos = repos;
}
上述的SearchPathLocator接口一般指代的是环境仓库类,比如JGitEnvironmentRepository。采用了适配器方式进行了整合
ResourceRepositoryConfiguration-文件资源仓库配置
内部的源码也很简单,就是创建了一个ResourceRepository对象,用于读取符合条件的任一单个PropertySource文件
@Configuration
@EnableConfigurationProperties(ConfigServerProperties.class)
@ConditionalOnMissingBean(ResourceRepository.class)
public class ResourceRepositoryConfiguration {
// GenericResourceRepository对象创建
@Bean
@ConditionalOnBean(SearchPathLocator.class)
public ResourceRepository resourceRepository(SearchPathLocator service) {
return new GenericResourceRepository(service);
}
}
其会在ResourceController类中有所提及,下文分析。有兴趣的可以去分析其源码,其内部有个findOne()方法用于找寻符合条件的任一文件
public interface ResourceRepository {
/**
**
** @param name 与搜寻路径搭配使用,见前文
** @param profile 与搜寻路径搭配使用,见前文
** @param label 与搜寻路径搭配使用,见前文
** @param path 文件具体名,比如application.properties
**
**
*/
Resource findOne(String name, String profile, String label, String path);
}
ConfigServerEncryptionConfiguration-加解密功能
对外暴露了EncryptionController,用于加解密参数。具体的读者可自行分析
@Configuration
public class ConfigServerEncryptionConfiguration {
@Autowired(required=false)
private TextEncryptorLocator encryptor;
@Autowired
private ConfigServerProperties properties;
@Bean
public EncryptionController encryptionController() {
EncryptionController controller = new EncryptionController(this.encryptor);
controller.setDefaultApplicationName(this.properties.getDefaultApplicationName());
controller.setDefaultProfile(this.properties.getDefaultProfile());
return controller;
}
}
ConfigServerMvcConfiguration-MVC配置
MVC的对外功能,主要是由此类来提供的,其创建了两个Controller:EnvironmentController和ResourceController。前者主要暴露符合条件的所有PropertySource,后者则暴露符合条件的任一个PropertySource。
@Configuration
@ConditionalOnWebApplication
// 复写WebMvcConfigurer接口
public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter {
@Autowired(required = false)
private EnvironmentEncryptor environmentEncryptor;
@Autowired(required = false)
private ObjectMapper objectMapper = new ObjectMapper();
// 增加对properties/yml/yaml文件的输出支持
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.mediaType("properties", MediaType.valueOf("text/plain"));
configurer.mediaType("yml", MediaType.valueOf("text/yaml"));
configurer.mediaType("yaml", MediaType.valueOf("text/yaml"));
}
// 针对环境属性
@Bean
public EnvironmentController environmentController(EnvironmentRepository envRepository, ConfigServerProperties server) {
EnvironmentController controller = new EnvironmentController(encrypted(envRepository, server), this.objectMapper);
controller.setStripDocumentFromYaml(server.isStripDocumentFromYaml());
controller.setAcceptEmpty(server.isAcceptEmpty());
return controller;
}
// 针对文件,与GenericResourceRepository搭配使用
@Bean
@ConditionalOnBean(ResourceRepository.class)
public ResourceController resourceController(ResourceRepository repository, EnvironmentRepository envRepository, ConfigServerProperties server) {
ResourceController controller = new ResourceController(repository,
encrypted(envRepository, server));
return controller;
}
}
暴露的HTTP接口本文就不赘述了,读者可自行查阅,附上些许例子
Environment接口
小结
最后以一张图来表明springcloud config server的工作原理,方便后期回顾整理
springcloud情操陶冶-springcloud config server(二)的更多相关文章
- springcloud情操陶冶-springcloud config server(三)
承接前文springcloud情操陶冶-springcloud config server(二),本文就不讲述server了,就简单阐述下client的应用 前话 config server在引入的时 ...
- springcloud情操陶冶-springcloud config server(一)
承接前文springcloud情操陶冶-springcloud context(二),本文将在前文基础上浅析下ConfigServer的工作原理 前话 根据前文得知,bootstrapContext引 ...
- 七、springcloud之配置中心Config(二)之高可用集群
方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...
- springcloud情操陶冶-初识springcloud
许久之前便听到了springcloud如雷贯耳的大名,但是不曾谋面,其主要应用于微服务的相关架构.笔者对微服务并不是很了解,但其既然比较出众,遂也稍微接触研究下 springcloud特性 sprin ...
- springcloud情操陶冶-bootstrapContext(二)
承接前文监听器对bootstrapContext创建的引导,笔者了解到其主要入口类为BootstrapImportSelectorConfiguration.本文将基于此类进行简单的分析 Bootst ...
- springcloud情操陶冶-bootstrapContext(一)
基于前文对springcloud的引导,本文则从源码角度查阅下cloud的context板块的运行逻辑 前言 springcloud是基于springboot开发的,所以读者在阅读此文前最好已经了解了 ...
- springcloud情操陶冶-bootstrapContext(三)
本文则将重点阐述context板块的自动配置类,观察其相关的特性并作相应的总结 自动配置类 直接查看cloudcontext板块下的spring.factories对应的EnableAutoConfi ...
- 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 ...
随机推荐
- bzoj 4008 亚瑟王 期望概率dp
对于这种看起来就比较傻逼麻烦的题,最关键的就是想怎么巧妙的设置状态数组,使转移尽可能的简洁. 一开始我想的是f[i][j]表示到第j轮第i张牌还没有被选的概率,后来发现转移起来特别坑爹,还会有重的或漏 ...
- STL--multiset用法
multiset: multiset<int>s; 定义正向迭代器与正向遍历: multiset<int>::iterator it; for(it=s.begin();it! ...
- 如何解决在ie下,Echarts多次使用setOption更改数据时,数据错乱问题
一.问题描述 根据用户的操作,通过Ajax请求,获取某段时间内的某数据趋势折线图数据.用户切换数据项或更改时间段时,ie中渲染的折线图包含了上一次获取的数据,导致数据错乱,如下图所示: 二.代码 数据 ...
- nodejs-5.1 ejs模板引擎
ejs官方文档:https://ejs.bootcss.com/ 1.什么是 EJS? "E" 代表 "effective",即[高效]. EJS 是一套简单的 ...
- P2P综述
原文参见:http://www.lotushy.com/?p=113 [TOC] 什么是P2P P2P全称是Peer-to-peer.P2P计算或P2P网络是一种分布式应用架构.它将任务或负载分发给P ...
- Java基础-方法重载和方法重写的区别
什么是java方法重载 (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. (2) ...
- 当需要向数据库插入空值时,sql语句的判断
方法如下: 1.int代表整形. 2.string 代表 字符型. 3.datetime ,日期类型判断如下 if(account.date!=Datetime.MinValue) { Str1.Ap ...
- 常用 Linux 命令的基本使用
常用 Linux 命令的基本使用 操作系统 作用:管理好硬件设备,让软件可以和硬件发生交互类型 桌面操作系统 Windows macos linux 服务器操作系统 linux Windows ser ...
- SpaceSyntax【空间句法】之DepthMapX学习:第二篇 输出了什么东西 与 核心概念
这节比较枯燥,都是原理,不过也有干货.这篇能不能听懂,就决定是否入门...所以,加油吧 博客园/B站/知乎/CSDN @秋意正寒 转载请在文头注明本文地址 本篇讲空间句法的几个核心概念,有一些也是重 ...
- 使用强类型实体Id来避免原始类型困扰(一)
原文地址:https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/ 作者: ...