Search code examples
springspring-bootspring-cloudcloud-foundryspring-cloud-config

Spring Cloud Config Server - Placeholder Label


I have developed a microservice using Spring Boot. This service is fetching the properties using Spring cloud config server. This microservice accepts version in the header and based on version , it executes the appropriate function. In my github repo , I have 2 branches , 1 for each version. The service usually sends the below information to config server to fetch the properties -

application-name + profile + label

Is there a way to have a placeholder in place of label in my .yml file? I want the label to be set to v1 dynamically , if I see v1 in the header else v2.

EDIT:

I see references to placeholder in this documentation (http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html) under section "Placeholders in Git URI", however I am not sure how values can be substituted dynamically from incoming request


Solution

  • spring-cloud-config-server makes several REST API available, allowing to query directly for a property file:

    $ hostname:port/{label}/{name}-{profiles}.properties]
    

    You can dynamically use any label of your choice, as long as it matches an existing label on git.

    For instance, to retrieve application.properties, labeled v1 in git:

     $ http://${hostname}:${port}/v1/application.properties
    

    Config-server REST API:

    • /{name}/{profiles}/{label:.*
    • /{label}/{name}-{profiles}.properties
    • /{name}-{profiles}.json
    • /{label}/{name}-{profiles}.json
    • /{label}/{name}-{profiles}.yml
    • /{label}/{name}-{profiles}.yaml
    • /{name}-{profiles}.yml
    • /{name}-{profiles}.yaml
    • /{name}/{profiles:.[^-].}
    • /{name}-{profiles}.properties
    • /{name}/{profile}/{label}/**

    I tried a sample spring-cloud-server project with a property file on a git. I applied the git tags v1 and v2 with different values in the file for each label (I used the profile remote):

    label v1:

    http://localhost:8888/v1/application-remote.properties
    > testproperty: remotevalue-v1
    

    label v2:

    http://localhost:8888/v2/application-remote.properties
    > testproperty: remotevalue-v2
    

    no label:

    http://localhost:8888/application-remote.properties
    > testproperty: remotevalue-master
    

    Java code

    I did not try it, but I suppose you could also use cloud-config-server's java API (injecting and calling the controller directly instead of doing an http request):

    @Autowired
    EnvironmentController environmentController;
    ...
    
    Environment labelled = environmentController.labelled("application", "remote", "v1");
    Map<?, ?> keyValues = labelled.getPropertySources().get(0).getSource();