Spring boot external configuration with spring cloud config and spring actuator

While developing a spring boot application , most developers have to define some fixed properties in their application.properties like the secret key for the token , the session timeout, the path of uploaded files and a lot of other properties.

The problem with this approch is that these properties are loaded only once when the server starts. So what’s gonna happen when we want to change some properties while the app is in prod. In this cas we have to shut down the server , change the properties and restart the server.

Even worse , what if we’re working multiple microservices , it would be a disaster if we shut down all microservices.

Solution?

To avoid these complications we have to externalize our config files in a distance github repository and access these files using spring cloud config which represtents a distributed file server as described below:

Externalization

1-Create a git repository

As a first a step we have to create a git repository where we will store the configuartions of our application or microservices and name it for example“configuration-server-repo”.

In this repo we create a propertie file and name it with the exact name defined in the propertie : spring.application.name of the app , because that’s how our config server will link the application with it’s distant configuration.

For example we have an ecommerce application, in its application.properties file we add this line:

spring.application.name=ecommerce-application

And the application.propeties file in the git repo will have the same name: ecommerce-application.properties

Now we move all the properties of the app (except the propertie spring.application.name )into the ecommerce-application.properties file in the git repo and commit the changes.

Our git repo will look like this:

Git repo

2-Create the config server application

Now go to spring initializr or from any IDE and create a new spring boot application with spring cloud config as starter

config-server

Open the project with your IDE and go to application.properties and put the following properties:

spring.application.name=config-server
server.port
:9101 #config server port
spring.cloud.config.server.git.uri
=https://github.com/FirasMessaoudi/configuration-server-repo.git #url of the git repo containing the propertie file of the application.

We have now to define the app as a config server by adding the annotation @EnableConfigServer

ConfigServerApplication

Start the server and navigate to http://localhost:9101/ecommerce-application/default. You will see your application.properties retrieved from the git repo:

3-Linking ecommerce application with the server config

As for now we have a distant repo linked to the server config, we must now tell the ecommerce application to get its configurations from the config server.

Update the pom.xml with the following dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Go to the application.properties of the ecommerce application which only has the propretie “spring.application.name” and add the following propertie:

spring.cloud.config.uri=http://localhost:9101

Start the ecommerce application and you should see this log in the console :

Fetching config from server at: http://localhost:9101

Which means that the ecommerce application is now fetching its configuration from the config server.

4-Refresh configuartions

We have one last problem to solve: if you try to change the property upload.path and upload a new image from your application , you will notice that the path didn’t change. So do we still have to restart the config server ?

Not if we use spring Actuator which provides an endpoint to our application to refresh the configuration without restarting the app nor the the config server.

Go to pom.xml in the ecommerce application and the following starter:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

And add the following propertie ( in the propertie file in the app or in the distant repo):

management.endpoints.web.exposure.include=*

Finally we have to inform all the beans consuming any of the properties to get refreshed each time we make a change, by adding the following annotation:

@RefreshScope

Restart the ecommerce application , change your upload.path propertie from the git , commit the changes and trigger the resfresh event by making a post request:

http://localhost:8080/actuator/refresh.

Now go back and upload another image , and you’ll see the new path being considered by your application.

Final result

So now we have our ecommerce application fetching its config from a distant git repo through the config-server. And we can change our configurations with no need to restart the application nor the config server.

Enjoy reading and clap if you like this article.

Full stack (Spring boot — Angular ) developer and computer science engineering student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store