Skip to content

Understanding order of precedence for Spring boot configurations/properties

License

Notifications You must be signed in to change notification settings

bkjam/config-server-order

Repository files navigation

Exploring Loading Precedence for Properties files with Spring Boot, Spring Profiles, and Spring Cloud Config Server

This is my exploration to play around and understand how properties files are loaded in Spring Boot when using with Spring Profiles and Spring Cloud Config. For more information, refer to the blog post I have on medium below.

Exploration Article
Testing Loading Precedence of Properties Files 5 Observations on Spring Boot's Loading Precedence for Properties Files with Spring Cloud Config
Testing Local Development
Testing Refresh Properties
3 Useful Tips for Developers when using Spring Cloud Config

In this repository, I have configured some Spring Boot applications that loads configurations from the Spring Cloud Config Server:

Spring Boot Application Description
config-client sample app used for understanding order of precedence for properties files with Spring Cloud Config
config-client-bootstrap sample app used for understanding spring cloud config with bootstrap
config-client-local sample app used for understanding localhost development with Spring Cloud Config
config-client-refresh sample app used for understanding properties refresh

Additionally, the folders are for the following purpose:

Folder Description
config-repo stores the remote properties files
config-server Spring Cloud Config Server project

Usage

Exploration: Testing Loading Precedence of Properties Files

  1. Start the Spring Cloud Config Server

    ./gradlew config-server:bootrun -Pargs=--spring.profiles.active=git
  2. Start the Config Client

    ./gradlew config-client:bootrun
  3. Check the order of precedence

    curl localhost:8080/actuator/env

Exploration: Testing Localhost Development

1) Using Property "spring.cloud.config.allow-override"

  1. Start the Spring Cloud Config Server

    ./gradlew config-server:bootrun -Pargs=--spring.profiles.active=git
  2. Start the Config Client

    ./gradlew config-client-local:bootrun

2) Using "local" Spring Profiles

  1. Start the Spring Cloud Config Server

    ./gradlew config-server:bootrun -Pargs=--spring.profiles.active=git
  2. Start the Config Client with local Profiles

    ./gradlew config-client-local:bootrun -Pargs=--spring.profiles.active=dev,local

3) Using localhost Spring Cloud Config Server

  1. Start the Spring Cloud Config Server with Local File Repository

    ./gradlew config-server:bootrun -Pargs=--spring.profiles.active=native
  2. Start the Config Client

    ./gradlew config-client-local:bootrun

Exploration: Testing Refresh Properties (Manual Refresh)

  1. Start the Spring Cloud Config Server

    ./gradlew config-server:bootrun -Pargs=--spring.profiles.active=git
  2. Start the Config Client

    ./gradlew config-client-refresh:bootrun
  3. Print the Custom Property by calling the endpoint

    curl localhost:8080/api/print
  4. Update the remote properties files (config-client-refresh-dev.yaml)

    # Update config-client-refresh-dev.yaml
    
    # Commit and push changes to remote branch
    git add config-repo
    git commit -m "update configurations"
    git push
  5. Refresh the config-client

    curl -X POST localhost:8080/actuator/refresh
  6. Print the Custom Property by calling the endpoint. You should notice that the property have been updated.

    curl localhost:8080/api/print

Other useful commands

# Dockerize Config Client
./gradlew config-client:jibDockerBuild

# Start Dockerize Config Client
docker run -d --name config-client --network=host --env SPRING_CONFIG_ADDITIONAL_LOCATION='/config/' --volume <path_to_config_folder>:/config config-client

References / Credits

About

Understanding order of precedence for Spring boot configurations/properties

Resources

License

Stars

Watchers

Forks

Languages