spring-bootenvironment-variablesspring-cloud-configsetx

how to change config server uri dynamically using OS environment


I have a spring config server and spring config client. In client I have set spring.cloud.config.uri to http://localhost:8888 but I want to change it to some other uri say http://example.com:8888 in windows using OS environment with setx. so I ran

setx spring.cloud.config.uri "http://example.com:8888" 

but when I run my spring config client it's still trying to read from localhost . As per this link my spring.cloud.config.uri in bootstrap.yml should get override by what i set with OS environment but it didn't. Please let me know what I am doing wrong here.


Solution

  • setx

    setx adds the variable but doesn't make it available in the current shell (as explained here):

    setx modifies the value permenantly, which affects all future shells, but does not modify the environment of the shells already running. You have to exit the shell and reopen it before the change will be available, but the value will remain modified until you change it again.

    Just make sure you are runnning from a newly opened shell window.

    I suggest you use set spring.cloud.config.uri=http://example.com:8888 just for the sake of testing.

    Check if the environment variable is present

    You may add the following as the very first line your main method:

    System.out.println(System.getenv("spring.cloud.config.uri"));
    System.out.println(System.getenv("SPRING_CLOUD_CONFIG_URI"));
    

    Variable name with underscore

    Windows supports environment variables with dots, so it should be ok for you. This isnt the case for pretty much all the other OS. Just so you know, spring supports variable name with underscores (as explained in the link you provided):

    If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (e.g. SPRING_CONFIG_NAME instead of spring.config.name).