Can Spring Boot application using Consul for configuration be compiled to GraalVM native?

110 Views Asked by At

I have a Spring Boot (v3.1.4) application that I would like to deploy as AOT compiled image. The DB url is specific to each environment in which the application is deployed. It use a property in application.properties "spring.datasource.url". The value of this is provided to the application via Consul. The application.properties contains the configuration to connect to Consul as:

spring.config.import=consul:
spring.cloud.consul.enabled = true
spring.cloud.consul.config.enabled = true
spring.cloud.consul.config.fail-fast = true
spring.cloud.consul.host = ${CONSUL_SERVER}
spring.cloud.consul.port = ${CONSUL_PORT}
spring.cloud.consul.config.prefixes = config/${APP_ENVIRONMENT}
spring.cloud.consul.config.defaultContext = ${spring.application.name}

The CONSUL_SERVER, CONSUL_PORT, and APP_ENVIRONMENT are defined as environment variables on the host where the deployed application run.

The problem I am having is that the DB url, when the application runs on the deployment host, is the value that was returned from Consul to the build host.

There is a paragraph on this page https://github.com/spring-cloud/spring-cloud-release/wiki/AOT-transformations-and-native-image-support that covers Spring Cloud Config. In this paragraph one sentence seems to indicate that the values from Consul will be baked in at build time. But the next sentence seems to indicate that Consul will be queried at runtime. Both seem to be true, but why.

If the value from Consul are runtime will not override the values from Consul at build time, what is the point.

Do I have to build a unique native image for each environment?

1

There are 1 best solutions below

0
On

I found that answer. Instead of an environment variable of "APP_ENVIRONMENT=<env> on the deployed host the environment variable of "spring.cloud.consul.config.prefixes=config/<env>" is required.