获取配置

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

主要过程

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

03-05 19:40