简体   繁体   English

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

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

I'm trying to migrate our stable of app servers to get their configuration from a Spring Cloud Config server.我正在尝试迁移我们稳定的应用服务器以从 Spring Cloud Config 服务器获取它们的配置。 Each app has a {my-app}.yml file on the config server and we can use profiles (either in files named {my-app}-{profile}.yml or using multi-profile YAML documents) to have different configuration per environment for each app, and we can even include one profile in another using spring.profiles.include to provide some sort of inheritance - so far, so good.每个应用{my-app}.yml在配置服务器上都有一个{my-app}.yml文件,我们可以使用配置文件(在名为{my-app}-{profile}.yml的文件中或使用多配置文件 YAML 文档)来为每个{my-app}-{profile}.yml提供不同的配置每个应用程序的环境,我们甚至可以使用spring.profiles.include将一个配置文件包含在另一个配置文件中以提供某种继承 - 到目前为止, spring.profiles.include都很好。

However, we can only include profiles from the same app in each other and we have several apps configured from the same config server which share a lot of config per environment - for instance they almost all use the same DataSource config to connect to the same database and likewise for messaging, cache and so on.但是,我们只能在彼此中包含来自同一个应用程序的配置文件并且我们从同一个配置服务器配置了多个应用程序,它们在每个环境中共享大量配置 - 例如,它们几乎都使用相同的 DataSource 配置来连接到同一个数据库同样用于消息传递、缓存等。 That's a lot of duplicated config and a lot of places it needs to be changed - precisely what Spring Cloud Config is supposed to avoid!这是很多重复的配置和很多需要更改的地方 - 正是 Spring Cloud Config 应该避免的!

Is there a way to "include" (via profiles or otherwise!) shared config properties across apps in Spring Cloud Config server?有没有办法“包含”(通过配置文件或其他方式!)在 Spring Cloud Config 服务器中跨应用程序共享配置属性?

Update更新

In addition to the correct answer by @vladsfl below, beware if you're using the native profile on the config server to serve config from the filesystem or classpath instead of a git repo, the config server will use application.yml and its profile variants for itself but refuse to serve them out to other apps.除了下面@vladsfl 的正确答案之外,请注意,如果您使用配置服务器上的本机配置文件从文件系统或类路径而不是 git 存储库提供配置,配置服务器将使用 application.yml 及其配置文件变体但拒绝将它们提供给其他应用程序。 The solution is to use spring.cloud.config.server.native.searchLocations to pull the served config from a different location.解决方案是使用spring.cloud.config.server.native.searchLocations从不同的位置拉取服务的配置。

Yes.是的。 You can have application.yml or application-<profile>.yml on your config server and now every application that is using this config server will inherit all settings in application.yml .您可以在配置服务器上拥有application.ymlapplication-<profile>.yml ,现在每个使用此配置服务器的application.yml都将继承application.yml所有设置。 Every application that runs in specific profile will inherit settings from application-<profile>.yml .在特定配置文件中运行的每个应用程序都将从application-<profile>.yml继承设置。

Probably it's too late already, but in case someone else is struggling with the same issue, the final solution is as follows:可能已经太晚了,但如果其他人也遇到同样的问题,最终的解决方案如下:

You can create as many yml files under config-server classpath as you wish.您可以根据需要在 config-server 类路径下创建任意数量的 yml 文件。 Even if it is in native profile selected, there will be provided to client applications.即使它在本机配置文件中选择,也会提供给客户端应用程序。 The only thing not mentioned before is, you should tell the client application to read those settings files as well.之前唯一没有提到的是,您应该告诉客户端应用程序也读取这些设置文件。

Here is a working example:这是一个工作示例:

Config server file structure:配置服务器文件结构:

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

Client application bootstrap.yml file:客户端应用程序 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

The key is name: ${spring.application.name}, settings which tells the config client to load the following settings from the config server:关键是name: ${spring.application.name}, settings告诉配置客户端从配置服务器加载以下设置:

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

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

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