.uri:http:
如果开发人员使用其他安全方式,可能就需要提供一个到ConfigServicePropertySourceLocator的RestTemplate(例:在bootstrap上下文中获取并注入一个)。
Health Indicator 健康指标
客户端支持Spring Boot的健康指标。不过也可以通过health.config.enabled=false来禁用。因为性能原因响应会被存到缓存。默认缓存生存5分钟。health.config.time-to-live属性可以修改。
提供一个自定义的RestTemplate
某些情况下,可以会需要自定义请求从客户端发送到配置中心。通常这涉及传递特殊的授权头来验证发送到服务端的请求。提供自定义的RestTemplate请按照以下步骤。
1.创建一个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;
}
}
2.在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。
dreamingodd原创文章,如转载请注明出处。
|