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 分钟前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole22 分钟前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端
华仔啊31 分钟前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端
程序员鱼皮1 小时前
刚刚 Java 25 炸裂发布!让 Java 再次伟大
java·javascript·计算机·程序员·编程·开发·代码
浮游本尊1 小时前
Java学习第21天 - 微服务架构设计
java
渣哥1 小时前
Java CyclicBarrier 详解:原理、使用方式与应用场景
java
杨杨杨大侠2 小时前
打开 JVM 黑匣子——走进 Java 字节码(一)
java·jvm·agent
SimonKing2 小时前
接口调用总失败?试试Spring官方重试框架Spring-Retry
java·后端·程序员
咖啡Beans2 小时前
SpringCloud网关Gateway功能实现
java·spring cloud