配置服务器bootstrap.yml
:
spring:
application:
name: configserver
profiles:
active: vault
cloud:
config:
server:
vault:
host: ${vault_server_host:localhost}
port: ${vault_server_port:8200}
scheme: ${vault_server_scheme:https}
backend: ${vault_backend:configserver}
保险库机密:
$ vault kv get configserver/configclient
=== Data ===
Key Value
--- -----
foo VAUUULT
因此,我可以使用curl
:获取配置值
$ curl -X GET http://localhost:8888/configclient/default -H "X-Config-Token: f7b238dd-425f-52f8-2104-1e37ecf65ede"
{
"name":"configclient",
"profiles":[
"default"
],
"label":null,
"version":null,
"state":null,
"propertySources":[
{
"name":"vault:configclient",
"source":{
"foo":"VAUUULT"
}
}
]
}
因此,我尝试从Config客户端从Config服务器获取foo
值。配置客户端bootstrap.yml
:
spring:
application:
name: configclient
cloud:
config:
uri: http://localhost:8888
headers:
X-Config-Token: ${vault_token}
然而,Config客户端似乎无法定位Config服务器:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-07-12 10:03:53.809 INFO 15448 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-07-12 10:03:54.239 WARN 15448 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 400 null
2018-07-12 10:03:54.256 INFO 15448 --- [ main] c.t.i.t.s.t.TdevConfigclientApplication : No active profile set, falling back to default profiles: default
所以,它让我明白:
由java.lang.IllegalArgumentException引起:无法解析值"${foo}"中的占位符"foo">
foo
配置为@Value("${foo}")
:
@SpringBootApplication
@RestController
public class TdevConfigclientApplication {
@RequestMapping("/")
public String home() {
return "Hello World! " + this.foo;
}
@Value("${foo}")
private String foo;
public static void main(String[] args) {
SpringApplication.run(TdevConfigclientApplication.class, args);
}
}
在这里,您可以看到更详细的配置客户端跟踪片段:
2018-07-12 10:29:05.249 INFO 17299 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-07-12 10:29:05.457 DEBUG 17299 --- [ main] o.s.web.client.RestTemplate : Created GET request for "http://localhost:8888/configclient/default"
2018-07-12 10:29:06.023 DEBUG 17299 --- [ main] o.s.web.client.RestTemplate : Setting request Accept header to [application/json, application/*+json]
2018-07-12 10:29:06.092 DEBUG 17299 --- [ main] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@3bb6b7e25 pairs: {GET /configclient/default HTTP/1.1: null}{Accept: application/json, application/*+json}{User-Agent: Java/10.0.1}{Host: localhost:8888}{Connection: keep-alive}
2018-07-12 10:29:06.121 DEBUG 17299 --- [ main] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@3b1892d05 pairs: {null: HTTP/1.1 400}{Content-Type: application/json;charset=UTF-8}{Transfer-Encoding: chunked}{Date: Thu, 12 Jul 2018 08:29:06 GMT}{Connection: close}
2018-07-12 10:29:06.145 DEBUG 17299 --- [ main] o.s.web.client.RestTemplate : GET request for "http://localhost:8888/configclient/default" resulted in 400 (null); invoking error handler
2018-07-12 10:29:06.162 WARN 17299 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 400 null
有什么想法吗?
在bootstrap.yml中,您需要将spring.cloud.config.headers替换为:
spring:
application:
name: configclient
cloud:
config:
uri: http://localhost:8888
token : ${vault_token}
你可以看到医生http://cloud.spring.io/spring-cloud-config/1.4.x/single/spring-cloud-config.html