Home > Net >  Profile based Spring Cloud Config Server client with optional local developer properties
Profile based Spring Cloud Config Server client with optional local developer properties

Time:06-25

My Goal:

Having the Spring Cloud Config Server import active and provide a way for developers to have an optional property file on their machine.

wanted cfg prio diagram

My Config Server is up and running using org.springframework.cloud:spring-cloud-config-server:3.1.3 and @EnableConfigServer on the main class. Http requests to the concrete endpoint yield the expected result. This server should provide important environment configurations for a developer for his/her local setup.

$ curl http://localhost:8888/test-application/dev
{"name":"test-application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cfg/test-application/application-dev.yml","source":{"server.port":1111}},{"name":"classpath:/cfg/test-application/application.yml","source":{"server.port":8000}}]}

Where localhost:8888 is my Config Server and test-application ist the name of the client application (defined via spring.application.name). The provided dev is the currently active profile on the client (The dev profile indicates a locally running software, there is no dev environment).

Clients configuration:

application.yml

spring:
  application:
    name: test-application
  config:
    import:
      - configserver:http://localhost:8888                  # <- pull the configuration from the configserver
      - optional:file:/absolute/path/to/the/project/root/   # <- if there are any additional configuration files, use them

The client uses the following dependencies:

  • org.springframework.boot:spring-boot-starter-web:2.7.0
  • org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.3
  • org.springframework.cloud:spring-cloud-starter:3.1.3
  • org.springframework.cloud:spring-cloud-starter-config:3.1.3

As shown above, the "base" application.yml configured server.port=8000 where the profile specific is set to server.port=1111. When reading the Environment runtime snapshot

Does anyone have an idea how to solve this problem?

I have created a project that reproduces this exact behaviour. enter image description here

And put it in configServer application.yml at the end of search-locations. Order matters so please make sure you put overriding location/s at the end.

/configServer/src/main/resources/application.yml enter image description here

Build and start both the applications and you will see client application server started on port 2222 enter image description here

  • Related