Spring Cloud 参考文档(Spring Cloud Config Client)

27次阅读

共计 5110 个字符,预计需要花费 13 分钟才能阅读完成。

Spring Cloud Config Client
Spring Boot 应用程序可以立即利用 Spring Config Server(或应用程序开发人员提供的其他外部属性源),它还提取了一些与 Environment 变化事件相关的额外有用特性。
配置优先 Bootstrap
在类路径上具有 Spring Cloud Config Client 的任何应用程序的默认行为如下:当配置客户端启动时,它会绑定到 Config Server(通过 spring.cloud.config.uri bootstrap 配置属性)并使用远程属性源初始化 Spring Environment。
这种行为的最终结果是,所有想要使用 Config Server 的客户端应用程序都需要一个 bootstrap.yml(或环境变量),其服务器地址在 spring.cloud.config.uri 中设置(默认为“http://localhost:8888″)。
发现优先 Bootstrap
如果你使用 DiscoveryClient 实现,例如 Spring Cloud Netflix 和 Eureka Service Discovery 或 Spring Cloud Consul,你可以将 Config Server 注册到 Discovery Service,但是,在默认的“配置优先 Bootstrap”模式下,客户端无法利用注册。
如果你更喜欢使用 DiscoveryClient 来定位 Config Server,可以通过设置 spring.cloud.config.discovery.enabled=true(默认值为 false)来实现,这样做的最终结果是客户端应用程序都需要具有适当发现配置的 bootstrap.yml(或环境变量)。例如,使用 Spring Cloud Netflix,你需要定义 Eureka 服务器地址(例如,在 eureka.client.serviceUrl.defaultZone 中),使用此选项的代价是启动时额外的网络往返,以查找服务注册,好处是,只要 Discovery Service 是固定点,Config Server 就可以更改其坐标。默认服务 ID 是 configserver,但你可以通过设置 spring.cloud.config.discovery.serviceId 在客户端上更改它(并在服务器上,以通常的方式提供服务,例如通过设置 spring.application.name)。
发现客户端实现都支持某种元数据映射(例如,我们为 Eureka 提供了 eureka.instance.metadataMap),可能需要在其服务注册元数据中配置 Config Server 的一些额外属性,以便客户端可以正确连接。如果使用 HTTP Basic 保护 Config Server,则可以将凭据配置为 user 和 password,此外,如果 Config Server 具有上下文路径,则可以设置 configPath,例如,以下 YAML 文件用于作为 Eureka 客户端的 Config Server:
bootstrap.yml
eureka:
instance:

metadataMap:
user: osufhalskjrtl
password: lviuhlszvaorhvlo5847
configPath: /config
Config Client 快速失败
在某些情况下,如果服务无法连接到 Config Server,你可能希望服务启动失败,如果这是所需的行为,请将 bootstrap 配置属性 spring.cloud.config.fail-fast=true 设置为使客户端停止并显示异常。
Config Client 重试
如果你预期配置服务器在应用程序启动时偶尔可能不可用,你可以在失败后继续尝试。首先,你需要设置 spring.cloud.config.fail-fast=true,然后,你需要在类路径中添加 spring-retry 和 spring-boot-starter-aop,默认行为是重试六次,初始回退间隔为 1000 毫秒,后续回退的指数乘数为 1.1,你可以通过设置 spring.cloud.config.retry.* 配置属性来配置这些属性(和其他属性)。
要完全控制重试行为,请添加一个类型为 RetryOperationsInterceptor 的 @Bean,其 ID 为 configServerRetryInterceptor,Spring Retry 有一个 RetryInterceptorBuilder 支持创建它。
查找远程配置资源
Config Service 从 /{name}/{profile}/{label} 提供属性源,其中客户端应用程序中的默认绑定如下:

“name”= ${spring.application.name}

“profile”= ${spring.profiles.active}(实际上是 Environment.getActiveProfiles())
“label”=“master”

设置属性 ${spring.application.name} 时,不要在应用程序名称前加上保留字 application-,以防止解析正确的属性源问题。
你可以通过设置 spring.cloud.config.* 来覆盖所有这些(其中 * 是 name、profile 或 label),该 label 可用于回滚到以前版本的配置,使用默认的 Config Server 实现,它可以是 git 标签,分支名称或提交 ID。标签也可以以逗号分隔列表的形式提供,在这种情况下,列表中的项目将逐个尝试,直到成功为止,在处理特性分支时,此行为非常有用。例如,你可能希望将配置标签与你的分支对齐,但使其成为可选(在这种情况下,请使用 spring.cloud.config.label=myfeature,develop)。
为 Config Server 指定多个 URL
当你部署了多个 Config Server 实例并预期一个或多个实例不时不可用时,为确保高可用性,你可以指定多个 URL(作为 spring.cloud.config.uri 属性下的逗号分隔列表),也可以让所有实例在 Eureka 等 Service Registry 中注册(如果使用发现优先 Bootstrap 模式)。请注意,只有在 Config Server 未运行时(即应用程序已退出时)或发生连接超时时,才能确保高可用性,例如,如果 Config Server 返回 500(内部服务器错误)响应或 Config Client 从 Config Server 收到 401(由于凭据错误或其他原因),则 Config Client 不会尝试从其他 URL 获取属性,这种错误表示用户问题而不是可用性问题。
如果在 Config Server 上使用 HTTP 基本安全性,则仅当你在 spring.cloud.config.uri 属性下指定的每个 URL 中嵌入凭据时,才能支持每个 Config Server 身份验证凭据,如果使用任何其他类型的安全机制,则无法(目前)支持每个 Config Server 身份验证和授权。
配置读取超时
如果要配置读取超,可以使用属性 spring.cloud.config.request-read-timeout 来完成此操作。
安全性
如果你在服务器上使用 HTTP Basic 安全性,客户端需要知道密码(如果不是默认值,则需要用户名),你可以通过配置服务器 URI 或单独的用户名和密码属性指定用户名和密码,如以下示例所示:
bootstrap.yml
spring:
cloud:
config:
uri: https://user:secret@myconfig.mycompany.com
以下示例显示了传递相同信息的另一种方法:
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 中,因为它不需要在配置文件中),以下示例在本地运行,并在名为 configserver 的 Cloud Foundry 上为用户提供服务:
bootstrap.yml
spring:
cloud:
config:
uri: ${vcap.services.configserver.credentials.uri:http://user:password@localhost:8888}
如果你使用其他形式的安全性,则可能需要向 ConfigServicePropertySourceLocator 提供一个 RestTemplate(例如,通过在引导程序上下文中获取它并注入它)。
健康指示器
Config Client 提供尝试从 Config Server 加载配置的 Spring Boot Health Indicator,可以通过设置 health.config.enabled=false 来禁用健康指示器,出于性能原因,还会缓存响应,生存的默认缓存时间为 5 分钟,要更改该值,请设置 health.config.time-to-live 属性(以毫秒为单位)。
提供自定义 RestTemplate
在某些情况下,你可能需要自定义从客户端向配置服务器发出的请求,通常,这样做涉及传递特殊的 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
Vault
使用 Vault 作为配置服务器的后端时,客户端需要为服务器提供令牌以从 Vault 检索值,可以通过在 bootstrap.yml 中设置 spring.cloud.config.token 在客户端内提供此令牌,如以下示例所示:
bootstrap.yml
spring:
cloud:
config:
token: YourVaultToken
Vault 中的嵌套密钥
Vault 支持将密钥嵌套在 Vault 中存储的值中,如以下示例所示:
echo -n ‘{“appA”: {“secret”: “appAsecret”}, “bar”: “baz”}’ | vault write secret/myapp –
此命令将 JSON 对象写入 Vault,要在 Spring 中访问这些值,可以使用传统的点(.)注解,如以下示例所示:
@Value(“${appA.secret}”)
String name = “World”;
上面的代码会将 name 变量的值设置为 appAsecret。

上一篇:推送通知和 Spring Cloud Bus

正文完
 0