javaspring-bootconsulspring-cloud-consulconsul-kv

How to get multiple Consul KVs from different Consul context?


I have these KVs sources store on Consul:

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

And in my spring boot app application.yml, I have config it as following:

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

I know that this configuration that I made has pointed the app to read from config/books .

But I can't figure out how can I retrieve the Consul KV config from both config/common and config/books.


Solution

  • It's not really flexible there, but it can be done if you rename your project and use defaultContext as well. Based on the documentation Spring Cloud Consul Config takes next paths:

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

    where testApp - name of your app, application - default context

    So with the following configuration it will work

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

    Logs after running this configuration:

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