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。实现方法为:
@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的更多相关文章
- Apollo配置中心源码分析
Apollo配置中心源码分析 1. apollo的核心代码分享 SpringApplication启动的关键步骤 在SpringApplication中,会加载所有实现了Init方法的类 protec ...
- Nacos配置中心源码分析
1.使用 compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2.2.3.RELEASE' spring: app ...
- Spring Cloud Config 配置中心
请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
- 微服务SpringCloud之Spring Cloud Config配置中心Git
微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...
- 微服务SpringCloud之Spring Cloud Config配置中心服务化
在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...
- spring cloud --- config 配置中心 [本地、git获取配置文件]
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...
- spring cloud config配置记录
1. spring cloud config配置记录 1.1. pom <!-- 分布式配置中心 --> <dependency> <groupId>org.spr ...
- Spring Cloud Config 配置刷新
客户端进行刷新操作. 1.添加 actuator包,这样 /refresh url才处于可用状态. <dependency> <groupId>org.springframew ...
随机推荐
- ViewPager中的数据更新
getItemPosition(Object object) { return POSITION_NONE;} 出现的问题: 我希望能够通过调用 mAdapter.notifyDataSetChang ...
- caffe(9) caffe例子
为了程序的简洁,在caffe中是不带练习数据的,因此需要自己去下载.但在caffe根目录下的data文件夹里,作者已经为我们编写好了下载数据的脚本文件,我们只需要联网,运行这些脚本文件就行了. 注意: ...
- Linux/Mac vi命令详解
刚开始学着用Linux,对vi命令不是很熟,在网上转接了一篇. vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指 ...
- 洛谷2850 【Usaco2006 Dec】虫洞Wormholes SPFA
问题描述 John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N ...
- jumpserver 安装python 报错
环境centos7.5 pip3 insatll ./python-gssapi-0.6.4.tar.gz 报错 Command "python setup.py egg_info&quo ...
- DATA_PUMP_DIR impdp 指定导出目录
1.mkdir /tdms1/oracle/dump 2.sqlplus / as sysdba 3.create directory udir as '/tdms1/oracle/dump'; 4. ...
- HTML学习----------DAY2第五节
属性为 HTML 元素提供附加信息. HTML 属性 HTML 标签可以拥有属性.属性提供了有关 HTML 元素的更多的信息. 属性总是以名称/值对的形式出现,比如:name="value& ...
- rac重新启动遭遇ORA-01078、ORA-01565、ORA-17503、ORA-12547
今天測试环境server重新启动导致一个节点集群无法重新启动,遭遇ORA-12547错误.详细例如以下: server重新启动后,rac1集群无法启动,rac2正常启动: [root@rac1 ~]# ...
- 思考一下activity的启动模式
在android里,有4种activity的启动模式.分别为:"standard" (默认) "singleTop" "singleTask" ...
- Windows cannot find ". Make sure you typed the name correctly, and then try again
https://answers.microsoft.com/en-us/windows/forum/windows_10-desktop/windows-10-explorerexe-error-wi ...