Search code examples
springspring-cloudspring-cloud-config

Spring Cloud Config Server - Configure the server to get common properties from one repository and application properties form a different repository


I have the following requirements:

  • There is a git repo (commonRepo) with an application.yml file that contains properties which are common to all applications.
  • There is another git repo (applicationRepo) with the applications' config files: app1.yml, app2.yml, etc.

The point of this is to have all common properties centralized in a repo with specific write permissions.

I need to configure Spring Cloud Config Server to read from both repos, with the following priority:

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

Is it possible to configure it to do that?


Solution

  • This was my initial config:

    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
    

    There is a default repo in case there's no environment match and three repos depending on a pattern.

    The idea is have another base repo (with the lowest priority) that would always be loaded and also depends on the environment. This base repo contains an application.yml with shared properties managed by a different team.

    Apparently there's no way of doing it via yml properties so I had to define the beans manually, this is the code necessary to do it:

        @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;
        }