spring cloud config配置中心源码分析之注解@EnableConfigServer
spring cloud config的主函数是ConfigServerApplication,其定义如下:
@Configuration @EnableAutoConfiguration @EnableConfigServer public class ConfigServerApplication {public static void main(String[] args) {new SpringApplicationBuilder(ConfigServerApplication.class).properties("spring.config.name=configserver").run(args);}}
其中
@Configuration是spring定义的注解,使用注解,配置信息的载体由 XML 文件转移到了 Java 类中。
@EnableAutoConfiguration是spring boot定义的注解,控制spring applicationContext的自动配置的开关。
@EnableConfigServer是spring cloud定义的注解,
@EnableConfigServer定义如下:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({ ResourceRepositoryConfiguration.class, EnvironmentRepositoryConfiguration.class, ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class }) public @interface EnableConfigServer {}
1. ResourceRepositoryConfiguration
定义如下:
@Configuration @EnableConfigurationProperties(ConfigServerProperties.class) @ConditionalOnMissingBean(ResourceRepository.class) public class ResourceRepositoryConfiguration {@Bean@ConditionalOnBean(SearchPathLocator.class)public ResourceRepository resourceRepository(SearchPathLocator service) {return new GenericResourceRepository(service);} }
返回ResourceRepository,其实现类为:GenericResourceRepository,内部服务为SearchPathLocator。实现方法为:
@Overridepublic synchronized Resource findOne(String application, String profile, String label,String path) {String[] locations = this.service.getLocations(application, profile, label).getLocations();try {for (int i = locations.length; i-- > 0;) {String location = locations[i];for (String local : getProfilePaths(profile, path)) {Resource file = this.resourceLoader.getResource(location).createRelative(local);if (file.exists() && file.isReadable()) {return file;}}}}catch (IOException e) {throw new NoSuchResourceException("Error : " + path + ". (" + e.getMessage() + ")");}throw new NoSuchResourceException("Not found: " + path);}
SearchPathLocator定义了搜索资源路径的策略,其层次结构如下:
2.EnvironmentRepositoryConfiguration
内部定了四种环境的配置
2.1. NativeEnvironmentRepository
@Configuration@Profile("native")protected static class NativeRepositoryConfiguration {@Autowiredprivate ConfigurableEnvironment environment;@Beanpublic NativeEnvironmentRepository environmentRepository() {return new NativeEnvironmentRepository(this.environment);}}
2.2. MultipleJGitEnvironmentRepository
@Configuration@ConditionalOnMissingBean(EnvironmentRepository.class)protected static class GitRepositoryConfiguration {@Autowiredprivate ConfigurableEnvironment environment;@Autowiredprivate ConfigServerProperties server;@Beanpublic MultipleJGitEnvironmentRepository environmentRepository() {MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment);if (this.server.getDefaultLabel()!=null) {repository.setDefaultLabel(this.server.getDefaultLabel());}return repository;}}
2.3.SvnKitEnvironmentRepository
@Configuration@Profile("subversion")protected static class SvnRepositoryConfiguration {@Autowiredprivate ConfigurableEnvironment environment;@Autowiredprivate ConfigServerProperties server;@Beanpublic SvnKitEnvironmentRepository environmentRepository() {SvnKitEnvironmentRepository repository = new SvnKitEnvironmentRepository(this.environment);if (this.server.getDefaultLabel()!=null) {repository.setDefaultLabel(this.server.getDefaultLabel());}return repository;}}
2.4.VaultEnvironmentRepository
@Configuration@Profile("vault")protected static class VaultConfiguration {@Beanpublic EnvironmentRepository environmentRepository(HttpServletRequest request, EnvironmentWatch watch) {return new VaultEnvironmentRepository(request, watch, new RestTemplate());}}
另外还有,ConfigServerHealthIndicator、ConsulEnvironmentWatch、EnvironmentWatch
3.ConfigServerEncryptionConfiguration
定义了EncryptionController
@Beanpublic EncryptionController encryptionController() {EncryptionController controller = new EncryptionController(this.encryptor);controller.setDefaultApplicationName(this.properties.getDefaultApplicationName());controller.setDefaultProfile(this.properties.getDefaultProfile());return controller;}
4.ConfigServerMvcConfiguration
定义了EnvironmentController和ResourceController
@Beanpublic EnvironmentController environmentController() {EnvironmentController controller = new EnvironmentController(encrypted(), this.objectMapper);controller.setStripDocumentFromYaml(this.server.isStripDocumentFromYaml());return controller;}@Bean@ConditionalOnBean(ResourceRepository.class)public ResourceController resourceController(ResourceRepository repository) {ResourceController controller = new ResourceController(repository,encrypted());return controller;}
支持的协议有三种:
@Overridepublic 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"));}
转载于:https://www.cnblogs.com/davidwang456/p/5979563.html
spring cloud config配置中心源码分析之注解@EnableConfigServer相关推荐
- Apollo 配置中心源码分析
Apollo 配置中心源码分析 Apollo是携程开源的一款分布式配置管理中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
- 知其所以然之Nacos配置中心源码浅析
文章目录 引例 NacosConfigService的初始化 ServerHttpAgent的构造函数解析 ServerListManager的构造函数解析 ConfigFilterChainMana ...
- (七)Alian 的 Spring Cloud Config 配置中心(客户端)
目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景 通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...
- Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...
- Spring Cloud Config 配置的加密解密
配置内容的加密解密 很多场景下很多场景下,对于某些敏感的配置内容,例如数据库账号密码等应当加密存储.Config Server为配置内容的加密与解密提供了支持. 安装JCE Config Server ...
- (六)Alian 的 Spring Cloud Config 配置中心(服务端)
目录 一.简介 二.数据库 2.1.应用表 2.2.属性表 2.3.视图 2.4.初始化数据 三.配置 3.1.pom.xml 3.2.application.properties 3.3.主类 3. ...
- Spring Cloud Config配置服务
文章目录 前言 一.Spring Cloud Config Service 引入POM文件 启动配置服务 基于(Native)本地配置 配置类NativeEnvironmentProperties 解 ...
- Spring Cloud Config 配置中心
1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <p ...
- Spring Cloud Config配置中心的使用
一.概述 1. 为什么使用? 1> 配置文件太多,不方便维护 2> 配置文件一般都保存这各种明文显示的密码,无法保证配置内容的安全性,也无法做到按权限分配给个人 3> 更新配置项目需 ...
最新文章
- DDM实践:数据库秒级平滑扩容方案
- Map的4种遍历方法
- 伽马分布极大似然估计_一文通俗解释极大似然估计
- CSP2019洛谷P5665:划分(单调队列,高精度)
- springboot创建项目
- Ubuntu下安装tilix终端仿真器
- mysql count和limit,COUNT与LIMIT在mysql查询
- 攻击服务器修改数据库,SQL服务器数据库注入式攻击解释
- Codeforces Round #315 (Div. 2C) 568A Primes or Palindromes? 素数打表+暴力
- 如何更改电脑IP地址 哪个IP转换器比较好用
- 有效压缩量子数据的量子自动编码器——Quantum autoencoders for efficient compression of quantum data论文翻译
- 公路堵车概率模型:Nagel-Schreckenberg 模型模拟
- u盘容量影响计算机运行速度,插u盘导致电脑运行速度慢的解决方法
- 标识符(含义、组成、定义规则、命名规范)
- Pocket PC 2003 PC网卡上网设置
- Android 文字的收起与展开功能
- html和word相互转换
- 改善代码设计 —— 优化函数的构成(Composing Methods)
- 使用yum报错:You could try using --skip-broken to work around the problem
- MT4-EA自动化交易研究笔记(2022-04-22)
热门文章
- oracle中批量更新,oracle 批量更新
- pyqt 获取 UI 中组件_初级UI需注意10个移动端的关键原则
- redis hash key mysql_Linux取得Redis数据库中hash类型的所有feild或者所有value
- linux permit用法,技术|12 条实用的 zypper 命令范例
- 鸿蒙51单片机,基于C51系列单片机的交通控制系统设计
- emq插件开发mysql_EMQ的Mysql插件
- php下字符与二进制互转函数,PHP 字符串与二进制互转
- tf.broadcast_to
- ubantu使用apt安装时出现: xxx is not found 的解决方法
- Elasticsearch 使用过程中有哪些坑?教你避开这些坑