配置 Spring 使用 Spring-Cloud-Connectors 启动以使用 PWS 配置服务器

Configuring Spring Boot with Spring-Cloud-Connectors to use the PWS Config-Server

我很难配置我的 Spring 启动应用程序以连接到通过 Spring-Cloud-Connectors 提供的 Config-Server 的 PWS(关键 Web 服务) .

在 manifest.yml 中,配置服务器绑定到应用程序,相应的 VCAP_SERVICES 条目正确反映了这一点:

applications:
- name: edge-service-webapp-myapp
  services:
  - infrastructure-config-server
  memory: 512M
  env:
    TRUST_CERTS: api.run.pivotal.io
    SPRING_PROFILES_DEFAULT: cloud
  instances: 1
  host: edge-service-webapp-myapp
  domain: cfapps.io
  buildpack: java_buildpack

{
 "VCAP_SERVICES": {
  "p-config-server": [
   {
    "credentials": {
     "access_token_uri": "https://p-spring-cloud-services.uaa.run.pivotal.io/oauth/token",
     "client_id": "p-config-server-84d66ea6-ebc6-xxx",
     "client_secret": "***",
     "uri": "https://config-b4320676-xxx.cfapps.io"
    }, ...
}

该应用程序是使用 spring-boot-starter-parent 1.5.2.RELEASE、spring-cloud-dependencies Camden.SR5 和 spring-cloud- 构建的服务依赖 1.4.1.RELEASE。我还使用 spring-cloud-starter-config 和 spring-boot-starter-cloud-connectors 作为显式依赖项。

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.pivotal.spring.cloud</groupId>
                <artifactId>spring-cloud-services-dependencies</artifactId>
                <version>1.4.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
             ....
        </dependencies>
    </dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cloud-connectors</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
       ...

当我卷曲配置服务器时,我可以看到可用于应用程序名称 (my-app) 和活动 'cloud' 配置文件的应用程序版本。

spring:
  application:
    name: my-app
  cloud:
    config:
      enabled: true


curl -H "Authorization: Bearer XXX" https://config-b4320676-xxx.cfapps.io/tradefoundry/cloud
{"name":"my-app","profiles":["cloud"],"label":"master","version":"389e4f909ff1303332167b2159b4d75201109d69","state":null,"propertySources":[{"name":"https://gitlab.com/myapp/configuration.git/myapp-cloud.properties","source":{"spring.thymeleaf.cache":"true","message":"Hello Cloud!"}},{"name":"https://gitlab.com/myapp/configuration.git/myapp.properties","source":{"server.compression.enabled":"true","spring.thymeleaf.cache":"true","application.version":"0.0.1"}},{"name":"https://gitlab.com/myapp/configuration.git/application.properties","source":{"server.compression.enabled":"true","spring.thymeleaf.cache":"true","application.cache.busting.enabled":"false","application.version":"0.0.1-20170202195700","server.compression.mime-types":"application/json,application/xml,text/html,text/xml,text/plain,text/css,application/javascript"}}]}

但是还是启动失败,报错属性 application.version.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myWebappApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'application.version' in value "${application.version}"

我在这里错过了什么?我以为云连接器都是通过自动配置即插即用的!?

欢迎任何帮助!

要使用 PWS 上提供的 Spring 云服务配置服务器,您需要使用一组不同的客户端库,如 PWS docs 中所示。

将对 spring-boot-starter-cloud-connectorsspring-cloud-starter-config 的依赖替换为仅此一个依赖:

<dependency>
  <groupId>io.pivotal.spring.cloud</groupId>
  <artifactId>spring-cloud-services-starter-config-client</artifactId>
</dependency>

Spring 云服务配置服务器在开源 Spring 云配置服务器之上添加了额外的基于 OAuth2 的安全性。这个客户端库自动进行 OAuth 协商。