Configuration Server and Client application in Micro Service Architecture
In this tutorial we are going to discuss, how to setup configuration server using GitHub in Micro Service architecture.
First we start with the reason why we should use configuration server in MSA.
Main benefit which we will achieve is separation of configuration from the codebase. In JAVA application if you want to change one property value in the property file, so to reflect that change into your application, you’ll need to build and deploy the application again.
But by using Configuration server, there will be no need to build and deploy application again, you can just refresh the context and property changes will be reflected to the corresponding environment.
To start with configuration server we need to create at least two spring boot application.
1- Configuration Server
2- Configuration Client
For this tutorial you will need a GitHub Account, to create new account you can click here.
Configuration server application
We will be using spring Initializr for creating our server application.
After selecting these config server and actuator dependencies for this application click on generate and import it as maven project into your workspace.
Use @EnableConfigServer annotation on your main class for the application.
Add following properties in your application.properties file.
server.application.name=config-server
server.port=8888
spring.cloud.config.git.uri={{GITHUB_REPOURL}}
spring.cloud.config.git.username={{YOUR_GITHUB_USERNAME}}
spring.cloud.config.git.password={{YOUR_GITHUB_PASSWORD}}
GITHUB_REPO_URL should be something similar to https://github.com/{{GITHUB_USER}}/{{REPO_NAME}}.git
Now you’re all setup with your configuration and you can start your application.
Configuration client application-
Actuator and Config Client (spring-cloud-starter-config) dependencies to add in project.
Changes in application.properties
spring.application.name=config-client
spring.config.import=optional:configserver:http://localhost:8888
management.endpoints.web.exposure.include=*
You’re all setup the client application as well for using properties from GitHub now.
Accessing the properties from config server
To access property files from GitHub we need to follow certain conventions as follows —
To access property file our config-client application where server is running on port 8888
http://localhost:8888/config-client/dev1, for this to work we need to create config-client-dev1.properties in our GitHub Repository.
config-client-dev.properties — http://localhost:8888/config-client/dev
config-client-test.properties — http://localhost:8888/config-client/test
config-client-prod.properties — http://localhost:8888/config-client/prod
Now as we setup both server and client application for our architecture. Now we need to check how can we change property value in GitHub repo and those changes start reflecting in our client application without restarting application.
Refresh property from the config server
Any client application will fetch the properties from config server at the start of application and now if we update any property in GitHub repo, we need some way to trigger that change to all the clients of configuration sever so that they can grab this updated property. We will discuss about two ways to trigger that for client application
1- Manual by calling actuator endpoint
2- Spring cloud bus
We will discuss first way in this topic here-
There are actuator endpoint available for this refresh to take place. We can use actuator endpoint for this — /refresh from client application using the POST method for this. (you can use POSTMAN client application)
http://localhost/actuator/refresh — Method POST
Once you hit the /refresh endpoint , all the beans annotated with @RefreshScope would be refreshed with the updated value from config server.
Second method is more convenient for real time applications, because there can be many client application and its really hard to hit all client’s refresh endpoints.
We’ll discuss that second way of refreshing property in another topic. You can find Spring Cloud Bus way for refreshing properties by clicking here.