Implementing config server to externalize configuration | SpringBoot Microservices
Step 1 → Our config server will be a microservice with dependencies -
Config Server | SPRING CLOUD CONFIG
Eureka Discovery Client | SPRING CLOUD DISCOVERY
Step 2 → Register the microservice as Discovery Client. For this we have already added dependency “Eureka Discovery Client | SPRING CLOUD DISCOVERY“. We just need to add the configurations for which you can refer to - Implementing Service Discovery Client in SpringBoot | Microservices .
Step 3 → Add @EnableConfigServer to the main Application class which contains main method of our application.
package com.movie_rating.ConfigServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Step 3 → Create a GitHub repository and create properties files inside it. These properties files will hold the externalized configuration for our application.
Step 4 → Add configuration in our ConfigServer microservices so that it can read configurations from the properties files stored in our GitHub repository.
spring.cloud.config.server.git.uri=https://github.com/Prash4nt-K/microservice-config
spring.cloud.config.server.git.clone-on-start=true
Step 5 → Add configurations on the properties files on out GitHub Repository. Since eureka client registration configuration properties are common for all our microservices we can put these in “application.yml” properties file which will also act as the “default profile” for our application.
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
Lastly we can verify if our ConfigServer microservice is working or not by running it and hitting the link - http://192.168.29.101:8084/application/default
Here “application” refers to our application.yml file and “default” refers to the “default profile”. If we wish to check our “prod profile” then the link can me modified to http://192.168.29.101:8084/application/prod .
The benefit that comes with the externalizing configuration using ConfigServer is that we can change the configurations stored on our GitHub without having to restart our application to reflect the changes. Changes gets reflected in real time as me make changes in GitHub files without the need to restart the application.