10. Spring Cloud ConfigClient 端
Spring Boot 应用程序可以立即利用 Spring Config Server(或应用程序开发人员提供的其他外部属性源)。它还选择了与Environment
更改事件相关的其他有用功能。
10.1 Config First Bootstrap
在 Classpath 上具有 Spring Cloud Config Client 的任何应用程序的默认行为如下:当配置 Client 端启动时,它将绑定到 Config Server(通过spring.cloud.config.uri
bootstrap 配置属性),并使用远程属性源初始化 Spring Environment
。
此行为的最终结果是,所有要使用 Config Server 的 Client 端应用程序都需要一个bootstrap.yml
(或环境变量),其服务器地址设置为spring.cloud.config.uri
(默认为“ http:// localhost:8888”)。
10.2 Discovery First Bootstrap
如果您使用DiscoveryClient
实现,例如 Spring Cloud Netflix 和 Eureka Service Discovery 或 Spring Cloud Consul,则可以让 Config Server 向 Discovery Service 注册。但是,在默认的“配置优先”模式下,Client 端无法利用注册。
如果您更喜欢使用DiscoveryClient
来定位 Config Server,则可以通过设置spring.cloud.config.discovery.enabled=true
(默认值为false
)来完成。这样做的最终结果是,所有 Client 端应用程序都需要具有适当发现配置的bootstrap.yml
(或环境变量)。例如,对于 Spring Cloud Netflix,您需要定义 Eureka 服务器地址(例如,在eureka.client.serviceUrl.defaultZone
中)。使用此选项的价格是启动时需要进行额外的网络往返,以查找服务注册。好处是,只要发现服务是固定点,配置服务器就可以更改其坐标。默认服务 ID 是configserver
,但是您可以在 Client 端上通过设置spring.cloud.config.discovery.serviceId
来更改它(在服务器上,以一种通常的服务方式,例如通过设置spring.application.name
)来更改它。
发现 Client 端实现均支持某种元数据 Map(例如,对于 Eureka,我们有eureka.instance.metadataMap
)。 Config Server 的某些其他属性可能需要在其服务注册元数据中进行配置,以便 Client 端可以正确连接。如果 Config Server 受 HTTP Basic 保护,则可以将凭据配置为user
和password
。另外,如果 Config Server 具有上下文路径,则可以设置configPath
。例如,以下 YAML 文件适用于作为 EurekaClient 端的 Config Server:
bootstrap.yml.
eureka:
instance:
...
metadataMap:
user: osufhalskjrtl
password: lviuhlszvaorhvlo5847
configPath: /config
10.3 配置 Client 端快速失败
在某些情况下,如果服务无法连接到 Config Server,您可能希望启动失败。如果这是所需的行为,请设置引导程序配置属性spring.cloud.config.fail-fast=true
,以使 Client 端因 Exception 而暂停。
10.4 Config Client 重试
如果您希望配置服务器在应用程序启动时偶尔不可用,则可以使其在失败后 continue 尝试。首先,您需要设置spring.cloud.config.fail-fast=true
。然后,您需要将spring-retry
和spring-boot-starter-aop
添加到您的 Classpath 中。默认行为是重试六次,初始回退间隔为 1000ms,随后的回退的指数乘数为 1.1. 您可以通过设置spring.cloud.config.retry.*
配置属性来配置这些属性(和其他属性)。
Tip
要完全控制重试行为,请添加 ID 为configServerRetryInterceptor
的RetryOperationsInterceptor
类型的@Bean
。 Spring Retry 的RetryInterceptorBuilder
支持创建一个。
10.5 查找远程配置资源
Config Service 提供来自/{name}/{profile}/{label}
的属性源,其中 Client 端应用程序中的默认绑定如下:
-
“名称” =
${spring.application.name}
-
“Profile” =
${spring.profiles.active}
(实际上是Environment.getActiveProfiles()
) -
“ label” =“大师”
Note
设置属性${spring.application.name}
时,请勿在您的应用名称前加上保留字application-
前缀,以防止解析正确的属性源时出现问题。
您可以通过设置spring.cloud.config.*
(其中*
是name
,profile
或label
)来覆盖所有参数。 label
对于回滚到以前的配置版本很有用。使用默认的 Config Server 实现,它可以是 git 标签,分支名称或提交 ID。标签也可以逗号分隔的列表形式提供。在这种情况下,列表中的项目将一一尝试直到成功为止。在要素分支上工作时,此行为可能很有用。例如,您可能想使 config 标签与您的分支对齐,但使其成为可选(在这种情况下,请使用spring.cloud.config.label=myfeature,develop
)。
10.6 为配置服务器指定多个地址
为确保在部署了 Config Server 的多个实例并希望不时有一个或多个实例不可用时的高可用性,可以指定多个 URL(作为spring.cloud.config.uri
属性下的逗号分隔列表)或将所有实例都包含在内在 Eureka 之类的服务注册表中注册(如果使用 Discovery-First Bootstrap 模式)。请注意,只有在未运行 Config Server 时(即,应用程序退出时)或发生连接超时时,这样做才能确保高可用性。例如,如果 Config Server 返回 500(内部服务器错误)响应,或者 Config Client 从 Config Server 收到 401(由于凭据错误或其他原因),则 Config Client 不会尝试从其他 URL 获取属性。此类错误表示用户问题,而不是可用性问题。
如果您在 Config Server 上使用 HTTP 基本安全性,则仅当您将凭据嵌入在spring.cloud.config.uri
属性下指定的每个 URL 中时,当前才有可能支持 per-Config Server 身份验证凭据。如果使用任何其他类型的安全性机制,则(当前)不能支持每个 Config Server 的身份验证和授权。
10.7 配置读取超时
如果要配置读取超时,可以使用属性spring.cloud.config.request-read-timeout
完成。
10.8 Security
如果在服务器上使用 HTTP 基本安全性,则 Client 端需要知道密码(如果不是默认用户名,则需要用户名)。您可以通过配置服务器 URI 或通过单独的用户名和密码属性来指定用户名和密码,如以下示例所示:
bootstrap.yml.
spring:
cloud:
config:
uri: https://user:[emailprotected]
以下示例显示了传递相同信息的另一种方法:
bootstrap.yml.
spring:
cloud:
config:
uri: https://myconfig.mycompany.com
username: user
password: secret
spring.cloud.config.password
和spring.cloud.config.username
值会覆盖 URI 中提供的任何内容。
如果您在 Cloud Foundry 上部署应用程序,则提供密码的最佳方法是通过服务凭据(例如 URI,因为它不需要在配置文件中)。以下示例在本地运行,并且适用于 Cloud Foundry 上名为configserver
的用户提供的服务:
bootstrap.yml.
spring:
cloud:
config:
uri: ${vcap.services.configserver.credentials.uri:http://user:[emailprotected]:8888}
如果您使用另一种形式的安全性,则可能需要提供一个 RestTemplate到ConfigServicePropertySourceLocator
(例如,通过在引导上下文中获取并注入它)。
10.8.1 健康 Metrics
Config Client 提供一个 Spring Boot Health Indicator,该指示器尝试从 Config Server 加载配置。可以通过设置health.config.enabled=false
禁用运行状况指示器。由于性能原因,响应也被缓存。默认的生存时间为 5 分钟。要更改该值,请设置health.config.time-to-live
属性(以毫秒为单位)。
10.8.2 提供自定义的 RestTemplate
在某些情况下,您可能需要自定义来自 Client 端对配置服务器的请求。通常,这样做涉及传递特殊的Authorization
Headers 以验证对服务器的请求。提供自定义RestTemplate
:
- 创建一个具有
PropertySourceLocator
实现的新配置 bean,如以下示例所示:
CustomConfigServiceBootstrapConfiguration.java.
@Configuration
public class CustomConfigServiceBootstrapConfiguration {
@Bean
public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
ConfigClientProperties clientProperties = configClientProperties();
ConfigServicePropertySourceLocator configServicePropertySourceLocator = new ConfigServicePropertySourceLocator(clientProperties);
configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
return configServicePropertySourceLocator;
}
}
- 在
resources/META-INF
中,创建一个名为spring.factories
的文件并指定您的自定义配置,如以下示例所示:
spring.factories.
org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration
10.8.3 Vault
将保管库用作配置服务器的后端时,Client 端需要为服务器提供令牌以从保管库检索值。可以通过在bootstrap.yml
中设置spring.cloud.config.token
来在 Client 端中提供此令牌,如以下示例所示:
bootstrap.yml.
spring:
cloud:
config:
token: YourVaultToken
10.9 Vault中的嵌套键
Vault支持将键嵌套在Vault中存储的值中的功能,如以下示例所示:
echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -
此命令将 JSON 对象写入您的Vault。要在 Spring 中访问这些值,您将使用传统的 dot(.
)注解,如以下示例所示
@Value("${appA.secret}")
String name = "World";
前面的代码会将name
变量的值设置为appAsecret
。