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

相关推荐
YSRM1 分钟前
Leetcode+Java+图论+最小生成树&拓扑排序
java·leetcode·图论
沐浴露z1 小时前
【JVM】详解 Class类文件的结构
java·jvm·class
桦说编程1 小时前
Java并发编程:两种控制并发度的实现方法及其比较
java·后端
杯莫停丶1 小时前
设计模式之:单例模式
java·单例模式·设计模式
消失的旧时光-19431 小时前
@JvmStatic 的作用
java·开发语言·kotlin
火锅机器1 小时前
java 8 lambda表达式对list进行分组
java·开发语言·list
我是华为OD~HR~栗栗呀2 小时前
华为od-22届考研-测试面经
java·c++·python·功能测试·华为od·华为·面试
是梦终空2 小时前
计算机毕业设计241—基于Java+Springboot+vue的爱心公益服务系统(源代码+数据库+11000字文档)
java·spring boot·vue·毕业设计·课程设计·毕业论文·爱心公益系统
_殊途2 小时前
项目开发手册-项目结构
java