2

I'm trying to create Spring Application without referring to any external files. This is supposed to be a module that you'd then include as a dependency, configure and use to plug in the service into an existing ecosystem. This is how I'm doing that:

Map<String, Object> properties = new HashMap<>();
properties.put("server.address", "0.0.0.0")
properties.put("server.port", 8080)
properties.put("spring.profiles.active", "cloud")
properties.put("spring.application.name", "someApp")
properties.put("spring.cloud.config.failFast", true)
properties.put("spring.cloud.config.discovery.enabled", true)
properties.put("spring.cloud.config.discovery.serviceId", "config")
properties.put("eureka.instance.preferIpAddress", true)
properties.put("eureka.instance.statusPageUrlPath", "/health")

new SpringApplicationBuilder()
  .bannerMode(Banner.Mode.OFF)
  .properties(properties)
  .sources(SpringConfiguration.class)
  .web(false)
  .registerShutdownHook(true)
  .build()

I then go on to provide Eureka default zone in the run command, via environmental variables:

--env eureka_client_serviceUrl_defaultZone='http://some-host:8765/eureka/' --env SPRING_CLOUD_CONFIG_LABEL='dev' --env SPRING_CLOUD_INETUTILS_PREFERRED_NETWORKS='10.0'

Application registers successfully in Eureka, but unfortunately it tries to fetch the config prior to that and it's looking for it under the default URL (http://localhost:8888) instead of fetching config server IP from the registry. And yes, it does work if I put all of those properties in the bootstrap.yml file. Can I somehow make it work without using file-resources?

Bartek Andrzejczak
  • 1,292
  • 2
  • 14
  • 27

1 Answers1

0

You are passing the properties using SpringApplicationBuilder which is responsible for SpringApplication and ApplicationContext instances.

From the documentation , the properties provided here will be part of ApplicationContext NOT the BootstrapContext. ApplicationContext is the child of BootstrapContext.

You can read more about the Bootstrap Context here -

http://cloud.spring.io/spring-cloud-commons/1.3.x/single/spring-cloud-commons.html#_the_bootstrap_application_context

Bootstrap.yml/properties is used to configure your Bootstrap Context.

You can look at these properties to change the name or location of the file -

                 spring.cloud.bootstrap.name - bootstrap(default)
                 spring.cloud.bootstrap.location

You will have to use a file resource(yml or properties).

Indraneel Bende
  • 3,196
  • 4
  • 24
  • 36