Updated April 6, 2023
Introduction to Spring Cloud Dependencies
Spring cloud dependency helps us to build cloud applications, we have to add some dependencies in order to make this work. First, understand spring cloud is a framework that helps and provide us various features to developed cloud applications easily, we can build a microservice architecture based application by the use of spring cloud framework, there are no such things as spring cloud dependencies, the core concept is cloud framework, and in order to use this framework inside our application, we have to some dependencies provide by them, that we generally do in many areas where we want to implement the specific functionality to our application. So these cloud dependencies allowed us to create the discovery service, client service, gateway, and many more. In the coming section of the tutorial, we will see internally working on this by taking practice examples for better understanding and clarity.
Syntax:
As we have already discussed spring cloud is a framework and we used cloud dependencies inside our application in order to make the cloud application, let take a look at what dependency we need to add in our application for spring cloud see below;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
As you have seen we have to add these inside our application pom.xml file for spring cloud dependencies. In the next section of the tutorial, we will see the internal implementation of the spring cloud for better understanding and usage.
How do Spring cloud dependencies work?
As of now we already know that why we are using spring cloud dependencies to our application, in this section we will first see the internal working than what are different configurations are required in order to make this work, so lets’ get started to see below;
Spring Cloud has three main components which help us to divide our application into the different module so, now we will have details look at each one of them in detail see below;
1) Discovery service: this is the main service of spring cloud, which helps us to discover our different service and let us communicate with one another. As the name suggests it helps to discover the service. It is also known as the registered service, where all other services register themselves in order to get found by it. For this, we have to make few configurations into our application and keep in mind that it will be a separate spring boot project running in the server. We need this to keep track of our registered service and also it does not register itself, in the coming section we will see the configuration required to enable this.
2) Client service: This is also another form of service but this service represents the module from our application which is responsible to perform some business logic and return the result. Basically, this service provides data or we can say actual data for the application. we can have multiple client services in our application and these multiple services will represent the different functionality of the application. We have to register these services with the discovery service.
3) Gateway service: This service act as the entry point for our application, it can contain the common logic for our application or centralized configuration which is needed by all the different services we have. Basically, it does the authentication part for us, we can also place our centralized logging part here, it can be anything depending on the requirement of the application.
Below see the basic flow of the application using spring cloud dependencies:
As you can see in the flow chart we have different layer internally let’s understand the flow in details;
a) First the client will make any request to the server for resource in order to show them to the user on UI,
b) In this setup the request will first go to the gateway which is responsible to perform the authentication on the request we are receiving from the user.
c) If the user is valid then the gateway will map the URL and forward the request to the specified service in order to get the data.
d) If the request or user is not valid then the gateway will simply reject the request, and no further action will be performed to get the data.
As you have seen in the flow chart we can have many different and small service running independently on the server, also we can have separate databases for each of them because service 1 does not depend on service 2 for data. In the coming section of the tutorial, we will see how we can configure it in our application with one simple example.
Examples
1) create the spring boot project from soring initializer and add the required dependency as well. from the below URL: https://start.spring.io/
2) After this import it to your IDE and add the below dependency for creating the eureka server,
e.g. :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3) After this we have to add only one annotation to our main spring application class, to enable the eureka server and we can run the application to see the output:
e.g. :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication{
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4) Add the below configuration to the application file so the register will not register itself to the service, see below;
We are using .yml file to make the configuration so syntax will be but different for this; for reference, I am attaching the code
e.g. :
server:
port: 8989
eureka:
client:
register-with-eureka: false
fetch-registry: false
5) Run the application and see the logs, as below;
output:
Conclusion
As we have seen, spring cloud makes us developed a microservice-based architecture, and that is very easy to build and deploy. It provides us to monitor our every service, by providing one common UI. It helps developers to keep track of their services. easy to use, handle, and implements as well.
Recommended Articles
This is a guide to Spring Cloud Dependencies. Here we discuss the Definition, syntax, How Spring cloud dependencies work? examples with code implementation. You may also have a look at the following articles to learn more –