SpringCloud源码走读--ConfigServer
博客专区 > 子达 的博客 > 博客详情
SpringCloud源码走读--ConfigServer
子达 发表于2年前
SpringCloud源码走读--ConfigServer
  • 发表于 2年前
  • 阅读 36
  • 收藏 1
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

摘要: SpringCloud源码走读,快速掌握整体脉络,旨在能在出现问题的时候快速定位并解决问题

先占个坑,这篇主要记录查找更新失败的问题上, 后期有时间会详细补充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);
     }
  }
}

共有 人打赏支持
粉丝 3
博文 5
码字总数 3170
×
子达
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: