在 Spring 个云配置客户端之间共享配置

Share configuration between Spring cloud config clients

我正在尝试在 Spring Cloud 客户端与 Spring Cloud 配置服务器之间共享配置有一个基于文件的存储库:

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

// application.yml
server:
  port: 8888

spring:
  profiles:
    active: native

test:
  foo: world

我的一个 Spring Cloud 客户端使用 test.foo 配置,在配置服务器中定义,配置如下:

@SpringBootApplication
@RestController
public class HelloWorldServiceApplication {

    @Value("${test.foo}")
    private String foo;

    @RequestMapping(path = "/", method = RequestMethod.GET)
    @ResponseBody
    public String helloWorld() {
        return "Hello " + this.foo;
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldServiceApplication.class, args);
    }
}

// boostrap.yml
spring:
  cloud:
      config:
        uri: ${SPRING_CONFIG_URI:http://localhost:8888}
      fail-fast: true

// application.yml
spring:
  application:
    name: hello-world-service

尽管有此配置,Spring 云 客户端中的 Environment 不包含 test.foo 条目(参见 java.lang.IllegalArgumentException: Could not resolve placeholder 'test.foo')

但是,如果我将属性放在 hello-world-service.yml 文件中,在我的基于配置服务器文件的存储库中,它会完美运行。

Maven dependencies on Spring Cloud Brixton.M5 and Spring Boot 1.3.3.RELEASE with spring-cloud-starter-config and spring-cloud-config-server

来自Spring Cloud documentation

With the "native" profile (local file system backend) it is recommended that you use an explicit search location that isn’t part of the server’s own configuration. Otherwise the application* resources in the default search locations are removed because they are part of the server.

所以我应该将共享配置放在外部目录中,并将路径添加到 config-serverapplication.yml 文件中。

// application.yml
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:/Users/herau/config-repo

// /Users/herau/config-repo/application.yml
test:
  foo: world