【问题标题】:Share configuration between Spring cloud config clients在 Spring 云配置客户端之间共享配置
【发布时间】:2016-06-15 10:40:00
【问题描述】:

我正在尝试在 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 条目(参见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

【问题讨论】:

  • @AliDehghani 该问题已更新为正确的拼写
  • 您说的是“基于文件的存储库”,但您没有显示它的配置,也没有说明application.yml 的位置。
  • @DaveSyer 我说的是file-base repository,因为我使用的是native spring profileapplication.yml 文件位于本地类路径中,如标准 Spring Boot 应用程序
  • 如果我没记错的话,默认情况下我们不会为配置服务器自己的 application.yml 提供服务,否则任何服务都会尝试使用server.port: 8888 运行。将test.foo 放在application.yml 中,放在与hello-world-service.yml 相同的目录中。
  • @spencergibb hello-world-service.yml 文件与application.yml 位于同一目录中。我终于找到了问题的原因(见我的回答)

标签: spring spring-boot spring-cloud


【解决方案1】:

来自Spring Cloud documentation

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

所以我应该将共享配置放在外部目录中,并将路径添加到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

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 2018-04-27
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2023-04-10
    • 2016-07-31
    相关资源
    最近更新 更多