Unable to configure Spring Cloud Consul for specific profiles

1.7k views Asked by At

I'm trying to configure spring cloud consul for a specific profile but when I try to run the application I get the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Config data location 'consul:' does not exist

Action:

Check that the value 'consul:' at class path resource [application-local.properties] - 6:22 is correct, or prefix it with 'optional:'


Process finished with exit code 1

If I update spring.config.import to spring.config.import=optional:consul: in application-local.properties, consul configuration is ignored and the configuration from the properties file is used instead of consul.

application.properties:

spring.cloud.consul.enabled=false

application-local.properties:

spring.cloud.consul.enabled=true
spring.config.import=consul:

spring.cloud.consul.config.enabled=true
spring.cloud.consul.config.watch.enabled=true
spring.cloud.consul.config.watch.delay=6000
spring.cloud.consul.config.watch.wait-time=1
spring.cloud.consul.config.profile-separator=_

It seems that Spring is not overriding the default configuration with the one specified in application-local.properties. I also tried configuring consul in bootstrap.properties and bootstrap-local.properties but I got the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=consul: property to your configuration.
    If configuration is not required add spring.config.import=optional:consul: instead.
    To disable this check, set spring.cloud.consul.config.enabled=false or 
    spring.cloud.consul.config.import-check.enabled=false.


I'm using org.springframework.cloud:spring-cloud-starter-consul-config:3.0.3 and spring boot 2.4.7

Thank you in advance.

2

There are 2 answers

1
Ali mjz On

I did this before, so first you can add below dependency :

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

and second, create a bootstrap.yml file. you can set your profile here and have separate configuration like below:

        spring:
      profiles:
        active: dev
        include:
          - consul
    #      - offline
      cloud:
        consul:
          host: 10.20.8.67
          port: 8500
          config:
            enabled: false
          discovery:
            heartbeat:
              enabled: true
              ttl: 5s
            acl-token: 4319cf67-2690-62b3-a8bb-623d79b92737
          service-registry:
            enabled: true
          retry:
            enabled: true
            max-attempts: 1
      application:
        name: servicename
    server:
      port: 8090
    
    ---
    
spring:
  config:
    activate:
      on-profile: offline
  cloud:
    bus:
      enabled: false
    consul:
      config:
        enabled: false
  application:
    name: servicename

in this case, if you set include profile to offline, the offline section property will use and disable your config consul. with bootstrap.yml you can enable or disable discovery or config feature in consul. you can also use active profile directly instead of include profile group.

0
Alec BW On

Try including the following in your pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>