Sharing Configuration With All Applications

根据你采用的方法,在所有应用程序之间共享配置会有所不同,如下面的主题中所述:

Sharing configuration between all applications varies according to which approach you take, as described in the following topics:

File Based Repositories

对于基于文件的存储库(git、svn 和 native),文件名中包含 application*application.propertiesapplication.ymlapplication-*.properties 等)的资源在所有客户端应用程序之间共享。你可以使用这些文件名的资源来配置全局默认值,并根据需要让应用程序特定的文件来覆盖它们。

With file-based (git, svn, and native) repositories, resources with file names in application* (application.properties, application.yml, application-*.properties, and so on) are shared between all client applications. You can use resources with these file names to configure global defaults and have them be overridden by application-specific files as necessary.

property overrides功能还可以用于设置全局默认值,且允许在本地覆盖占位符应用程序。

The property overrides feature can also be used for setting global defaults, with placeholders applications allowed to override them locally.

使用 “native” 配置文件(本地文件系统后端),您应该使用不属于服务器自身配置的明确搜索位置。否则,默认搜索位置中的 application* 资源将被删除,因为它们是服务器的一部分。

With the “native” profile (a local file system backend) , you should use an explicit search location that is not part of the server’s own configuration. Otherwise, the application* resources in the default search locations get removed because they are part of the server.

Vault Server

在将 Vault 用作后端时,你可以通过将配置放在 secret/application 中来与所有应用程序共享配置。例如,如果你运行以下 Vault 命令,则所有使用 config server 的应用程序都将可以使用属性 foobaz

When using Vault as a backend, you can share configuration with all applications by placing configuration in secret/application. For example, if you run the following Vault command, all applications using the config server will have the properties foo and baz available to them:

$ vault write secret/application foo=bar baz=bam

CredHub Server

在将 CredHub 用作后端时,你可以通过将配置放在 /application/ 中或将其放在应用程序的 default 配置文件中,来与所有应用程序共享配置。例如,如果你运行以下 CredHub 命令,则所有使用 config server 的应用程序都将可以使用属性 shared.color1shared.color2

When using CredHub as a backend, you can share configuration with all applications by placing configuration in /application/ or by placing it in the default profile for the application. For example, if you run the following CredHub command, all applications using the config server will have the properties shared.color1 and shared.color2 available to them:

credhub set --name "/application/profile/master/shared" --type=json
value: {"shared.color1": "blue", "shared.color": "red"}
credhub set --name "/my-app/default/master/more-shared" --type=json
value: {"shared.word1": "hello", "shared.word2": "world"}

JDBC Environment Repository

若要使用 JDBC 后端共享配置,请在你为所有客户端共享的条目中将记录插入到你的数据库中,并在应用程序列中将其值设为“application”。然后,应用程序特定属性可以覆盖这些共享配置,从而提供对应用程序环境的灵活性和控制。

To share configurations using the JDBC backend, insert records into your database with ’application'` as the value in the application column for entries intended to be shared across all clients. Application-specific properties can then override these shared configurations, providing flexibility and control over your application environments.

INSERT INTO PROPERTIES (APPLICATION, PROFILE, LABEL, KEY, VALUE)
VALUES ('application', 'default', 'master', 'a.b.c', 'shared-value');
INSERT INTO PROPERTIES (APPLICATION, PROFILE, LABEL, KEY, VALUE)
VALUES ('myapp', 'prod', 'master', 'd.e.f', 'specific-value');

请参考 JdbcEnvironmentRepository 实现和相关测试,了解使用 JDBC 仓库进行设置和配置管理的详细示例。

Refer to the JdbcEnvironmentRepository implementation and associated tests for detailed examples on setup and configuration management using the JDBC repository.