spring cloud config server源码解析

获取配置

curl -i http://config-server-host:port/{application}/{proflie}

主要过程

clipboard.png

ResourceController

@RequestMapping("/{name}/{profile}/{label}/**")
    public String resolve(@PathVariable String name, @PathVariable String profile,
            @PathVariable String label, HttpServletRequest request) throws IOException {
        String path = getFilePath(request, name, profile, label);
        return resolve(name, profile, label, path);
    }

resolve方法

synchronized String resolve(String name, String profile, String label, String path)
            throws IOException {
        if (label != null && label.contains("(_)")) {
            // "(_)" is uncommon in a git branch name, but "/" cannot be matched
            // by Spring MVC
            label = label.replace("(_)", "/");
        }
        StandardEnvironment environment = prepareEnvironment(
                this.environmentRepository.findOne(name, profile, label));

        // ensure InputStream will be closed to prevent file locks on Windows
        try (InputStream is = this.resourceRepository.findOne(name, profile, label, path)
                .getInputStream()) {
            String text = StreamUtils.copyToString(is, Charset.forName("UTF-8"));
            return resolvePlaceholders(environment, text);
        }
    }

GenericResourceRepository.findOne

public synchronized Resource findOne(String application, String profile, String label,
            String path) {
        String[] locations = this.service.getLocations(application, profile, label).getLocations();
        try {
            for (int i = locations.length; i-- > 0;) {
                String location = locations[i];
                for (String local : getProfilePaths(profile, path)) {
                    Resource file = this.resourceLoader.getResource(location)
                            .createRelative(local);
                    if (file.exists() && file.isReadable()) {
                        return file;
                    }
                }
            }
        }
        catch (IOException e) {
            throw new NoSuchResourceException(
                    "Error : " + path + ". (" + e.getMessage() + ")");
        }
        throw new NoSuchResourceException("Not found: " + path);
    }

JGitEnvironmentRepository.getLocations

public synchronized Locations getLocations(String application, String profile,
            String label) {
        if (label == null) {
            label = this.defaultLabel;
        }
        String version = refresh(label);
        return new Locations(application, profile, label, version,
                getSearchLocations(getWorkingDirectory(), application, profile, label));
    }

refresh

/**
     * Get the working directory ready.
     */
    private String refresh(String label) {
        initialize();
        Git git = null;
        try {
            git = createGitClient();
            if (shouldPull(git)) {
                fetch(git, label);
                //checkout after fetch so we can get any new branches, tags, ect.
                checkout(git, label);
                if(isBranch(git, label)) {
                    //merge results from fetch
                    merge(git, label);
                    if (!isClean(git)) {
                        logger.warn("The local repository is dirty. Resetting it to origin/"
                                + label + ".");
                        resetHard(git, label, "refs/remotes/origin/" + label);
                    }
                }
            }
            else{
                //nothing to update so just checkout
                checkout(git, label);
            }
            //always return what is currently HEAD as the version
            return git.getRepository().getRef("HEAD").getObjectId().getName();
        }
        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);
            }
        }
    }

shouldPull

public /*public for testing*/ boolean shouldPull(Git git) throws GitAPIException {
        boolean shouldPull;
        Status gitStatus = git.status().call();
        boolean isWorkingTreeClean = gitStatus.isClean();
        String originUrl = git.getRepository().getConfig().getString("remote", "origin",
                "url");

        if (this.forcePull && !isWorkingTreeClean) {
            shouldPull = true;
            logDirty(gitStatus);
        }
        else {
            shouldPull = isWorkingTreeClean && originUrl != null;
        }
        if (!isWorkingTreeClean && !this.forcePull) {
            this.logger.info("Cannot pull from remote " + originUrl
                    + ", the working tree is not clean.");
        }
        return shouldPull;
    }

docs

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值