Microservice Configuration
Spring Boot
Parameters like password, db connection data, dev parameters, test parameters etc should be maintained in a separately called configuration. This is possible by maintaining .yml file or .properties file.
.properties
file contains the key value pairs and are accessed in the microservice by @Value(“$key”) annotation before a variable declaration..yml
file also stores key value pairs. It can have tree structure to avoid long prefixes.@ConfigurationParameter(“prefix”)
annotation is used to fetch all the parameters in config file with that prefix and stores in variables of a bean class.- But, all these go to the jar file, so config is not externalised. We can achieve externalisation by having a copy of the config file at the same directory of jar. This will override the internal config file. We can mention the parameters as command line arguments.
- application-test.yml goes to test env, application-dev.yml goes to dev env otherwise takes the default.
- Commit the changes in config file to git for version management or we can use SVN or HashiCorp.
- Real time monitoring is done by Spring Cloud Config Server and Spring Cloud Config Client. Add the dependency and arcade dependency too.
- Spring Cloud Config Server will have interact with git and fetches the latest config properties and exposes an endpoint
http://localhost:8200/application/default
orhttp://localhost:8200/application/dev
orhttp://localhost:8200/application/test
to view the parameters depending on the environment. We need to mention the port in this microservice. - Spring Cloud Config Client must be added to pick up the changes from Spring Cloud Config Server. It exposes /refresh endpoint. After making a request to this endpoint, we can have real-time update in parameters in the application without restart.