Refresh application.properties in Spring Boot without restarting tomcat server
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:
Add these 3 dependecies in pom.xml of the spring boot project:
- spring-cloud-config-client
- spring-cloud-starter-bootstrap
- spring-boot-starter-actuator
Rename application.properties to bootstrap.properties like below:
Add @RefreshScope to the classes where we have @Value annotaion.
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:
- spring-cloud-config-server
Update application.properties with the following property.
- server.port=8888
- spring.application.name=config-server
- 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.
Add @EnableConfigServer on top of main class of config server. Like below:
Create a Configuration Repo in Github and add below files according to spring profiles. We will create 3 profiles:
- dev
- qa
- prod
So we will create 4 properties file and the file name will start with the name of application. So the files are:
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:
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
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)
We will get what are the properties updated without restart. To Test we will again: Execute localhost:9999/profile o/p: qaProfile
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