设为首页 加入收藏

TOP

Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等(三)
2019-09-17 18:44:32 】 浏览:73
Tags:Spring Cloud 官方 文档 中文版 -Spring Config 客户端
.uri:http://user:password@localhost:8888}

如果开发人员使用其他安全方式,可能就需要提供一个到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原创文章,如转载请注明出处。
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇最近重构公司消息服务的架构设计 下一篇多服务器终端交互利器--polysh和a..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目