Spring Cloud全解析:配置中心之springCloudConfig配置存储

配置存储

springCloudConfig是使用EnvironmentRepository来存储服务器的配置数据的,返回Environment对象

java 复制代码
public Environment(@JsonProperty("name") String name,
			@JsonProperty("profiles") String[] profiles,
			@JsonProperty("label") String label, @JsonProperty("version") String version,
			@JsonProperty("state") String state) {
		super();
		this.name = name;
		this.profiles = profiles;
		this.label = label;
		this.version = version;
		this.state = state;
	}

基于本地文件

如果存储是基于文件的,即配置了spring.cloud.config.server.native.searchLocation,此时使用的是NativeEnvironmentRepository来查找配置

使用本地文件的前提是要配置spring.prifiles.active=native

java 复制代码
@Configuration(proxyBeanMethods = false)
@Profile("native")
class NativeRepositoryConfiguration {

   @Bean
   public NativeEnvironmentRepository nativeEnvironmentRepository(
         NativeEnvironmentRepositoryFactory factory,
         NativeEnvironmentProperties environmentProperties) {
      return factory.build(environmentProperties);
   }

}

只有配置了spring.prifiles.active=native才会创建NativeEnvironmentRepository这个bean

基于Git

如果存储是基于git的,即配置了spring.cloud.config.server.git.uri,此时使用的是JGitEnvironmentRepository来查找配置

java 复制代码
if (getUri().startsWith(FILE_URI_PREFIX)) {
   return copyFromLocalRepository();
}
else {
   return cloneToBasedir();
}

如果使用file:前缀来进行设置的,此时是从本地Git仓库获取的,也就不需要克隆

java 复制代码
private Git copyFromLocalRepository() throws IOException {
   Git git;
   File remote = new UrlResource(StringUtils.cleanPath(getUri())).getFile();
   Assert.state(remote.isDirectory(), "No directory at " + getUri());
   File gitDir = new File(remote, ".git");
   Assert.state(gitDir.exists(), "No .git at " + getUri());
   Assert.state(gitDir.isDirectory(), "No .git directory at " + getUri());
   git = this.gitFactory.getGitByOpen(remote);
   return git;
}

如果不是用file前缀来进行设置的,就需要从git库去进行clone

java 复制代码
private Git cloneToBasedir() throws GitAPIException {
   CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository()
         .setURI(getUri()).setDirectory(getBasedir());
   configureCommand(clone);
   try {
      return clone.call();
   }
   catch (GitAPIException e) {
      this.logger.warn("Error occured cloning to base directory.", e);
      deleteBaseDirIfExists();
      throw e;
   }
}

https://zhhll.icu/2023/框架/微服务/springcloud/配置中心/springCloudConfig/5.配置存储/

相关推荐
喜欢读源码的小白16 分钟前
Spring Boot+MyBatis实现无限层级组织架构设计|邻接表vs闭包表性能对比|树形结构数据存储方案
java·数据库·组织结构·树级层级·无线层级
安当加密25 分钟前
基于ASP身份认证服务器实现远程办公VPN双因素认证的架构与实践
java·服务器·架构
ysdysyn39 分钟前
Java奇幻漂流:从Spring秘境到微服务星辰的冒险指南
java·spring·微服务
DARLING Zero two♡1 小时前
【优选算法】D&C-Mergesort-Harmonies:分治-归并的算法之谐
java·数据结构·c++·算法·leetcode
天天摸鱼的java工程师1 小时前
领导:“线程池又把服务器搞崩了!” 八年 Java 开发:按业务 + 服务器配,从此稳抗大促
java·后端
初级程序员Kyle1 小时前
开始改变第四天 Java并发(2)
java·后端
SimonKing2 小时前
【开发者必备】Spring Boot 2.7.x:WebMvcConfigurer配置手册来了(六)!
java·后端·程序员
caimo2 小时前
Java无法访问网址出现Timeout但是浏览器和Postman可以
java·开发语言·postman
Deamon Tree2 小时前
ElasticSearch架构和写入、更新、删除、查询的底层逻辑
java·大数据·elasticsearch·架构
北极糊的狐3 小时前
IntelliJ IDEA插件:CodeGeeX 智能助手插件
java·ide·intellij-idea