Spring Boot应用程序能够当即利用Spring Config Server(或应用程序开发人员提供的其余外部属性源),它还提取了一些与Environment
变化事件相关的额外有用特性。java
在类路径上具备Spring Cloud Config Client的任何应用程序的默认行为以下:当配置客户端启动时,它会绑定到Config Server(经过spring.cloud.config.uri
bootstrap配置属性)并使用远程属性源初始化Spring Environment
。git
这种行为的最终结果是,全部想要使用Config Server的客户端应用程序都须要一个bootstrap.yml
(或环境变量),其服务器地址在spring.cloud.config.uri
中设置(默认为“http://localhost:8888
" )。spring
若是你使用DiscoveryClient
实现,例如Spring Cloud Netflix和Eureka Service Discovery或Spring Cloud Consul,你能够将Config Server注册到Discovery Service,可是,在默认的“配置优先Bootstrap”模式下,客户端没法利用注册。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
) 。segmentfault
发现客户端实现都支持某种元数据映射(例如,咱们为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 Server,你可能但愿服务启动失败,若是这是所需的行为,请将bootstrap配置属性spring.cloud.config.fail-fast=true
设置为使客户端中止并显示异常。安全
若是你预期配置服务器在应用程序启动时偶尔可能不可用,你能够在失败后继续尝试。首先,你须要设置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}
提供属性源,其中客户端应用程序中的默认绑定以下:网络
${spring.application.name}
${spring.profiles.active}
(其实是Environment.getActiveProfiles()
)设置属性${spring.application.name}
时,不要在应用程序名称前加上保留字application-
,以防止解析正确的属性源问题。
你能够经过设置spring.cloud.config.*
来覆盖全部这些(其中*
是name
、profile
或label
),该label
可用于回滚到之前版本的配置,使用默认的Config Server实现,它能够是git标签,分支名称或提交ID。标签也能够以逗号分隔列表的形式提供,在这种状况下,列表中的项目将逐个尝试,直到成功为止,在处理特性分支时,此行为很是有用。例如,你可能但愿将配置标签与你的分支对齐,但使其成为可选(在这种状况下,请使用spring.cloud.config.label=myfeature,develop
)。
当你部署了多个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
属性(以毫秒为单位)。
在某些状况下,你可能须要自定义从客户端向配置服务器发出的请求,一般,这样作涉及传递特殊的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检索值,能够经过在bootstrap.yml
中设置spring.cloud.config.token
在客户端内提供此令牌,如如下示例所示:
bootstrap.yml
spring: cloud: config: token: YourVaultToken
Vault支持将密钥嵌套在Vault中存储的值中,如如下示例所示:
echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -
此命令将JSON对象写入Vault,要在Spring中访问这些值,可使用传统的点(.
)注解,如如下示例所示:
@Value("${appA.secret}") String name = "World";
上面的代码会将name
变量的值设置为appAsecret
。