Accessing Centralized configurations in Spring Boot
When we talk about microservices architecture, we don’t just say that we should have our complete functionality divided into smaller services and then these services can communicate to each other through some mechanism, what we actually mean is having a centralized configuration , service registry to mange the services and other concepts to implement 12 factor methodology.
In this post we will be discussing about Centralized Configuration, creating a Centralized configuration server and some services that will use this config server to get the properties.
So, what was the need to create a common config server, all the services can get the properties from. Below are some of the points that suggest us to go for centralized, version controlled config directories and these are taken from the book Cloud Native Java by Josh Long.
- Changes to an application’s configuration, as configured, would require restart.
- There is not traceability: how do we determine what changes were introduced into production and, if necessary, roll back those changed?
- Configuration is decentralized; it’s not immediately apparent where to go to change what.
- There is no out-of-the-box support for encryption and decryption for security.
Creating a config server
To create a config server, we will have to include below dependency in out projects pom.xml (if maven is being used) and we are good to create the configuration directory
once you have added above dependency in your project you can install the spring cloud config server by annotating your SpringBootApplication class with the annotation @EnableConfigServer
Once you have annotated the mail class with this annotation you will have to tell this server, the repository where your properties are located. This directory (repository) can be on any remote location accessible via HTTP or in your local machine. You can optionally change the port where this config server will running at, by default the port will be 8080 but conventionally it should be 8888. To do the above mentioned things we will have to change our application.(properties/yml) file as below
the property server.port will decide the port this server will be running on and the other property will tell the repository where all the configuration properties are stored at, in this example the repository is my local machine by it can be any versioned repository.
This directory springcloud-configserver will store the properties file of all the services that we have in our application, lets take an example that we have a service named one-service and want this service to access the properties from config server. What we can do is, we can create a file named one-service.properties and this file will be used by another service named one-service. Create the file and enter any key vale pair that we will access from another service i.e. one-service. Lest say we created a file named one-service.properties and entered below value in it, we will use this value in a config client later
Now run the config server and access the URL http://localhost:8888/one-service/master
and you will be able to see the configurations that are inside the file one-service.properties.
Creating Config Client
To create a client (service) that will use the properties that we have configured in Config Server we will have to add below dependency to make the project config client
and this client will look for the config server at the default location http://localhost:8888 and one more thing we will have to change the name of this application to one-service so that it can fetch the values from the properties file one-service.properties that is in the versioned directory springcloud-configserver (configured in config server).
To use the value that is present in the one-service.properties we have to user @Value annotation and and with the name of the key that is there in the properties file.
Lets say we wrote below controller
and access the URL http://localhost:8080/hi we will be able to see the value of the databaseurl property that is configured in the config server.