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

时间:2016-03-02 14:11:00

标签: spring spring-boot spring-cloud

我尝试在 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 Cloud 客户端中的Environment并不包含test.foo条目(cf java.lang.IllegalArgumentException: Could not resolve placeholder 'test.foo'

然而,如果我将属性放在hello-world-service.yml文件中,在我的配置服务器基于文件的存储库中,它的效果非常好。

  

Maven依赖于 Spring Cloud Brixton.M5和 Spring Boot 1.3.3.RELEASE与spring-cloud-starter-configspring-cloud-config-server

1 个答案:

答案 0 :(得分:1)

来自Spring Cloud documentation

  

使用“本机”配置文件(本地文件系统后端)   建议您使用不属于您的显式搜索位置   服务器自己的配置。否则申请*   默认搜索位置中的资源将被删除,因为它们是   服务器的一部分。

所以我应该将共享配置放在外部目录中,并在application.yml的{​​{1}}文件中添加路径。

config-server