Spring Cloud Config 客户端

Spring Boot应用程序可以立即利用Spring Config Server(或应用程序开发人员提供的其他外部属性源)。 它还选择了一些与以下内容相关的其他有用功能Environment更改事件。spring-doc.cadn.net.cn

Spring Boot 配置数据导入

Spring Boot 2.4 引入了一种通过spring.config.import财产。这现在是绑定到 Config Server 的默认方式。spring-doc.cadn.net.cn

要选择性地连接到配置服务器,请在 application.properties 中设置以下内容:spring-doc.cadn.net.cn

应用程序属性
spring.config.import=optional:configserver:

这将连接到默认位置“http://localhost:8888”的配置服务器。删除optional:前缀将导致 Config Client 在无法连接到 Config Server 时失败。要更改配置服务器的位置,请将spring.cloud.config.uri或将 url 添加到spring.config.import语句,例如,spring.config.import=optional:configserver:http://myhost:8888.import 属性中的位置优先于 uri 属性。spring-doc.cadn.net.cn

一个bootstrap文件(属性或 yaml)对于通过导入的 Spring Boot Config Data 方法不需要spring.config.import.

配置第一个引导程序

要使用旧版引导方式连接到 Config Server,必须通过属性或spring-cloud-starter-bootstrap起动机。该属性是spring.cloud.bootstrap.enabled=true.它必须设置为系统属性或环境变量。 启用引导后,任何在类路径上具有 Spring Cloud Config Client 的应用程序都将连接到 Config Server,如下所示: 当配置客户端启动时,它会绑定到配置服务器(通过spring.cloud.config.uribootstrap 配置属性)并初始化 SpringEnvironment使用远程属性源。spring-doc.cadn.net.cn

此行为的最终结果是,所有想要使用 Config Server 的客户端应用程序都需要一个bootstrap.yml(或环境变量),服务器地址设置为spring.cloud.config.uri(默认为“http://localhost:8888”)。spring-doc.cadn.net.cn

发现优先查找

除非您使用 config first bootstrap,否则您需要有一个spring.config.import属性,并在配置属性中使用optional:前缀。 例如spring.config.import=optional:configserver:.

如果您使用DiscoveryClient实现,例如 Spring Cloud Netflix 和 Eureka Service Discovery 或 Spring Cloud Consul,您可以让 Config Server 向 Discovery Service 注册。spring-doc.cadn.net.cn

如果您更喜欢使用DiscoveryClient要找到配置服务器,您可以通过将spring.cloud.config.discovery.enabled=true(默认值为false). 例如,使用 Spring Cloud Netflix,您需要定义 Eureka 服务器地址(例如,在eureka.client.serviceUrl.defaultZone). 使用此选项的价格是在启动时进行额外的网络往返,以查找服务注册。 好处是,只要发现服务是固定点,配置服务器就可以更改其坐标。 默认服务 ID 为configserver,但您可以通过在客户端上设置spring.cloud.config.discovery.serviceId(在服务器上,以服务的常用方式,例如通过将spring.application.name).spring-doc.cadn.net.cn

发现客户端实现都支持某种元数据映射(例如,我们有eureka.instance.metadataMap尤里卡)。 可能需要在其服务注册元数据中配置服务器的一些附加属性,以便客户端可以正确连接。 如果配置服务器使用 HTTP Basic 进行保护,则可以将凭据配置为userpassword. 此外,如果配置服务器具有上下文路径,您可以将configPath. 例如,以下 YAML 文件适用于作为 Eureka 客户端的 Config Server:spring-doc.cadn.net.cn

eureka:
  instance:
    ...
    metadataMap:
      user: osufhalskjrtl
      password: lviuhlszvaorhvlo5847
      configPath: /config

使用 Eureka 和 WebClient 的首次发现引导

如果您使用 EurekaDiscoveryClient来自 Spring Cloud Netflix 并且也想使用WebClient而不是Jersey或RestTemplate, 您需要包括WebClient在你的类路径上以及eureka.client.webclient.enabled=true.spring-doc.cadn.net.cn

配置客户端快速故障

在某些情况下,如果服务无法连接到配置服务器,则可能需要启动服务失败。 如果这是所需的行为,请设置引导配置属性spring.cloud.config.fail-fast=true使客户端因异常而停止。spring-doc.cadn.net.cn

要使用spring.config.import,只需省略optional:前缀。

配置客户端重试

如果您预计应用程序启动时配置服务器可能偶尔不可用,则可以让它在失败后继续尝试。 首先,您需要设置spring.cloud.config.fail-fast=true. 然后你需要添加spring-retryspring-boot-starter-aop到你的类路径。 默认行为是重试六次,初始回退间隔为 1000 毫秒,后续回退的指数乘数为 1.1。 您可以通过设置spring.cloud.config.retry.*配置属性。spring-doc.cadn.net.cn

要完全控制重试行为并使用旧版引导程序,请添加@Bean类型RetryOperationsInterceptorID 为configServerRetryInterceptor. Spring Retry 有一个RetryInterceptorBuilder这支持创建一个。

使用 spring.config.import 重试配置客户端

重试适用于 Spring Bootspring.config.import语句和 normal 属性有效。但是,如果 import 语句位于配置文件中,例如application-prod.properties,则需要以不同的方式配置重试。配置需要作为 url 参数放置在 import 语句上。spring-doc.cadn.net.cn

应用程序生产属性
spring.config.import=configserver:http://configserver.example.com?fail-fast=true&max-attempts=10&max-interval=1500&multiplier=1.2&initial-interval=1100"

这设置spring.cloud.config.fail-fast=true(注意上面缺少的前缀)和所有可用的spring.cloud.config.retry.*配置属性。spring-doc.cadn.net.cn

查找远程配置资源

配置服务提供来自/{application}/{profile}/{label},其中客户端应用中的默认绑定如下所示:spring-doc.cadn.net.cn

设置属性时${spring.application.name}不要在应用名称前加上保留字application-以防止在解析正确的属性源时出现问题。

您可以通过设置spring.cloud.config.*(其中 是*name,profilelabel). 这label对于回滚到以前版本的配置很有用。 使用默认的 Config Server 实现,它可以是 git 标签、分支名称或提交 ID。 标签也可以作为逗号分隔的列表提供。 在这种情况下,列表中的项目将逐个尝试,直到一个成功。 在处理功能分支时,此行为非常有用。 例如,您可能希望将配置标签与分支对齐,但将其设为可选(在这种情况下,请使用spring.cloud.config.label=myfeature,develop).spring-doc.cadn.net.cn

为配置服务器指定多个 URL

为了确保在部署了多个 Config Server 实例并预计一个或多个实例不时不可用时实现高可用性,您可以指定多个 URL(作为spring.cloud.config.uri属性)或让您的所有实例都像 Eureka 一样在服务注册表中注册(如果使用 Discovery-First Bootstrap 模式)。请注意,只有在配置服务器未运行(即应用程序退出时)或发生连接超时时,这样做才能确保高可用性。例如,如果配置服务器返回 500(内部服务器错误)响应,或者配置客户端从 Config 服务器收到 401(由于凭据错误或其他原因),则配置客户端不会尝试从其他 URL 获取属性。此类错误表示用户问题,而不是可用性问题。spring-doc.cadn.net.cn

如果您在 Config Server 上使用 HTTP 基本安全性,则目前只有在spring.cloud.config.uri财产。 如果您使用任何其他类型的安全机制,则(当前)无法支持每个配置服务器的身份验证和授权。spring-doc.cadn.net.cn

配置超时

如果要配置超时阈值:spring-doc.cadn.net.cn

安全

如果您在服务器上使用 HTTP Basic 安全性,则客户端需要知道密码(如果密码不是默认密码,则需要知道用户名)。 您可以通过配置服务器 URI 或通过单独的用户名和密码属性指定用户名和密码,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
     uri: https://user:[email protected]

以下示例显示了传递相同信息的另一种方法:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
     uri: https://myconfig.mycompany.com
     username: user
     password: secret

spring.cloud.config.passwordspring.cloud.config.username值将覆盖 URI 中提供的任何内容。spring-doc.cadn.net.cn

如果您在 Cloud Foundry 上部署应用程序,则提供密码的最佳方式是通过服务凭据(例如在 URI 中,因为它不需要位于配置文件中)。 以下示例适用于本地,适用于名为 Cloud Foundry 的用户提供的服务configserver:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
     uri: ${vcap.services.configserver.credentials.uri:http://user:password@localhost:8888}

如果配置服务器需要客户端 TLS 证书,您可以通过属性配置客户端 TLS 证书和信任存储,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      uri: https://myconfig.myconfig.com
      tls:
        enabled: true
        key-store: <path-of-key-store>
        key-store-type: PKCS12
        key-store-password: <key-store-password>
        key-password: <key-password>
        trust-store: <path-of-trust-store>
        trust-store-type: PKCS12
        trust-store-password: <trust-store-password>

spring.cloud.config.tls.enabled需要为 true 才能启用配置客户端 TLS。什么时候spring.cloud.config.tls.trust-store省略,则使用 JVM 默认信任存储。的默认值spring.cloud.config.tls.key-store-typespring.cloud.config.tls.trust-store-type是 PKCS12。如果省略密码属性,则假定为空密码。spring-doc.cadn.net.cn

如果您使用其他形式的安全性,则可能需要提供一个RestTemplateConfigServicePropertySourceLocator(例如,通过在引导上下文中抓取它并注入它)。spring-doc.cadn.net.cn

健康指标

Config Client 提供了一个 Spring Boot Health Indicator,该指示器尝试从 Config Server 加载配置。 可以通过设置health.config.enabled=false. 出于性能原因,响应也会被缓存。 默认缓存生存时间为 5 分钟。 要更改该值,请将health.config.time-to-live属性(以毫秒为单位)。spring-doc.cadn.net.cn

提供自定义 RestTemplate

在某些情况下,您可能需要自定义从客户端向配置服务器发出的请求。 通常,这样做涉及将特殊Authorization标头来验证对服务器的请求。 要提供自定义RestTemplate:spring-doc.cadn.net.cn

  1. 创建一个新的配置 Bean,实现为PropertySourceLocator,如以下示例所示:spring-doc.cadn.net.cn

CustomConfigServiceBootstrapConfiguration.java
@Configuration
public class CustomConfigServiceBootstrapConfiguration {
    @Bean
    public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
        ConfigClientProperties clientProperties = configClientProperties();
       ConfigServicePropertySourceLocator configServicePropertySourceLocator =  new ConfigServicePropertySourceLocator(clientProperties);
        configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
        return configServicePropertySourceLocator;
    }
}
对于添加的简化方法Authorizationheaders,则spring.cloud.config.headers.*属性可以代替。
  1. resources/META-INF,创建一个名为spring.factories并指定自定义配置,如以下示例所示:spring-doc.cadn.net.cn

spring.工厂
org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration

将 Vault 用作配置服务器的后端时,客户端需要为服务器提供Tokens以从 Vault 检索值。 可以通过将spring.cloud.config.tokenbootstrap.yml,如以下示例所示:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      token: YourVaultToken

Vault 中的嵌套密钥

Vault 支持将键嵌套在存储在 Vault 中的值中,如以下示例所示:spring-doc.cadn.net.cn

echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -spring-doc.cadn.net.cn

此命令将 JSON 对象写入您的 Vault。要在 Spring 中访问这些值,您可以使用传统的点(.) 注释,如以下示例所示spring-doc.cadn.net.cn

@Value("${appA.secret}")
String name = "World";

前面的代码将设置namevariable 设置为appAsecret.spring-doc.cadn.net.cn