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。实现方法为:

    @Override
public 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 { @Autowired
private ConfigurableEnvironment environment; @Bean
public NativeEnvironmentRepository environmentRepository() {
return new NativeEnvironmentRepository(this.environment);
} }

2.2. MultipleJGitEnvironmentRepository

@Configuration
@ConditionalOnMissingBean(EnvironmentRepository.class)
protected static class GitRepositoryConfiguration { @Autowired
private ConfigurableEnvironment environment; @Autowired
private ConfigServerProperties server; @Bean
public 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 {
@Autowired
private ConfigurableEnvironment environment; @Autowired
private ConfigServerProperties server; @Bean
public 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 {
@Bean
public EnvironmentRepository environmentRepository(HttpServletRequest request, EnvironmentWatch watch) {
return new VaultEnvironmentRepository(request, watch, new RestTemplate());
}
}

另外还有,ConfigServerHealthIndicator、ConsulEnvironmentWatch、EnvironmentWatch

3.ConfigServerEncryptionConfiguration

定义了EncryptionController

    @Bean
public EncryptionController encryptionController() {
EncryptionController controller = new EncryptionController(this.encryptor);
controller.setDefaultApplicationName(this.properties.getDefaultApplicationName());
controller.setDefaultProfile(this.properties.getDefaultProfile());
return controller;
}

4.ConfigServerMvcConfiguration

定义了EnvironmentController和ResourceController

    @Bean
public 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;
}

支持的协议有三种:

    @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"));
}

spring cloud config配置中心源码分析之注解@EnableConfigServer的更多相关文章

  1. Apollo配置中心源码分析

    Apollo配置中心源码分析 1. apollo的核心代码分享 SpringApplication启动的关键步骤 在SpringApplication中,会加载所有实现了Init方法的类 protec ...

  2. Nacos配置中心源码分析

    1.使用 compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2.2.3.RELEASE' spring: app ...

  3. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  4. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  5. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  6. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  7. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  8. spring cloud config配置记录

    1. spring cloud config配置记录 1.1. pom <!-- 分布式配置中心 --> <dependency> <groupId>org.spr ...

  9. Spring Cloud Config 配置刷新

    客户端进行刷新操作. 1.添加 actuator包,这样 /refresh url才处于可用状态. <dependency> <groupId>org.springframew ...

随机推荐

  1. 三分钟明白 Activiti工作流 -- java运用_转载

    一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...

  2. 51Nod 蜥蜴和地下室(搜索)

    哈利喜欢玩角色扮演的电脑游戏<蜥蜴和地下室>.此时,他正在扮演一个魔术师.在最后一关,他必须和一排的弓箭手战斗.他唯一能消灭他们的办法是一个火球咒语.如果哈利用他的火球咒语攻击第i个弓箭手 ...

  3. js实现观察者模式风格替换

    如下图,我们看到两种风格:在选择男士时,页面颜色为黑色:在选择女士时,页面颜色为粉红色. 主要可以分为两类: 下拉框 ---> 被观察者 div ---> 观察者 面向过程实现风格替换: ...

  4. POJ-1001 Exponentiation 高精度算法

    题目链接:https://cn.vjudge.net/problem/POJ-1001 以前写过一个高精度乘法,但是没有小数点,实现起来也没什么难得, 现在把代码都般过来,等会把旧电脑弄一弄,暂时就不 ...

  5. ZooKeeper 特性

    ZooKeeper 拥有一个层次的命名空间.(like distributed)       注意:ZooKeeper 中不许使用相对路径.   一    ZooKeeper 数据模型         ...

  6. android 在短信发送界面, 短信发送失败时,提示音不完整,会被中断

    1. 当一条SMS到来, 此时SMS是unseen状态, 就会弹出Notification提示用户 2. 但假设处于同一个联系人的界面下, 用户会立马看到这条SMS, 此时这条SMS会被高速的标记为s ...

  7. windows下用ADT进行android NDK开发的具体教程(从环境搭建、配置到编译全过程)

    郑重申明:如需转载本博客,请注明出处,谢谢! 这几天在学习android NDK的开发.那么首先让我们来看看android NDK开发的本质是什么. NDK(Native Development Ki ...

  8. 【Android实战】Socket消息通信

    这篇博客的内容是基于http://my.oschina.net/fengcunhan/blog/178155进行改造的.所以须要先看完这篇博客,然后再来看以下的内容. 1.须要完毕的功能是直播间的so ...

  9. QThread 爬坑之旅(三种办法解决QObject: Cannot create children for a parent that is in a different thread)

    Cannot create children for a parent that is in a different thread. 在Qt的官方文档,大家知道有两种方式使用QThread. You ...

  10. .vscode folder

    https://stackoverflow.com/questions/32964920/should-i-commit-the-vscode-folder-to-source-control Che ...