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

我有以下要求:

>有一个带有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

是否可以配置它来做到这一点?

最佳答案 这是我的初始配置:

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;
    }
点赞