JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Software Development Team? Take two weeks Trial! Try Now

How does Anatomy of Spring cloud config work?

Introduction to Spring Cloud:

Anatomy of Spring cloud config work

Spring Cloud: Spring cloud libraries are intended to be used by spring boot applications. They are not intended to be utilized using just the spring framework although it is possible. All of the dependency management is based on spring boot. Most importantly the startup process of the application is usually altered slightly, and the application context is not created in the same straight forward way as a normal spring framework-based application. I am not going to the details here about the spring framework, but I am completely focusing on the spring cloud configuration and service discovery.

Spring Cloud Dependencies Walk Through:

Spring Cloud Configuration

This module is about centralized versioned configuration management for distributed cloud-based application. Now, I am going to explain what Spring Cloud Config is and how we can build and run our own spring cloud config server is. Also, I am going to explain how you can establish your own repository for holding configuration information.

Application Configuration:

As you know that Applications have connections to resources like databases or message queues or email servers and also, they need to call other applications. For example, they might need to call web services. Now usually when we have credentials for databases or URLs that connect it to specify message queues and such then we usually do not hardcode that in our code. So, for this we use external configuration to dynamically adjust the application behavior to describe where the resources are located and how to connect to a database.

Different configuration options available:

Challenges for application configuration:

Desired Solution for Configuration Management:

These are the things exactly that the Spring cloud is going to come in.

Configuration Management in Spring Cloud

Demo on Spring Cloud Config

The Server Side configurations and code:

The Client-Side configurations and code:

Note: If there are no active profile, then it will take the default profile. Spring clients do this automatically on startup.

Environment Repository Example:

Let’s assume that we have a client application and its name is “cloud-demo” and the profile name is “dev”. So, the file name will be “cloud-demo-dev.yml”. Now, when the spring cloud config server consults the file in the repository, it will look for files that match the application name and the profile specified.

Note: In YAML format of the file, it can hold multiple properties within a single file. If we use properties file, then for two different profiles, we have to create two properties file but the same can be done in the same yaml file.

How Properties work in Spring Application: Every Spring application context has an “Environment” object in it that is populated by the container. Environment object contains multiple “PropertySources”. Propertysource is nothing but a source of property that came from somewhere. Now Spring application context will automatically populate property sources from the environment variables where an app is running and from the system properties that we specify when we launch the app. Also, we can specify our property source to pick up the external property files.

When the spring app starts up and it uses spring cloud config on a client-side, then the library simply alters the set of behaviors just to add another property source and it populates that property source by connecting to the server over HTTP using the below URL and getting the response back and populating the property source :

http://<server>:<port>/<spring.application.name>/<profile>

Properties that are described by the server become part of the client application environment.

Note: Spring cloud server exposes properties over a restful interface using HTTP. It is reasonably easy to make this call to the server from any application.

What if Config server is Down?

In Microservice Applications, when we have different Microservice, talking to each other and if config server is down, then this will create a big problem. Actually, Spring cloud config server is recommended to be run in multiple instance. So, if we are running on cloud foundry or AWS, then we will have several instances running behind a load balancer.

Client application can specify the ultimate policy of how to handle the missing config server. There is a setting called “spring-cloud .config.failFast=true”. The default value is false. The default is that we do not want to fail. The client does not want to fail. And if we do not want this behavior then we can set failFast to true and this will make the client not to run without config server being provided. Config server setting overrides any local settings that we have. We can provide local fallbacks for our settings then we can have the config sever override this.

Read more - How to Setup SPRING CLOUD CONFIG

Demo on Spring Cloud Config Server and Client

Config Server creation:

Note: You can create “application.properties” file also. Either of these is fine. I have set the “server.port” to 8001.

application.yml file

Anatomy of Spring cloud config work

We can test our application by running the application. Open up your browser and hit the URL “http://localhost:8001/demo-client/default/”. You will be able to see the JSON response in the browser i.e. actually used by Spring. If any issue, then you should check your project setup.

Config Client creation:

Note: The server.port config value can be specified in either file (the application.yml or bootstrap.yml), but the URI to the config server must be specified in bootstrap.yml as it affects the startup sequence

Now, we have to create a Rest controller class (“MyController.java) to obtain the key-value pair that we have specified in our demo-client.yml file. In this class, I have a Get mapping method named showWord() which will fetch the value of the “country-word” key.

MyController.java

Anatomy of Spring cloud config work

Now, we can verify our rest end-point by accessing the below URL to get the “country-word” value displayed in the browser. Also, you can test in your postman

URL: http://localhost:8002/country-word

Configuration with Spring Profiles:

Note: Without updating our bootstrap.yml file with adding the “dev” profile, we can achieve the same result by just running our application with the below command-line arguments

“-Dspring.profiles.active=dev”

Summary: So, you noticed here that the client needed some dependencies for Spring Cloud that Java programmer provided in our application’s pom file and in our bootstrap file, only we specified the URI of the Spring Cloud server. No java code we had to write for this. So, how easy it is to configure without writing any code for this. This Springe cloud config server is a powerful feature and is more helpful in Microservice oriented applications.

Conclusion

Here, I have explained to you about the Spring cloud configuration with an example. But more advanced concepts I will be explaining in our other blogs with how Spring Cloud Bus and refresh scope can be used to dynamically propagate changes in the scenarios where we change a property after client applications have already started. It is very natural that behind a load balancer, we can run multiple instances of the config server in multiple zones.

References

Some of our clients

team