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

配置存储

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

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

@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来查找配置

if (getUri().startsWith(FILE_URI_PREFIX)) {
   return copyFromLocalRepository();
}
else {
   return cloneToBasedir();
}

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

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

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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值