Sign in
Log inSign up
Refresh application.properties in Spring Boot without restarting tomcat server

Refresh application.properties in Spring Boot without restarting tomcat server

Ruptam Sadhukhan's photo
Ruptam Sadhukhan
·Feb 24, 2022·

3 min read

Application.properties in spring boot project has all the properties which are necessary for the project to execute. In some of the scenarios we need to update the properties in application.properties fileand in order to reflect the changes we need to restart the server which can cause business related issues e.g., Increases application downtime.

In order to resolve this issue we can update application.properties file without restarting tomcat server and as a result there will be no or less downtime for the application.

Use case scenario:

Create a spring boot project and create an endpoint which will read a property from the application.properties file and will return the value in the application.properties file. We will change the value in the application.properties file and we will expect the new value without restarting the server.

Create a dummy endpoint which will return demo.service.profileProp Value defined in application.properties file.

Read the demo.service.profileProp in controller like below and create an endpoint that will return the value of demo.service.profileProp. So our Rest Controller looks like below:

image.png

Add these 3 dependecies in pom.xml of the spring boot project:

  • spring-cloud-config-client
  • spring-cloud-starter-bootstrap
  • spring-boot-starter-actuator

image.png

Rename application.properties to bootstrap.properties like below:

image.png

Add @RefreshScope to the classes where we have @Value annotaion.

image.png

Create a Config Server.

Config Server is nothing but a simple spring boot application which will read all out configurations from repository. We can create a config server by the following way.

Create a simple Spring boot project and add the following dependency:

  1. spring-cloud-config-server

image.png Update application.properties with the following property.

  1. server.port=8888
  2. spring.application.name=config-server
  3. spring.cloud.config.server.git.uri=https:github.com/lazycoderextreme/ConfigRepo

Note. Add your own repo url where you proerties are set for spring.cloud.config.server.git.uri. To how how to add properties check below.

image.png

Add @EnableConfigServer on top of main class of config server. Like below:

image.png

Create a Configuration Repo in Github and add below files according to spring profiles. We will create 3 profiles:

  1. dev
  2. qa
  3. prod

So we will create 4 properties file and the file name will start with the name of application. So the files are:

  1. demo-service.properties

image.png

  1. demo-service-qa.properties

image.png

  1. demo-service-prod.properties

image.png

  1. demo-service-dev.properties

image.png

Every file starts with demo-service, hence we will add the name 'demo-service' in the bootstrap.properties of our demo-service spring boot project. Hence the bootstrap.properties will look like this:

image.png spring.application.name: name of out spring boot application spring.cloud.config.profile: which profile we need to set. e.g., dev, qa, prod etc spring.cloud.config.uri: config-server uri management.endpoints.web.exposure.include=refresh: this is actuator property which will help us to refresh the properties without restarting the server.

Now start the config server and demo-service. Execute: localhost:9999/profile o/p: prodProfile

image.png Now go to bootstrap.properties and set spring.cloud.config.profile=qa (earlier the value was prod). Save the bootstrap.properties file.

Execute: localhost:9999/actuator/refresh Request Type: POST (No Body)

image.png We will get what are the properties updated without restart. To Test we will again: Execute localhost:9999/profile o/p: qaProfile

image.png

Find the code in github: github.com/lazycoderextreme

If you like this article then please react it and feel free to buy me a coffee: buymeacoffee.com/lazycodder