SpringCloud Config的实现

时间:2022-05-08
本文章向大家介绍SpringCloud Config的实现,主要内容包括一、config-client发送请求、二、config-server接收请求、三、建立临时仓库、四、加载配置信息、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

本文配置中心使用的git

一、config-client发送请求

ConfigServicePropertySourceLocator.locate(..)

Environment result = getRemoteEnvironment(restTemplate,
                        properties, label.trim(), state);
response = restTemplate.exchange(uri + path, HttpMethod.GET,
                    entity, Environment.class, args);

通过这步,发送http请求,URL为

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

二、config-server接收请求

ResourceController下有对应的接口

@RequestMapping("/{name}/{profiles}/{label:.*}")
public Environment labelled(@PathVariable String name, @PathVariable String profiles,
            @PathVariable String label) {
    Environment environment = this.repository.findOne(name, profiles, label);
    return environment;
}

到JGitEnvironmentRepository.getLocations()

@Override
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()步骤:

1. 初始化临时仓库文件夹
2. fetch
3. checkout
4. merge

四、加载配置信息

在临时仓库下,根据application和profile,加载配置信息,spring容器启动。