Spring Cloud Config
Where do you store your configuration file when you use microservices?
Spring Cloud Config is a centralized place to store the configuration file for all microservices.
-
In a monolithic application, we can store the configuration file in the
resourcesfolder. -
In a microservice application, we can store the configuration file in the
resourcesfolder of each microservice. However, this is not a good idea because we have to update the configuration file in each microservice when we want to change the configuration file. -
We can store the configuration file in a centralized place. So we need Spring Cloud Config.
How to use Spring Cloud Config in Spring Boot?
To Create a server:
- Set up configuration repo: choose a version control system like Git and create a new repo to store the config
- Create a Spring Boot application and add spring-cloud-config-server dependencies.
- Add
@EnableConfigServerannotation to the main class. - Add
application.propertiesfile and set up the port and the config repo url.
To Create a client:
- Create a Spring Boot application and add spring-cloud-starter-config dependencies.
- Add
bootstrap.propertiesfile and set up the port and the config server url. - Add
@RefreshScopeannotation to the main class. - Add
@Value("${property.name}")annotation to the field that you want to inject the value from the config server.