首页 > 解决方案 > 如何从不同的 Consul 上下文中获取多个 Consul KV?

问题描述

我在 Consul 上有这些 KV 源存储:

config/books/<key>
config/common/<key>

在我的 Spring Boot 应用程序 application.yml 中,我将其配置如下:

spring:
  application:
    name: sampleapp
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true
        prefix: config
        defaultContext: books

我知道我所做的此配置已将应用程序指向从config/books读取。

但我不知道如何从config/commonconfig/books中检索 Consul KV 配置。

标签: javaspring-bootconsulspring-cloud-consulconsul-kv

解决方案


那里不是很灵活,但是如果您重命名项目并使用 defaultContext 也可以做到。根据文档Spring Cloud Consul Config 采用以下路径:

config/testApp,dev/
config/testApp/
config/application,dev/
config/application/

其中testApp - 您的应用程序的名称,应用程序- 默认上下文

因此,使用以下配置它将起作用

spring:
  application:
    name: books
  cloud:
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true
        prefix: config
        defaultContext: common

运行此配置后的日志:

Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/books/'}, ConsulPropertySource {name='config/common/'}]}

推荐阅读