Microservices breaks our system into small independent components. Spring boot has the features that are useful to build the microservices quickly. Through spring boot, we can configure the microservices quickly. This article will help you in building the microservices with spring boot.
Want to Get Certified in Spring Boot? Then visit here Spring Boot Training
In the blog, we will discuss the following topics:
- What is Microservices
- Principles of Microservices
- Microservices Architecture
- Why Spring Boot
- Building Microservices with Spring Boot
What is MicroServices
Microservices is a structural approach that constructs the application as a group of independent services, formed about an enterprise domain. Microservices architecture combines microservices to build a big service.
Principles of Microservices
1) Modeled Around Business Domain
Microservice does not limit itself from embracing the suitable database or technology stack. Database or Stack is useful for resolving the enterprise objectives.
2) Deploy Autonomously
Microservices are platform-independent. We can deploy and design them without impacting other services.
3) Isolated Failure
Any Big Application can persist unimpacted throughout the breakdown of one module. We have to detect the failure quickly because a service can fail at any time.
4) Single Responsibility Principle
This principle says that a module or a class of a program must have just one responsibility. A microservice can serve only one responsibility.
5) Infrastructure Automation
Infrastructure Automation is a mechanism of scripting the environments. Through the Scripting environment, we can use a similar configuration for one node or multiple nodes. This mechanism is known as configuration management or system configuration management.
Following are the essential points of Microservices Architectures:
- Every microservice will have an individual database.
- Client API will not have access to the services. It can only communicate with the API gateway.
- We will register every service on the discovery server. The discovery server contains the information of every microservice that exists in the system.
- The configuration server stores all the configurations of the microservices, and this server is useful for the users to get the configuration information such as URL, hostname, etc.
Why Spring Boot
Spring Boot allows developing production-ready apps and gives the following non-functional characteristics:
- It assists in controlling multiple components.
- It assists in setting the components explicitly.
- It provides embedded servers that are simple to implement with containers.
Challenges in Microservices Architectures
While designing the microservices may look simple, but many difficulties are related to the microservices architectures. Following are the challenges in Microservices:
Perceptibility: We have many small components for implementing and handling, which becomes tough to detect and control the problems. It needs huge perceptibility about the components.
Components automation: It is complex to automate the components because there are many small components in place of a monolith, i.e. Deployment, Builds, Monitoring, etc.
Debugging: It is complex to study every service because of an error. Centralized Dashboards and Logging are important for making it simple to debug the problems.
Consistency: We do not have various tools to solve a similar problem. While it is essential to promote innovatively, we should also have distributed administration around platforms, languages, tools, technologies, etc.
Configuration Management: It is very important to preserve the configurations of the components throughout the different environments.
So, to avoid the above challenges, we build the microservices with Spring Boot. Spring boot has the features to build the Microservices with minimum configuration.
Building Microservices with Spring Boot
1) Eureka Server
Eureka is a service registry or naming server. Eureka server gives name to every service. It is a very important service because:
- In eureka server, there is no need for hardcoding the microservices IP addresses.
- It is useful when the services utilize dynamic IP addresses, i.e., autoscaling.
So, every service should register in Eureka Server, and notify the Eureka server that it is active. If the eureka did not get any notification from a service, then that service is unregistered in the eureka server.
We require the following tools to developing the Eureka Server:
- Spring Tool
- Java 8
- Eclipse IDE
After Installing the above tools, we can develop the Eureka server.
Creating Eureka Server
To create the Eureka Server, first, we must create the “EurekaServer” project in the Eclipse IDE. To create the “Eureka Server” project, we press “Spring Starter Project”, and after that, we have to press “Next”. We can name the Spring Starter Project with any name.
In the following screenshot, we can see the creation of a new “Spring Project”.
Name the Spring Starter project as “Eureka Server”.
After that, choose “Eureka Server”as the dependency and press “Finish”.
Subscribe to our youtube channel to get new updates..!
Change the “application.properties” file for adding the port number and disable the registration.
Open “EurekaServiceApplication.java” and include “@EnableEurekaServer” above the “@SpringBootApplication”.
This notation configures the registry that enables the other applications to interact.
For starting the application, Right-click the Project/Run as/ Press “Spring Boot Application”.
When we open “http://localhost:8761”, the Eureka server will be opened.
2) Image Service
Eureka Client Service is an autonomous service in the microservice architecture. The client service can be account, payment, auth, notification, config, etc. Image service is a data source for the images, and every image will have a title, id, URL, etc. For pom.xml, in place of Eureka Server, we use Eureka Client.
In the below screenshot, we can see pom.xml code.
In “application.properties” file, we specify the configurations.
In the below screenshot, we can see the “application.properties” file code.
After that, activate the eureka client through “@EnableEurekaClient” Annotation.
Program for activating the eureka client:
As the image service reveals the data through the endpoints, we have to establish a controller, and specify the action methods.
Program for Action Methods:
3) Gallery Service
Eureka Client service is also known as REST Client that invokes the REST API Services(other services) in the microservice application. Therefore, for instance, the gallery service invokes the image service for getting a group of images, or only images that we generate in a particular year.
We can invoke the other services from the REST Client through:
REST Template: It is an object that sends requests to the REST API Services.
FeignClient: It is a client that gives an approach for the RestTemplate.
After that, we open “pom.xml”. It is similar to the image service. After that, we open the “application.properties” file. In the main application class of spring boot, along with activation of the eureka client, we have to generate the bean to “RestTemplate” for calling the image services.
Program to create a bean:
In the controller, we call the image service through “RestTemplate” and return the result.
Program for calling image service:
We can use the service name in place of “localhost: port”.
When we have to call the service from the browser, we cannot do that through the service name like gallery service. So, for calling the services from the browser, we use the gateway.
The gateway acts as an entry point to the system that we use for handling the requests by directing them to the equivalent service. We can also use it for monitoring, authentication, etc.
Zuul gateway is a gateway or a proxy, and it is a middle layer between our services and users. Eureka server solves the problems of hardcoding the IP addresses. We can run more than one service concurrently. Therefore, zuul:
- Balances the load between the services instances that are running in various ports.
- Maps between the prefix path “/gallery/**” and the service. Through eureka server for directing the requested services.
- We can filter the requests and include authentication.
Add dependencies like Eureka Client, Zuul to the “pom.xml” file.
In the following screenshot, we can see the “pom.xml” file code.
Activate the Eureka Client and Zuul.
Program for activating the Zuul and Eureka Client
4) Authentication(Auth) Service
Following is the Authentication Workflow:
- The Client sends his credentials for requesting a token .
- The Server verifies the credentials and returns a token.
- For every request, the user provides a token and server validates that token.
For validating the user credentials and issuing the tokens, we launch a new service called “Auth Service”. Gateway invokes the auth service for validating the tokens.
JWT(JSON Based Token)
A Token is a string that is created by the application and forwarded by the user with every request to enable the access to the resources that are uncovered by the application.
JWT is a JSON based open paradigm that we use to create the access tokens. It contains three sections:
The header section comprises a hashing algorithm
Payload section comprises attributes and their values
Signature is the muddling of: Header + “.” + payload + secret key
In the gateway, we do the following things:
- Validate the tokens for every request.
- Avoid all the unauthorised requests for our services.
Add JWT dependencies and Spring Security to “pom.xml” file.
In the following screenshot, we can see the “pom.xml” file code
After that, add the paths to the auth service in “application.properties” file.
For defining the security configurations, we have to create the class and comment with “@EnableWebSecurity”. After that extend the “WebSecurityConfigurerAdapter” for overriding and providing custom security configurations.
Spring Contains filters that get executed in the request life cycle. To activate and use the filters, we have to extend the filter class. Generally, springs find out when we should execute the filters.
Now, we have to implement the filters for validating the tokens. In the below example, we are using “OncePerRequestFilter” Filter. It assures one execution per request.
Testing the Microservices
After developing the microservices, we have to test the services. So, we have to run the eureka service and other services like image, auth, etc. we need to check whether every service is working properly or not.
Building microservices with spring boots help the users to use the microservices efficiently. Spring boot makes the microservices work together with minimum configuration. I hope this article provides you with the required information about developing microservices with spring boot.
If you have any queries, let us know by commenting in the below section.