在某些状况下,您可能须要从客户端自定义对配置服务器的请求。一般这涉及传递特殊的Authorization标头来对服务器的请求进行身份验证。要提供自定义RestTemplate,请按照如下步骤操做。 设置spring.cloud.config.enabled=false以禁用现有的配置服务器属性源。java
使用PropertySourceLocator实现建立一个新的配置bean。spring
CustomConfigServiceBootstrapConfiguration.java @Configuration public class CustomConfigServiceBootstrapConfiguration { @Bean public ConfigClientProperties configClientProperties() { ConfigClientProperties client = new ConfigClientProperties(this.environment); client.setEnabled(false); return client; }bootstrap
@Bean
public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
ConfigClientProperties clientProperties = configClientProperties();
ConfigServicePropertySourceLocator configServicePropertySourceLocator = new ConfigServicePropertySourceLocator(clientProperties);
configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
return configServicePropertySourceLocator;
}
复制代码
} 在resources/META-INF中建立一个名为spring.factories的文件,并指定您的自定义配置。后端
spring.factorties 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支持将键嵌入存储在Vault中的值。例如app
echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -this
此命令将向您的Vault编写一个JSON对象。要在Spring中访问这些值,您将使用传统的点(。)注释。例如spa
@Value("${appA.secret}") String name = "World"; 上代码将name变量设置为appAsecret。code