Spring Cloud学习--spring cloud config server 获取配置过程

版权声明:欢迎转载 https://blog.csdn.net/qq924862077/article/details/86557502

在上一篇博客《Spring Cloud学习--spring cloud config配置中心》 中我们已经了解学习到Spring Cloud Config简单的示例工程,这篇博客我们介绍一下Spring Cloud Config Server从git地址中获取有关配置的过程。

1、获取配置

访问http://localhost:1201/spring-cloud-config-server/master就可以获取到配置中心的相关配置信息

Spring Cloud Config Server提供了EnvironmentController,这样通过在浏览器访问即可从git中获取配置信息

@RestController
@RequestMapping(
    method = {RequestMethod.GET},
    path = {"${spring.cloud.config.server.prefix:}"}
)
public class EnvironmentController {
    private EnvironmentRepository repository;
    private ObjectMapper objectMapper;
    private boolean stripDocument;

    @RequestMapping({"/{name}/{profiles:.*[^-].*}"})
    public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
        return this.labelled(name, profiles, (String)null);
    }

    @RequestMapping({"/{name}/{profiles}/{label:.*}"})
    public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) {
        if (name != null && name.contains("(_)")) {
            name = name.replace("(_)", "/");
        }

        if (label != null && label.contains("(_)")) {
            label = label.replace("(_)", "/");
        }

        Environment environment = this.repository.findOne(name, profiles, label);
        return environment;
    }

    @RequestMapping({"/{name}-{profiles}.properties"})
    public ResponseEntity<String> properties(@PathVariable String name, @PathVariable String profiles, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws IOException {
        return this.labelledProperties(name, profiles, (String)null, resolvePlaceholders);
    }

    @RequestMapping({"/{label}/{name}-{profiles}.properties"})
    public ResponseEntity<String> labelledProperties(@PathVariable String name, @PathVariable String profiles, @PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws IOException {
        this.validateProfiles(profiles);
        Environment environment = this.labelled(name, profiles, label);
        Map<String, Object> properties = this.convertToProperties(environment);
        String propertiesString = this.getPropertiesString(properties);
        if (resolvePlaceholders) {
            propertiesString = EnvironmentPropertySource.resolvePlaceholders(EnvironmentPropertySource.prepareEnvironment(environment), propertiesString);
        }

        return this.getSuccess(propertiesString);
    }

    @RequestMapping({"{name}-{profiles}.json"})
    public ResponseEntity<String> jsonProperties(@PathVariable String name, @PathVariable String profiles, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
        return this.labelledJsonProperties(name, profiles, (String)null, resolvePlaceholders);
    }

    @RequestMapping({"/{label}/{name}-{profiles}.json"})
    public ResponseEntity<String> labelledJsonProperties(@PathVariable String name, @PathVariable String profiles, @PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
        this.validateProfiles(profiles);
        Environment environment = this.labelled(name, profiles, label);
        Map<String, Object> properties = this.convertToMap(environment);
        String json = this.objectMapper.writeValueAsString(properties);
        if (resolvePlaceholders) {
            json = EnvironmentPropertySource.resolvePlaceholders(EnvironmentPropertySource.prepareEnvironment(environment), json);
        }

        return this.getSuccess(json, MediaType.APPLICATION_JSON);
    }

    @RequestMapping({"/{name}-{profiles}.yml", "/{name}-{profiles}.yaml"})
    public ResponseEntity<String> yaml(@PathVariable String name, @PathVariable String profiles, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
        return this.labelledYaml(name, profiles, (String)null, resolvePlaceholders);
    }

    @RequestMapping({"/{label}/{name}-{profiles}.yml", "/{label}/{name}-{profiles}.yaml"})
    public ResponseEntity<String> labelledYaml(@PathVariable String name, @PathVariable String profiles, @PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws Exception {
        this.validateProfiles(profiles);
        Environment environment = this.labelled(name, profiles, label);
        Map<String, Object> result = this.convertToMap(environment);
        if (this.stripDocument && result.size() == 1 && ((String)result.keySet().iterator().next()).equals("document")) {
            Object value = result.get("document");
            return value instanceof Collection ? this.getSuccess((new Yaml()).dumpAs(value, Tag.SEQ, FlowStyle.BLOCK)) : this.getSuccess((new Yaml()).dumpAs(value, Tag.STR, FlowStyle.BLOCK));
        } else {
            String yaml = (new Yaml()).dumpAsMap(result);
            if (resolvePlaceholders) {
                yaml = EnvironmentPropertySource.resolvePlaceholders(EnvironmentPropertySource.prepareEnvironment(environment), yaml);
            }

            return this.getSuccess(yaml);
        }
    }
}

2、配置获取流程

获取流程如下:

标题

 
 

引用:

https://segmentfault.com/a/1190000007662334

猜你喜欢

转载自blog.csdn.net/qq924862077/article/details/86557502