Spring 云配置服务器 - 配置服务器以从一个存储库获取公共属性,并从另一个存储库获取应用程序属性
Spring Cloud Config Server - Configure the server to get common properties from one repository and application properties form a different repository
我有以下要求:
- 有一个 git 存储库 (commonRepo) 和一个 application.yml 文件,其中包含所有应用程序共有的属性。
- 还有另一个 git 存储库 (applicationRepo),其中包含应用程序的配置文件:app1.yml、app2.yml 等
这样做的目的是将所有公共属性集中在一个具有特定写入权限的存储库中。
我需要配置 Spring Cloud Config Server 以从两个存储库读取,优先级如下:
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;
}
我有以下要求:
- 有一个 git 存储库 (commonRepo) 和一个 application.yml 文件,其中包含所有应用程序共有的属性。
- 还有另一个 git 存储库 (applicationRepo),其中包含应用程序的配置文件:app1.yml、app2.yml 等
这样做的目的是将所有公共属性集中在一个具有特定写入权限的存储库中。
我需要配置 Spring Cloud Config Server 以从两个存储库读取,优先级如下:
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;
}