Environment Repository

你应该在哪里存储 Config Server 的配置数据?治理此行为的策略是 EnvironmentRepository,它提供 Environment 对象。此 Environment 是来自 Spring Environment 的域的浅表副本(包括 propertySource 作为主要功能)。Environment 资源由三个变量参数化:

Where should you store the configuration data for the Config Server? The strategy that governs this behaviour is the EnvironmentRepository, serving Environment objects. This Environment is a shallow copy of the domain from the Spring Environment (including propertySources as the main feature). The Environment resources are parametrized by three variables:

  • {application}, which maps to spring.application.name on the client side.

  • {profile}, which maps to spring.profiles.active on the client (comma-separated list).

  • {label}, which is a server side feature labelling a "versioned" set of config files.

存储库实现通常像 Spring Boot 应用程序一样,从等于 {application} 参数的 spring.config.name 和等于 {profiles} 参数的 spring.profiles.active 加载配置文件。配置文件的优先级规则也与常规 Spring Boot 应用程序中的相同:活动配置文件优先于默认配置文件,并且如果存在多个配置文件,则最后一个配置文件优先(类似于向 Map 添加条目)。

Repository implementations generally behave like a Spring Boot application, loading configuration files from a spring.config.name equal to the {application} parameter, and spring.profiles.active equal to the {profiles} parameter. Precedence rules for profiles are also the same as in a regular Spring Boot application: Active profiles take precedence over defaults, and, if there are multiple profiles, the last one wins (similar to adding entries to a Map).

以下示例客户端应用程序具有此引导配置:

The following sample client application has this bootstrap configuration:

spring:
  application:
    name: foo
  profiles:
    active: dev,mysql

(与 Spring Boot 应用程序一样,这些属性也可以通过环境变量或命令行参数设置)。

(As usual with a Spring Boot application, these properties could also be set by environment variables or command line arguments).

如果存储库是基于文件的,则服务器将从 application.yml(由所有客户端共享)和 foo.yml(优先使用 foo.yml)创建一个 Environment。如果 YAML 文件内部有指向 Spring 配置文件的文档,则会以更高的优先级(按照列出的配置文件的顺序)应用这些文档。如果有特定配置文件的 YAML(或属性)文件,那么它们也会以高于默认优先级的优先级应用。更高的优先级表示在 Environment 中更早列出的 PropertySource。(这些相同的规则适用于独立 Spring Boot 应用程序。)

If the repository is file-based, the server creates an Environment from application.yml (shared between all clients) and foo.yml (with foo.yml taking precedence). If the YAML files have documents inside them that point to Spring profiles, those are applied with higher precedence (in order of the profiles listed). If there are profile-specific YAML (or properties) files, these are also applied with higher precedence than the defaults. Higher precedence translates to a PropertySource listed earlier in the Environment. (These same rules apply in a standalone Spring Boot application.)

你可以将 spring.cloud.config.server.accept-empty 设置为 false,以便在找不到应用程序时,服务器返回 HTTP 404 状态。默认情况下,此标志设置为 true

You can set spring.cloud.config.server.accept-empty to false so that Server would return a HTTP 404 status, if the application is not found. By default, this flag is set to true.

您不能将 spring.main.* 属性放置在远程 EnvironmentRepository 中。这些属性用作应用程序初始化的一部分。

You cannot place spring.main.* properties in a remote EnvironmentRepository. These properties are used as part of the application initialization.