0

Given a config client bootstrap.yml:

spring:
  ...
  cloud:
    config:
      uri: ${SPRING_CONFIG_URI:http://localhost:9000}
      fail-fast: true

The environment variable SPRING_CONFIG_URI needs to be set at runtime. We can debate this code smell another time, but for this exercise this URI is only known when the service starts (it has to do with fetching randomized SRV records).

Is there a Spring way to set/override some of the bootstrap.yaml parmeters?

Here is what I tried:

@SpringBootApplication
public class Application {
    public static void main( String[] args ) throws Exception
    {
        String configServerPort = DnsHelper.resolveSrv("configserver.local");

        // This doesn't work
        System.setProperty("SPRING_CONFIG_URI", configServerPort);

        // This doesn't work
        // Use reflection to change the in-memory environment variables
        // @see https://stackoverflow.com/a/7201825/1938889
        setEnv( Map.of( "SPRING_CONFIG_URI", configServerPort ) );

        // After the environment variable is set, then start the service
        Application.run( Application.class, args );
    }
    ...
Drakes
  • 23,254
  • 3
  • 51
  • 94
  • you can take a look this tutorial baeldung.com/spring-reloading-properties so that you can reload your properties in `runtime`. – Jonathan JOhx Nov 06 '19 at 16:07
  • Thanks @JonathanJohx. That looks useful for reloading env vars, but I can't see an obvious way to set an env var before the bootstrap starts initially. Are you able to glean an example? – Drakes Nov 06 '19 at 16:25
  • Reviewing a little your example and what you want to do, I think it is missing to enable this property `spring.cloud.config.overrideSystemProperties=true` try it and let me know, I used to use this type of configs when I wanted to override from `config server` to `any service`. – Jonathan JOhx Nov 06 '19 at 16:35

0 Answers0