spring cloud config的主函數是ConfigServerApplication,其定義以下:spring
@Configuration @EnableAutoConfiguration @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigServerApplication.class) .properties("spring.config.name=configserver").run(args); } }
其中app
@Configuration是spring定義的註解,使用註解,配置信息的載體由 XML 文件轉移到了 Java 類中。ide
@EnableAutoConfiguration是spring boot定義的註解,控制spring applicationContext的自動配置的開關。函數
@EnableConfigServer是spring cloud定義的註解,ui
@EnableConfigServer定義以下:this
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({ ResourceRepositoryConfiguration.class, EnvironmentRepositoryConfiguration.class, ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class }) public @interface EnableConfigServer { }
1. ResourceRepositoryConfigurationspa
定義以下:code
@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。實現方法爲:server
@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定義了搜索資源路徑的策略,其層次結構以下:blog
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")); }