Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems. Coordination of distributed systems leads to boilerplate patterns, and using Spring Cloud developers can quickly run services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s laptop, bare metal data centers, and managed platforms such as Cloud Foundry.

In this article, I would like to discuss Spring Cloud Config. Now, let us see a simple use case that doesn’t have Spring Cloud Config.

|700x325

Problem Statement

The insurance provider is a microservice that contains rest endpoint i.e /insurance-service/getPlans . And there are many microservice clients such as insurance client 1, insurance client 2, insurance client 3 which are accessing insurance provider as shown above. All the clients are providing the same endpoints to access the insurance provider. Suppose insurance provider later changes its rest endpoint from /insurance-service/getPlans to /insurance-service/getUpdatedPlans . Since all the clients are tightly coupled with the insurance provider, they need to rewrite their endpoints as well and changed them to /getUpdatePlans .

Hence, the changes in the insurance provider will impact directly to the dependent clients. Also, if the clients do the changes then they need to restart and redeploy their server which is a massive and tedious task. So, how can we optimize this and how can we declare the endpoint globally in the insurance provider so that if there are any changes then they will not affect the dependent microservices.

So, let us discuss what is Spring Cloud Config and how can we solve the above problem using Spring Cloud Config.

What is Spring Cloud Config?

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the config-server, we have a central place to manage external properties for applications across all environments.

|700x287

Here, the insurance provider contains the endpoint /getAllPlans . All the clients are interacting with the insurance provider via a config server. This config server is interacting with GitHub repository and the repository is directly interacting with the insurance provider. So, if the insurance provider changes its URL then the insurance provider just needs to update those URL changes in GitHub repository so that all the dependent clients can get the changed URL from the config server.

Now, clients need not be bothered about the changes. With the help of a config server, clients are not as tightly coupled with the insurance provider as before. They are now loosely coupled. Now, we will develop the insurance provider, insurance config server, and insurance client. To reduce the complexity and to make you understand better, we are going to develop only one insurance client as a microservice here.

Note : If you want then you can develop all three clients as microservices and test the solution.

Let us create the insurance-provider using Spring Initializr with Spring Web and Spring Boot DevTools dependencies. Now, create a rest endpoint in the insurance-provider application.

In insurance-provider, create a get method getPlans() which will return the list of plans as mentioned below. The insurance-provider will run on port 8080.

|700x507

Now, create insurance-config-server using Spring Initializr with Spring Web, Spring Boot DevTools, Spring Boot Actuator, and Config Server dependencies. We added Spring Boot Actuator because the endpoint will redirect as we are not directly invoking the insurance provider from the insurance client. We will invoke the config server and the config server will invoke GitHub. So, default endpoint mapping will be done by the Spring Boot Actuator.

|700x242

|700x242

In the application.properties file, we can point to the GitHub URL to fetch the link. The insurance-config-server application will run on port 8888.

Note : You need to specifically provide the git.default-label as main otherwise config server will not be able to find the repository. Git has recently changed its master branch name from master to main but in Spring Cloud Config git.default-label is still master. Hence, it will not be able to fetch the details unless you specify the default-label as main.

|700x96

|700x96

I have created a new GitHub repository insurance-config-server and added the application.properties file that contains the URL.

|700x235

|700x235

Now, create an insurance-client using Spring Initializr with Spring Web, Spring Boot DevTools, and Config Client dependencies.

In insurance-client, we will consume the insurance-provider using the RestTemplate. From the insurance-client, the request will go to the insurance-config-server. In insurance-config-server, application.properties file will fetch the key ${insurance.provider.url} . Based on the key, it will find the appropriate endpoint URL which is mapped. This will invoke the service then we will get the required response.

|700x547

We need to add the below properties in application.properties which mean that insurance-client will interact with insurance-config-server. The insurance-client will run on port 9090.

|478x89

Let us start insurance-config-server, then insurance-provider, and finally insurance-client. Now, you will see that any changes done in URL in insurance-provider will reflect in insurance-client.

|700x320

You can find the complete project in the below GitHub :

https://github.com/ela4490/spring-boot/tree/main/spring-boot-cloud/spring-cloud-config

https://github.com/ela4490/insurance-config-server

This is a very simple example to understand the nuances of Spring Cloud Config. Hope this will help anyone who wants to learn more about Spring Cloud Config. I am just trying to share my experiences during my learning on Spring Cloud. Please feel free to review or correct the details mentioned in this article which will help me to create more valuable content for other developers. I will be creating more articles in future on Spring Cloud and its features with sample applications. Stay Tuned. Happy Learning! :)

Reference :

https://cloud.spring.io/spring-cloud-config/reference/html/

Reference https://medium.com/geekculture/getting-started-with-spring-cloud-config-24b31977bc8