Spring Cloud Config Server - 配置服务器以从一个存储库获取公共属性,并将应用程序属性从另一个存储库中获取

时间:2017-07-18 11:58:31

标签: spring spring-cloud spring-cloud-config

我有以下要求:

  • 有一个带有application.yml文件的git repo(commonRepo),该文件包含所有应用程序通用的属性。
  • 还有另一个git repo(applicationRepo)和应用程序'配置文件:app1.yml,app2.yml等。

重点是将所有公共属性集中在具有特定写入权限的仓库中。

我需要配置Spring Cloud Config Server以从两个repos读取,具有以下优先级:

  

applicationRepo:appX.yml> applicationRepo:application.yml>   commonRepo:application.yml

是否可以将其配置为执行此操作?

1 个答案:

答案 0 :(得分:1)

这是我的初始配置:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: file:///home/codependent/git/scc-dev
          repos:
            dev:
              pattern: '*/dev'
              uri: file:///home/codependent/git/scc-dev
            pre:
              pattern: '*/pre'
              uri: file:///home/codependent/git/scc-pre
            pro:
              pattern: '*/pro'
              uri: file:///home/codependent/git/scc-pro

如果没有环境匹配,则有一个默认仓库,根据模式,有三个仓库。

这个想法是有另一个基本仓库(具有最低优先级),它总是被加载并且还取决于环境。此基本仓库包含一个application.yml,其共享属性由另一个团队管理。

显然没有办法通过yml属性这样做,所以我必须手动定义bean,这是执行它所需的代码:

    @Bean
    public EnvironmentRepository baseEnvironmentRepository(){
        ConfigurableEnvironment ce = new StandardServletEnvironment();
        PatternMatchingJGitEnvironmentRepository devRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-ops-dev");
        devRepo.setEnvironment(ce);
        devRepo.setForcePull(false);
        devRepo.setPattern(new String[]{ "*/dev" });

        PatternMatchingJGitEnvironmentRepository preRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-ops-pre");
        preRepo.setEnvironment(ce);
        preRepo.setForcePull(false);
        preRepo.setPattern(new String[]{ "*/pre" });

        PatternMatchingJGitEnvironmentRepository proRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-ops-pro");
        proRepo.setEnvironment(ce);
        proRepo.setForcePull(false);
        proRepo.setPattern(new String[]{ "*/pro" });

        Map<String, PatternMatchingJGitEnvironmentRepository> map = new HashMap<>();
        map.put("dev", devRepo);
        map.put("pre", preRepo);
        map.put("pro", proRepo);

        MultipleJGitEnvironmentRepository multiple = new MultipleJGitEnvironmentRepository(new StandardServletEnvironment());
        multiple.setUri("file:///home/codependent/git/scc-ops-dev");
        multiple.setRepos(map);
        multiple.setOrder(Ordered.LOWEST_PRECEDENCE);
        return multiple;
    }

    @Bean
    public EnvironmentRepository environmentRepository(){
        ConfigurableEnvironment ce = new StandardServletEnvironment();
        PatternMatchingJGitEnvironmentRepository devRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-dev");
        devRepo.setEnvironment(ce);
        devRepo.setForcePull(false);
        devRepo.setPattern(new String[]{ "*/dev" });

        PatternMatchingJGitEnvironmentRepository preRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-pre");
        preRepo.setEnvironment(ce);
        preRepo.setForcePull(false);
        preRepo.setPattern(new String[]{ "*/pre" });

        PatternMatchingJGitEnvironmentRepository proRepo = new PatternMatchingJGitEnvironmentRepository("file:///home/codependent/git/scc-pro");
        proRepo.setEnvironment(ce);
        proRepo.setForcePull(false);
        proRepo.setPattern(new String[]{ "*/pro" });

        Map<String, PatternMatchingJGitEnvironmentRepository> map = new HashMap<>();
        map.put("dev", devRepo);
        map.put("pre", preRepo);
        map.put("pro", proRepo);

        MultipleJGitEnvironmentRepository multiple = new MultipleJGitEnvironmentRepository(new StandardServletEnvironment());
        multiple.setUri("file:///home/codependent/git/scc-dev");
        multiple.setRepos(map);
        multiple.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return multiple;
    }