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.配置存储/

相关推荐
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
Dcs6 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java
fouryears_234178 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc
阿葱(聪)8 小时前
java 在k8s中的部署流程
java·开发语言·docker·kubernetes
浮生带你学Java9 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展
板板正9 小时前
SpringAI——提示词(Prompt)、提示词模板(PromptTemplate)
java·spring boot·ai·prompt
板板正9 小时前
SpringAI——对话记忆
java·spring boot·ai
期待のcode9 小时前
图片上传实现
java·前端·javascript·数据库·servlet·交互
李长渊哦9 小时前
深入理解Java中的Map.Entry接口
java·开发语言