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. 200 from memory cache / from disk cache / 304 Not Modified 区别

    三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...

  2. php动态导出数据成Excel表格

    一.封装 Excel 导出类 include/components/ExecExcel.php <?php /*** * @Excel 导入导出类. */ class ExecExcel { / ...

  3. mongodb 的数据备份与恢复

    导入/导出可以操作是本地的或远程的,所以都有以下通用选项[如果是操作本地机并且没有密码的话可以省去]:                1.-h host         主机              ...

  4. Python对象引用的所有权

    目录 引用所有权 传递引用的所有权--返回值 出借引用的所有权--返回值 占据引用的所有权--参数 出借引用的所有权--参数 引用所有权 谁持有对象引用的所有权,谁就要对对象负责. 引用的所有权对函数 ...

  5. Xshell查看日志的基础使用

    2018\11\26 下载安装不多说,官网免费版即可,附上链接:https://www.netsarang.com/products/xsh_overview.html 打开后新建连接,输入主机ip即 ...

  6. 内存,寄存器和cache的区别与联系

    1. 寄存器是中央处理器内的组成部份.寄存器是有限存贮容量的高速存贮部件,它们可用来暂存指令.数据和位址.在中央处理器的控制部件中,包含的寄存器有指令寄存器(IR)和程序计数器(PC).在中央处理器的 ...

  7. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

  8. 去掉vs2010字符串下红色波浪线

    由于在vs集成了qt库,无法提升代码. 所以下载了visual assist,然后新的问题出现了,凡是在vs中输入的字符串,下面都有红色的波浪线,而且没有错误,只是看着不舒服. 解决方法: 在VAss ...

  9. Java 8 时间日期库的20个使用演示样例

    除了lambda表达式,stream以及几个小的改进之外,Java 8还引入了一套全新的时间日期API,在本篇教程中我们将通过几个简单的任务演示样例来学习怎样使用Java 8的这套API.Java对日 ...

  10. js实现小时钟,js中Date对象的使用?

    介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...