SpringCloud源码走读--ConfigServer

先占个坑,这篇主要记录查找更新失败的问题上, 后期有时间会详细补充ConfigServer的主要源码分析。

源码阅读目标

  • 启动过程
  • Restful获取配置原理过程

启动

  • 通过注解引入4个class
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ EnvironmentRepositoryConfiguration.class, ResourceRepositoryConfiguration.class,
     ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class })
public @interface EnableConfigServer {
}
  • EnvironmentRepositoryConfiguration 配置 environmentRepository

Profile为native或者subversion分别配置native和svn的repository, 否则为git

  • ResourceRepositoryConfiguration:Resource仓库配置

  • ConfigServerEncryptionConfiguration: 加密bean 注册

  • ConfigServerMvcConfiguration: controller baen注册

Restful获取配置原理过程

  • 对于git上配置更新,configserver是在restful请求的时候再更新的。
  • Controller -> XXX(这部分调用很简单,略过) -> AbstractScmEnvironmentRepository.findOne -> JGitEnvironmentRepository.getLocations -> JGitEnvironmentRepository.refresh -> JGitEnvironmentRepository.shouldPull(判断是否需要pull) -> pull

AbstractScmEnvironmentRepository.java

@Override
public synchronized Environment findOne(String application, String profile, String label) {
  NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(
        getEnvironment());
  Locations locations = getLocations(application, profile, label);
  delegate.setSearchLocations(locations.getLocations());
  Environment result = delegate.findOne(application, profile, "");
  result.setVersion(locations.getVersion());
  result.setLabel(label);
  return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(),
        getUri());
}

JGitEnvironmentRepository.java

@Override
public synchronized Locations getLocations(String application, String profile,
     String label) {
  if (label == null) {
     label = this.defaultLabel;
  }
  Ref ref = refresh(application, label);
  String version = null;
  if (ref != null) {
     version = ref.getObjectId().getName();
  }
  return new Locations(application, profile, label, version,
        getSearchLocations(getWorkingDirectory(), application, profile, label));
}

private Ref refresh(String application, String label) {
  initialize();
  Git git = null;
  try {
     git = createGitClient();
     git.getRepository().getConfig().setString("branch", label, "merge", label);
     Ref ref = checkout(git, label);
     if (shouldPull(git, ref)) {
        pull(git, label, ref);
     }
     return ref;
  }
  catch (RefNotFoundException e) {
     throw new NoSuchLabelException("No such label: " + label);
  }
  catch (GitAPIException e) {
     throw new IllegalStateException("Cannot clone or checkout repository", e);
  }
  catch (Exception e) {
     throw new IllegalStateException("Cannot load environment", e);
  }
  finally {
     try {
        if (git != null) {
           git.close();
        }
     }
     catch (Exception e) {
        this.logger.warn("Could not close git repository", e);
     }
  }
}

转载于:https://my.oschina.net/u/2408085/blog/709625

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值