spring cloud config配置中心源码分析之注解@EnableConfigServer
2017-01-20 14:45
459 查看
spring cloud config的主函数是ConfigServerApplication,其定义如下:
其中
@Configuration是spring定义的注解,使用注解,配置信息的载体由 XML 文件转移到了 Java 类中。
@EnableAutoConfiguration是spring boot定义的注解,控制spring applicationContext的自动配置的开关。
@EnableConfigServer是spring cloud定义的注解,
@EnableConfigServer定义如下:
1. ResourceRepositoryConfiguration
定义如下:
返回ResourceRepository,其实现类为:GenericResourceRepository,内部服务为SearchPathLocator。实现方法为:
SearchPathLocator定义了搜索资源路径的策略,其层次结构如下:
2.EnvironmentRepositoryConfiguration
内部定了四种环境的配置
2.1. NativeEnvironmentRepository
2.2. MultipleJGitEnvironmentRepository
2.3.SvnKitEnvironmentRepository
2.4.VaultEnvironmentRepository
另外还有,ConfigServerHealthIndicator、ConsulEnvironmentWatch、EnvironmentWatch
3.ConfigServerEncryptionConfiguration
定义了EncryptionController
4.ConfigServerMvcConfiguration
定义了EnvironmentController和ResourceController
支持的协议有三种:
@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")); }
相关文章推荐
- Java基本概念-过滤器
- Java 集合学习笔记(总述)
- Struts2基础学习(七)—值栈和OGNL
- (10)Java多线程之再看生产者/消费者模式
- Web开发环境搭建 Eclipse
- Java 加密 base64 encode
- Java byte[] 字节数组 转 二进制 八进制 十进制 十六进制字符串
- Eclipse SVN
- JAVA实现基于Tcp协议的简单Socket通信实例
- Java基本概念-el表达式
- Java--mac下的Intellij IDEA常用快捷键
- JAVA编程干货-参数校验
- 10 个简单易学的 Java 性能优化技巧
- JDK1.8源码阅读系列之二:LinkedList
- Java基本概念-文件上传和下载
- SpringBoot集成MyBatis
- Java基本概念-Servlet
- java8--多线程(java疯狂讲义3复习笔记)
- java数组的输出时为什么要调用Arrays.toString方法
- eclipse JAVA反编译