承接前文springcloud情操陶冶-springcloud config server(一),本文將在前文的基礎上講解config server的涉外接口html
經過前文筆者得知,cloud config server提供了多種方式的外部源配置獲取。固然也暴露了接口供外界調用,通用的方式是經過JMX接口來調用;筆者則比較關注其MVC方式,這也是本文分析的重點java
該自動配置類將server所涉及的功能安排的明明白白的。筆者先貼下它的源碼spring
@Configuration // 只有使用了@EnableConfigServer註解纔會生效 @ConditionalOnBean(ConfigServerConfiguration.Marker.class) @EnableConfigurationProperties(ConfigServerProperties.class) @Import({ EnvironmentRepositoryConfiguration.class, CompositeConfiguration.class, ResourceRepositoryConfiguration.class, ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class }) public class ConfigServerAutoConfiguration { }
導入的類有五個,按照步驟來稍微分析下mvc
此類在前文已經提過了,也就是提供多種方式的獲取遠程配置,好比GIT、SVN、VAULT等,具體可查閱前文app
與前者的EnvironmentRepository環境倉庫搭配使用,內部源碼也很簡單ide
private List<EnvironmentRepository> environmentRepos = new ArrayList<>(); @Bean @Primary @ConditionalOnBean(SearchPathLocator.class) public SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository() { return new SearchPathCompositeEnvironmentRepository(environmentRepos); } @Bean @Primary @ConditionalOnMissingBean(SearchPathLocator.class) public CompositeEnvironmentRepository compositeEnvironmentRepository() { return new CompositeEnvironmentRepository(environmentRepos); } @Autowired public void setEnvironmentRepos(List<EnvironmentRepository> repos) { this.environmentRepos = repos; }
上述的SearchPathLocator接口通常指代的是環境倉庫類,好比JGitEnvironmentRepository。採用了適配器方式進行了整合ui
內部的源碼也很簡單,就是建立了一個ResourceRepository對象,用於讀取符合條件的任一單個PropertySource文件this
@Configuration @EnableConfigurationProperties(ConfigServerProperties.class) @ConditionalOnMissingBean(ResourceRepository.class) public class ResourceRepositoryConfiguration { // GenericResourceRepository對象建立 @Bean @ConditionalOnBean(SearchPathLocator.class) public ResourceRepository resourceRepository(SearchPathLocator service) { return new GenericResourceRepository(service); } }
其會在ResourceController類中有所說起,下文分析。有興趣的能夠去分析其源碼,其內部有個findOne()方法用於找尋符合條件的任一文件code
public interface ResourceRepository { /** ** ** @param name 與搜尋路徑搭配使用,見前文 ** @param profile 與搜尋路徑搭配使用,見前文 ** @param label 與搜尋路徑搭配使用,見前文 ** @param path 文件具體名,好比application.properties ** ** */ Resource findOne(String name, String profile, String label, String path); }
對外暴露了EncryptionController,用於加解密參數。具體的讀者可自行分析server
@Configuration public class ConfigServerEncryptionConfiguration { @Autowired(required=false) private TextEncryptorLocator encryptor; @Autowired private ConfigServerProperties properties; @Bean public EncryptionController encryptionController() { EncryptionController controller = new EncryptionController(this.encryptor); controller.setDefaultApplicationName(this.properties.getDefaultApplicationName()); controller.setDefaultProfile(this.properties.getDefaultProfile()); return controller; } }
MVC的對外功能,主要是由此類來提供的,其建立了兩個Controller:EnvironmentController和ResourceController。前者主要暴露符合條件的全部PropertySource,後者則暴露符合條件的任一個PropertySource。
@Configuration @ConditionalOnWebApplication // 複寫WebMvcConfigurer接口 public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter { @Autowired(required = false) private EnvironmentEncryptor environmentEncryptor; @Autowired(required = false) private ObjectMapper objectMapper = new ObjectMapper(); // 增長對properties/yml/yaml文件的輸出支持 @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")); } // 針對環境屬性 @Bean public EnvironmentController environmentController(EnvironmentRepository envRepository, ConfigServerProperties server) { EnvironmentController controller = new EnvironmentController(encrypted(envRepository, server), this.objectMapper); controller.setStripDocumentFromYaml(server.isStripDocumentFromYaml()); controller.setAcceptEmpty(server.isAcceptEmpty()); return controller; } // 針對文件,與GenericResourceRepository搭配使用 @Bean @ConditionalOnBean(ResourceRepository.class) public ResourceController resourceController(ResourceRepository repository, EnvironmentRepository envRepository, ConfigServerProperties server) { ResourceController controller = new ResourceController(repository, encrypted(envRepository, server)); return controller; } }
暴露的HTTP接口本文就不贅述了,讀者可自行查閱,附上些許例子
Environment接口
最後以一張圖來代表springcloud config server的工做原理,方便後期回顧整理