I lived in Han Yang by a nearby lake.

I lived in Han Yang by a nearby lake., a Medium series by Miki Han

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Getting started with Spring Cloud Config

How configuration-related changes will be reflected without restart and redeploy your application using Spring Cloud Config

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.

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.

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.

Problem solved with Config Server

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.

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.

InsuranceProviderApplication.java

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.

InsuranceConfigServerApplication.java

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.

application.properties

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

insurance-config-server GitHub repository

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.

InsuranceClientApplication.java

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.

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.

Postman

You can find the complete project in the below GitHub :

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 :

Add a comment

Related posts:

Live selling effectively in the Philippines

Have you been browsing through your Facebook newsfeed or have been scrolling endlessly in your Shopee app only to find out online sellers conducting live selling? Does this give you an idea to start…

Why Your Business Needs an App

Every business today seems to have a mobile application. Are you considering getting an application but unsure why your business needs an app? Unfortunately, creating a mobile application for your…