一个Spring Boot应用程序可以立即使用Spring Config Server(或应用程序开发人员提供的其他外部属性源),并且还将获取与Environment更改事件相关的一些其他有用功能。
1 配置固定服务器地址
这是使用了Spring Cloud Config Client的任何应用程序的默认行为:当配置客户端启动时,它将通过配置服务器(通过引导配置属性spring.cloud.config.uri)绑定并使用远程属性源初始化Spring Environment。
所有想要使用配置服务器的客户端应用程序都要配置bootstrap.yml(或环境变量) 中的spring.cloud.config.uri(默认为“http:// localhost:8888”)。
2 服务发现配置中心
如果您正在使用“DiscoveryClient”组件,例如Spring Cloud Netflix的Eureka Service Discovery或Spring Cloud Consul,那么您可以让Config Server注册到Discovery 服务。但在默认的“配置优先”模式下,客户端将无法使用注册功能。
如果您希望使用DiscoveryClient找到Config Server,可以通过设置spring.cloud.config.discovery.enabled=true(默认为“false”)来实现。同时客户端应用程序都需要在bootstrap.yml(或环境变量)中设置合适属性。例如,使用Spring Cloud Netflix,您需要定义Eureka服务器地址(例如eureka.client.serviceUrl.defaultZone)。使用此选项的代价是启动时额外的网络请求,以定位服务注册组件。好处是配置服务器可以更改其地址,只要服务发现组件是固定的。默认的服务标识是“configserver”,但是您可以使用spring.cloud.config.discovery.serviceId在客户端上进行更改(在服务器上可以通过设置spring.application.name以通常方式更改服务名)。
服务发现组件都支持某种元数据map(例如,对于Eureka,我们有eureka.instance.metadataMap)。可能需要在其服务注册元数据中配置Config Server的一些其他属性,以便客户端可以正确连接。如果使用HTTP Basic安全配置服务器,则可以将凭据配置为“user”和“password”。并且如果配置服务器具有上下文路径,您可以设置“configPath”。例如,对于作为Eureka客户端的配置服务器:
bootstrap.yml
eureka:
instance:
...
metadataMap:
user: osufhalskjrtl
password: lviuhlszvaorhvlo5847
configPath: /config
3 配置客户端快速失败
在某些情况下,如果服务无法连接到配置服务器,则可能希望启动服务失败。如果这是所需的行为,请设置引导配置属性spring.cloud.config.failFast=true,客户端抛出异常并停止。
4 配置客户端重试
如果您的配置服务器在您的应用程序启动时可能偶尔不可用,您可以要求它在发生故障后继续尝试。首先,您需要设置spring.cloud.config.failFast=true,然后您需要将spring-retry和spring-boot-starter-aop依赖添加到您的类路径中。默认行为是重试6次,初始重试间隔为1000ms,指数乘数为1.1,用于后续重试。您可以使用spring.cloud.config.retry.*属性配置这些属性(和其他)。
要完全控制重试,请使用id“configServerRetryInterceptor”添加RetryOperationsInterceptor类型的@Bean。Spring Retry有一个RetryInterceptorBuilder,可以轻松创建一个。
5 查找远程配置资源
配置服务从/{application}/{profile}/{label}提供属性源,客户端应用程序中的默认绑定有:
- “name” = ${spring.application.name}
- “profile” = ${spring.profiles.active} (actually Environment.getActiveProfiles())
- “label” = “master”
设置属性“${spring.application.name}”时,不要使用前缀“application-”,避免匹配属性源时出错。
所有这些都可以通过设置spring.cloud.config.(其中是“name”,“profile”或“label”)来覆盖。“label”可用于回滚到以前版本的配置; 使用默认的Config Server实现,它可以是git label,分支名称或提交ID。label也可以以逗号分隔的列表形式提供。在这种情况下,会逐个尝试列表中的项目,直到成功。这对于在特征分支上工作时可能很有用。例如,当您可能希望将配置标签与您的分支对齐,但使其成为可选(如spring.cloud.config.label=myfeature,develop)。
6 配置中心高可用
为了保证配置中心的高可用,你可能会有多个配置中心实例,以便实现轮流尝试。你可以指定多个URL(设置spring.cloud.config.uri,以逗号分隔),或注册到eureka那样的注册中心。
只有一个配置中心下线或连接超时时,才会尝试连接其它实例。比如,如果配置中心返回一个500错误或401(未授权等),客户端不会尝试连接其它的配置中心实例。这类的错误通常是由用户造成的,而不是高可用问题。
如果你的配置中心开启了http安全认证,你可以在
pring.cloud.config.uri
属性中为每个配置服务器配置单独的认证。但如果你用的是其它的认证机制,目前不能为每个服务器单独配置认证。
7 配置超时时间
如果你想配置超时时间:
- spring.cloud.config.request-read-timeout 可以设置读超时
- spring.cloud.config.request-connect-timeout 可以设置连接超时
8 安全
如果您在服务器上使用HTTP基本安全认证,那么客户端需要知道密码(如果不是默认用户名也要提供用户名)。您可以在服务器URI中提供用户名密码,或通过单独的username和password属性设置,例如
bootstrap.yml
spring:
cloud:
config:
uri: https://user:[email protected]
或使用下面的配置:
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:[email protected]:8888}
如果您使用其它安全认证,则可能需要向ConfigServicePropertySourceLocator提供RestTemplate(例如,通过在引导上下文中获取并注入一个)。
8.1 健康指标
配置客户端提供尝试从配置服务器加载配置的Spring Boot运行状况指示器。可以通过设置health.config.enabled=false来禁用运行状况指示器。由于性能原因,响应也被缓存。默认缓存生存时间为5分钟。要更改该值,请设置health.config.time-to-live属性(以毫秒为单位)。
8.2 提供自定义RestTemplate
在某些情况下,您可能需要从客户端自定义对配置服务器的请求。通常这涉及传递特殊的Authorization头部来对服务器的请求进行身份验证。要提供自定义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;
}
}
可以设置 spring.cloud.config.headers.* 属性,可以快速添加 Authorization
头
在resources/META-INF中创建一个名为spring.factories的文件,并指定您的自定义配置:
spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration
8.3 Vault
当使用Vault作为配置服务器的后端时,客户端将需要为服务器提供令牌以从Vault中检索值。可以通过在客户端的bootstrap.yml中设置spring.cloud.config.token来提供此令牌。
bootstrap.yml
spring:
cloud:
config:
token: YourVaultToken
9 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。
10 刷新配置
详见 手动刷新配置
Spring Cloud Config 使用说明(1) – 快速开始
Spring Cloud Config 配置中心使用说明(2) – 服务器+git库
Spring Cloud Config 配置中心使用说明(3) – 服务器+文件存储
Spring Cloud Config 配置中心使用说明(4) – 服务器+数据库存储
Spring Cloud Config 配置中心使用说明(5) – 服务器健康与安全配置
Spring Cloud Config 配置中心使用说明(6) – 服务器 配置文件格式
Spring Cloud Config 配置中心使用说明(7) – 服务器 配置变更通知
Spring Cloud Config 配置中心使用说明(8) – 客户端