Search code examples
springspring-bootspring-cloud-config

Spring Cloud Client Config with placeholders


I have a Spring Boot application.yml with this config for Cloud Config:

spring:
  cloud:
    config:
      uri: http://localhost:8080/config
      name: ${cluster.name}
      profile: ${cluster.idx}

I read cluster.name and cluster.idx from a custom PropertySouce that loads a JSON file. But Spring Boot is not capable of resolve the placeholder yet.

13:04:37,370 ERROR [main] org.springframework.boot.SpringApplication(SpringApplication.java:839) : Application startup failed
java.lang.IllegalArgumentException: Could not resolve placeholder 'cluster.idx' in string value "${cluster.idx}" 

How do I tell Spring Boot to load my PropertySource before trying to resolve the placeholders? Thanks in advance.

I'm using Sp Boot 1.4.3 and Sp Cloud Config 1.2.2.


Solution

  • You won't be able to use the property sources as they are not yet available, and will not be at the time that this file is read. You have a couple of options, though. First, you can place these properties into the bootstrap.properties (or bootstrap.yml if you prefer). I suspect that you are trying to do something that's profile driven though and should use the appropriate properties for the profile. In this case, you can create bootstrap-{profile}.yml. So, if you are running with -Dspring.profiles.active=dev, you would have a bootstrap-dev.yml file that would contain the values for this profile.

    The second and simpler approach would be to pass these to the VM as arguments. -Dcluster.name=foo -Dcluster.idx=bar