Distributed Configuration with Zookeeper

Zookeeper 提供了一个 hierarchical namespace,可让客户端存储任意数据,例如配置数据。Spring Cloud ZookeeperConfig 是 Config Server and Client 的替代方案。配置在特殊的 “bootstrap” 阶段加载到 Spring 环境中。默认情况下,配置存储在 /config 名称空间中。会根据应用程序的名称和活动配置文件创建多个 PropertySource 实例,以模拟 Spring Cloud Config 解析属性的顺序。例如,一个名为 testApp 并具有 dev 配置文件的应用程序为此创建了以下属性源:

Zookeeper provides a hierarchical namespace that lets clients store arbitrary data, such as configuration data. Spring Cloud Zookeeper Config is an alternative to the Config Server and Client. Configuration is loaded into the Spring Environment during the special “bootstrap” phase. Configuration is stored in the /config namespace by default. Multiple PropertySource instances are created, based on the application’s name and the active profiles, to mimic the Spring Cloud Config order of resolving properties. For example, an application with a name of testApp and with the dev profile has the following property sources created for it:

  • config/testApp,dev

  • config/testApp

  • config/application,dev

  • config/application

最具体的属性源位于顶部,最不具体的属性源位于底部。config/application 名称空间中的属性适用于所有将 zookeeper 用于配置的应用程序。config/testApp 名称空间中的属性仅对名为 testApp 的服务实例可用。

The most specific property source is at the top, with the least specific at the bottom. Properties in the config/application namespace apply to all applications that use zookeeper for configuration. Properties in the config/testApp namespace are available only to the instances of the service named testApp.

当前,配置在应用程序启动时进行读取。将 HTTP POST 请求发送到 /refresh 会导致配置重新加载。还可观看配置名称空间(Zookeeper 支持)。

Configuration is currently read on startup of the application. Sending a HTTP POST request to /refresh causes the configuration to be reloaded. Watching the configuration namespace (which Zookeeper supports) is also available.

Activating

依赖 org.springframework.cloud:spring-cloud-starter-zookeeper-config 可以启用自动配置,以设置 Spring Cloud Zookeeper Config。

Including a dependency on org.springframework.cloud:spring-cloud-starter-zookeeper-config enables autoconfiguration that sets up Spring Cloud Zookeeper Config.

在使用 Zookeeper 版本 3.4 时,您需要按照 here 中的说明更改包含依赖项的方式。

When working with version 3.4 of Zookeeper you need to change the way you include the dependency as described here.

Spring Boot Config Data Import

Spring Boot 2.4 引入了通过 spring.config.import 属性导入配置数据的新方法。这现在是从 Zookeeper 获取配置的默认方法。

Spring Boot 2.4 introduced a new way to import configuration data via the spring.config.import property. This is now the default way to get configuration from Zookeeper.

要选择性地连接到 Zookeeper 以进行配置,请在 application.properties 中设置以下内容:

To optionally connect to Zookeeper for configuration set the following in application.properties:

application.properties
spring.config.import=optional:zookeeper:

这会连接到默认位置为“localhost:2181”的 Zookeeper。移除 optional: 前缀会导致 Zookeeper Config 在无法连接到 Zookeeper 时失败。要更改 Zookeeper Config 的连接属性,请设置 spring.cloud.zookeeper.connect-string 或将连接字符串添加到 spring.config.import 语句,如 spring.config.import=optional:zookeeper:myhost:2818。导入属性中的位置优先于 connect-string 属性。

This will connect to Zookeeper at the default location of "localhost:2181". Removing the optional: prefix will cause Zookeeper Config to fail if it is unable to connect to Zookeeper. To change the connection properties of Zookeeper Config either set spring.cloud.zookeeper.connect-string or add the connect string to the spring.config.import statement such as, spring.config.import=optional:zookeeper:myhost:2818. The location in the import property has precedence over the connect-string property.

Zookeeper Config 会尝试根据 spring.cloud.zookeeper.config.name(默认为 spring.application.name 属性的值)和 spring.cloud.zookeeper.config.default-context (默认为 application)从四个自动上下文加载值。如果您希望指定上下文,而不是使用计算得到的上下文,您可以在 spring.config.import 语句中添加该信息。

Zookeeper Config will try to load values from four automatic contexts based on spring.cloud.zookeeper.config.name (which defaults to the value of the spring.application.name property) and spring.cloud.zookeeper.config.default-context (which defaults to application). If you want to specify the contexts rather than using the computed ones, you can add that information to the spring.config.import statement.

application.properties
spring.config.import=optional:zookeeper:myhost:2181/contextone;/context/two

这样会仅从 /contextone/context/two 加载配置(可选)。

This will optionally load configuration only from /contextone and /context/two.

对于通过 spring.config.import 导入 Spring Boot 配置数据的方法,需要一个 bootstrap 文件(属性或 yaml)。

A bootstrap file (properties or yaml) is not needed for the Spring Boot Config Data method of import via spring.config.import.

Customizing

可以通过设置以下属性来自定义 Zookeeper Config:

Zookeeper Config may be customized by setting the following properties:

spring:
  cloud:
    zookeeper:
      config:
        enabled: true
        root: configuration
        defaultContext: apps
        profileSeparator: '::'
  • enabled: Setting this value to false disables Zookeeper Config.

  • root: Sets the base namespace for configuration values.

  • defaultContext: Sets the name used by all applications.

  • profileSeparator: Sets the value of the separator used to separate the profile name in property sources with profiles.

如果你已经设置了 spring.cloud.bootstrap.enabled=truespring.config.use-legacy-processing=true,或者包含了 spring-cloud-starter-bootstrap,那么上述值需要放在 bootstrap.yml 中,而不是 application.yml

If you have set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true, or included spring-cloud-starter-bootstrap, then the above values will need to be placed in bootstrap.yml instead of application.yml.

Access Control Lists (ACLs)

您可以通过调用 CuratorFramework bean 的 addAuthInfo 方法来为 Zookeeper ACL 添加身份验证信息。实现此方法的一种方法是提供您自己的 CuratorFramework bean,如下例所示:

You can add authentication information for Zookeeper ACLs by calling the addAuthInfo method of a CuratorFramework bean. One way to accomplish this is to provide your own CuratorFramework bean, as shown in the following example:

@BoostrapConfiguration
public class CustomCuratorFrameworkConfig {

  @Bean
  public CuratorFramework curatorFramework() {
    CuratorFramework curator = new CuratorFramework();
    curator.addAuthInfo("digest", "user:password".getBytes());
    return curator;
  }

}

请查阅 the ZookeeperAutoConfiguration class 以了解 CuratorFramework bean 的默认配置。

Consult the ZookeeperAutoConfiguration class to see how the CuratorFramework bean’s default configuration.

或者,您可以从依赖于现有 CuratorFramework bean 的类添加凭据,如下例所示:

Alternatively, you can add your credentials from a class that depends on the existing CuratorFramework bean, as shown in the following example:

@BoostrapConfiguration
public class DefaultCuratorFrameworkConfig {

  public ZookeeperConfig(CuratorFramework curator) {
    curator.addAuthInfo("digest", "user:password".getBytes());
  }

}

此 bean 的创建必须在引导阶段进行。您可以通过使用 @BootstrapConfiguration 为它们添加注解,并将它们包含在以逗号分隔的列表中(将其作为 resources/META-INF/spring.factories 文件中 org.springframework.cloud.bootstrap.BootstrapConfiguration 属性的值设置)来注册在此阶段运行的配置类,如下例所示:

The creation of this bean must occur during the boostrapping phase. You can register configuration classes to run during this phase by annotating them with @BootstrapConfiguration and including them in a comma-separated list that you set as the value of the org.springframework.cloud.bootstrap.BootstrapConfiguration property in the resources/META-INF/spring.factories file, as shown in the following example:

resources/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.project.CustomCuratorFrameworkConfig,\
my.project.DefaultCuratorFrameworkConfig