日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring cloud config配置中心源码分析之注解@EnableConfigServer

發布時間:2025/4/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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。實現方法為:

@Overridepublic 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 {@Autowiredprivate ConfigurableEnvironment environment;@Beanpublic NativeEnvironmentRepository environmentRepository() {return new NativeEnvironmentRepository(this.environment);}}

2.2.?MultipleJGitEnvironmentRepository

@Configuration@ConditionalOnMissingBean(EnvironmentRepository.class)protected static class GitRepositoryConfiguration {@Autowiredprivate ConfigurableEnvironment environment;@Autowiredprivate ConfigServerProperties server;@Beanpublic 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 {@Autowiredprivate ConfigurableEnvironment environment;@Autowiredprivate ConfigServerProperties server;@Beanpublic 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 {@Beanpublic EnvironmentRepository environmentRepository(HttpServletRequest request, EnvironmentWatch watch) {return new VaultEnvironmentRepository(request, watch, new RestTemplate());}}

另外還有,ConfigServerHealthIndicator、ConsulEnvironmentWatch、EnvironmentWatch

3.ConfigServerEncryptionConfiguration

定義了EncryptionController

@Beanpublic EncryptionController encryptionController() {EncryptionController controller = new EncryptionController(this.encryptor);controller.setDefaultApplicationName(this.properties.getDefaultApplicationName());controller.setDefaultProfile(this.properties.getDefaultProfile());return controller;}

4.ConfigServerMvcConfiguration

定義了EnvironmentController和ResourceController

@Beanpublic 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;}

支持的協議有三種:

@Overridepublic 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"));}

?

轉載于:https://www.cnblogs.com/davidwang456/p/5979563.html

總結

以上是生活随笔為你收集整理的spring cloud config配置中心源码分析之注解@EnableConfigServer的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。