0

配置服务器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"
         }
      }
   ]
}

因此,我尝试foo从 Config 客户端从 Config 服务器获取价值。配置客户端bootstrap.yml

spring:
  application:
    name: configclient
  cloud:
    config:
      uri: http://localhost:8888
      headers:
        X-Config-Token: ${vault_token}

但是,配置客户端似乎无法找到配置服务器:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

有任何想法吗?

4

1 回答 1

0

在您的 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

于 2018-10-20T17:54:53.650 回答