繁体   English   中英

在 Spring Cloud Config 服务器中包含多个应用程序的通用配置

[英]Include common config for multiple apps in Spring Cloud Config server

我正在尝试迁移我们稳定的应用服务器以从 Spring Cloud Config 服务器获取它们的配置。 每个应用{my-app}.yml在配置服务器上都有一个{my-app}.yml文件,我们可以使用配置文件(在名为{my-app}-{profile}.yml的文件中或使用多配置文件 YAML 文档)来为每个{my-app}-{profile}.yml提供不同的配置每个应用程序的环境,我们甚至可以使用spring.profiles.include将一个配置文件包含在另一个配置文件中以提供某种继承 - 到目前为止, spring.profiles.include都很好。

但是,我们只能在彼此中包含来自同一个应用程序的配置文件并且我们从同一个配置服务器配置了多个应用程序,它们在每个环境中共享大量配置 - 例如,它们几乎都使用相同的 DataSource 配置来连接到同一个数据库同样用于消息传递、缓存等。 这是很多重复的配置和很多需要更改的地方 - 正是 Spring Cloud Config 应该避免的!

有没有办法“包含”(通过配置文件或其他方式!)在 Spring Cloud Config 服务器中跨应用程序共享配置属性?

更新

除了下面@vladsfl 的正确答案之外,请注意,如果您使用配置服务器上的本机配置文件从文件系统或类路径而不是 git 存储库提供配置,配置服务器将使用 application.yml 及其配置文件变体但拒绝将它们提供给其他应用程序。 解决方案是使用spring.cloud.config.server.native.searchLocations从不同的位置拉取服务的配置。

是的。 您可以在配置服务器上拥有application.ymlapplication-<profile>.yml ,现在每个使用此配置服务器的application.yml都将继承application.yml所有设置。 在特定配置文件中运行的每个应用程序都将从application-<profile>.yml继承设置。

可能已经太晚了,但如果其他人也遇到同样的问题,最终的解决方案如下:

您可以根据需要在 config-server 类路径下创建任意数量的 yml 文件。 即使它在本机配置文件中选择,也会提供给客户端应用程序。 之前唯一没有提到的是,您应该告诉客户端应用程序也读取这些设置文件。

这是一个工作示例:

配置服务器文件结构:

resources
|-config
   |-auth-service.yml - service specific configuration file
|-application.yml - config server settings
|-settings.yml - general settings file, planed to be loaded in every service

客户端应用程序 bootstrap.yml 文件:

spring:
application:
  name: auth-service
cloud:
  config:
    username: "config-user"
    password: "config-password-1234"
    uri: "http://config-service:8888"
    name: ${spring.application.name}, settings

关键是name: ${spring.application.name}, settings告诉配置客户端从配置服务器加载以下设置:

  • ${spring.application.name}将加载config/auth-service.yml
  • settings将加载settings.yml中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM