Managing Autoscaled EC2 instances with Ansible and Jenkins

When running EC2 instances through the autoscaling group or by ECS (Elastic Container Service -EC2 type) we must keep our instances updated with the latest packages and security updates. Using…

Smartphone

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




How to implement Microservices using Spring boot and java

Microservices with Spring boot

This article is a continuation of my previous article on how to design the microservices, I will explain how we can build micro services using spring boot, eureka server and java and will fit with design principles of microservices.

Consider we got a requirement to build a web app or mobile app to rate different restaurants. The requirement is analyzed and the product owner and tech architect suggest a solution to build this with different microservices.

Project Name: Restaurant Rating App
Technologies:
1. Any Front end technologies (Vue.js, Angular or React etc.).
2. Middleware or Backend technology (Java based Microservices).
3. Any databases system.

Restaurant rating application Architecture
Restaurant rating application architecture

In this article I will not focus on end to end integration. I will focus only on building the microservices using spring boot and eureka discovery server.

JSON Representation:
{
“RestaurantId”: “xxxx”,
“RestuarantName”: “xxxx”,
“RestuarantDescription”: “xxxx”
}

2. Restaurant rating service- This service designed to provide the restaurant rating details such as Restaurant Reference Id and Restaurant Rating.

JSON Representation:
Get the Rating based on Restaurant Id
{
“RestaurantId”: “xxxx”,
“RestaurantRating”: xxxx
}

3. Restaurant Catalog service- This act as endpoint micro service which calls rating and information service and pass on the required details to front end system via eureka server. This service should hold the collection of different restaurant details such as Restaurant Catalog Name, Restaurant Catalog Description and Restaurant Catalog Rating based on user login.

JSON Representation:
[
{
“RestaurantCatalogName”: “xxxx”,
“RestaurantCatalogDescription”: “xxxx”,
“RestaurantCatalogRating”: xxxx
},
{
“RestaurantCatalogName”: “yyyy”,
“RestaurantCatalogDescription”: “yyyy”,
“RestaurantCatalogRating”: yyyy
}
]

Team allocation to build the Microservices:
Team 1
- To build eureka discovery server application which register rest other services.
Team 2- To build the restaurant information service.
Team 3- To build the restaurant rating service.
Team 4- To build the restaurant catalog service.

In the below example I used spring-boot, eureka discovery server and java. But in real time the microservices can be built with different programming languages and get it used in application. For instance, I have planned to build 10 microservices for an application but as an architect I know some module can be easily implemented in python than java, I will allocate those to separate team and get it implemented with python and use in the application. This is one of the main advantages of using the microservices.

Note: The screenshots are with eclipse IDE, it can be done in IntelliJ as well. (Pre-requisites: Should know how to build spring boot application)

1. Create a spring-boot application for this service:

This application act as eureka discovery server, this will act as service Registry. In this example we will build the mentioned microservices and get it registered in the server.

Discovery server application-Spring boot

2. Configuration (pom.xml):

Dependency management entries in pom.xml

3. Properties file:
The below details need to be given for discovery server else the spring-boot consider discovery server also as a client.

application.properties entries for discovery server

1. Create a spring boot application for this service:

Restaurant Information service -spring boot application

2. Create a model class:
The model class holds the below data. It contains the getter and setter method.

1. Restaurant Id.
2. Restaurant Name.
3. Restaurant Description.

Restaurant Info model class

3. Create a controller class:
The controller class is responsible in handling the request from catalog service and returns the details as per entity class. It contains the request mapping details.

Restaurant Information Controller Class

4. Configuration (pom.xml):

Pom.xml entry for Eureka client and spring cloud version
Dependency management entries in pom.xml

The same configuration entries used in other microservices also (Rating and Catalog services)

5. Properties file:
The below details should be given for client services.

Note: The @EnableEurekaClient annotation and configuration entry in pom.xml indicates “Boss I am a client service please inform my discovery server”

1. Create a spring boot application for this service:

Restaurant rating service -spring boot application

2. Create a model class:
Rating model class:
The model class holds the below data. It contains the getter and setter method.
1. Restaurant Id.
2. Restaurant Rating.

Restaurant rating model class

I created another entity class called “User Rating”. This class holds all the rating data. I will explain why I used this class in the catalog service.

User rating model class:
The model class holds the below data. It contains the getter and setter method.
1. User Rating (contains list of rating).

3. Create a controller class:
The controller class is responsible in handling the request from catalog service and returns the details as per model class. It contains the request mapping details.

Restaurant rating Controller Class

4. Configuration (pom.xml):

Pom.xml entry for Eureka client and spring cloud version
Dependency management entries in pom.xml

5. Properties file:
The below details should be given for client services.

1. Create a spring boot application for this service:

Restaurant catalog service -spring boot application

2. Create a model class:

The model class holds the below data. It contains the getter and setter method.
1. Restaurant Catalog Name.
2. Restaurant Catalog Description.
3. Restaurant Catalog Rating.

3. Create a controller class:
The catalog controller service is an endpoint service and returns the complete restaurant details.

Restaurant catalog Controller Class

4. Configuration (pom.xml):

Pom.xml entry for Eureka client and spring cloud version
Dependency management entries in pom.xml

5. Properties file:
The below details should be given for client services.

The Eureka server application need to be started and it runs in the port 8761 as mentioned in properties file above .

When the instances are active it gets displayed in discovery server dashboard.

Screenshot: No instance available because none of the service started

Screenshot: Instance details when the service is started

Eureka Server dashboard when instance are running

Note: The instance names are retrieving from properties files.

Technical Points:

1.To communicate with other microservices, I used “@RestTemplate” which executes synchronous HTTP requests. In future the spring use “Webclient” which is capable of handling asynchronous requests. I am not emphasizing on webclient in this article as this article is for microservices.

2. I used UserRating model class which takes list of rating details, when building microservices just have it mind to return the data as an object class instead of returning list. So it makes the life simpler when consuming the service.

2.a. The restTemplate.getForObject(serviceURL, Object.class) takes the responseType as object it is better to return the response as object class,by doing this the data access will be easy.
2.b. Technically yes you can pass the list as a response type in the services and retrieve but need to do lot of manipulation instead of doing the manipulation I have wrapped with another model class which holds the list.

3. In the catalog service, I used replica of info and service model class in order to manipulate the data easily.

4. I have hard-coded the data instead of using the database connection. I focused to explain the microservices concept so didn’t focus on end-to-end integration.

Postman results

Restaurant Info Service:

Restaurant Information service response

Restaurant rating Service:

Restaurant rating service response

Restaurant catalog Service:

Restaurant catalog service response

In a nutshell, microservices concept and implementation is simple to understand. Microservices is an architecture, so use it when it’s needed and it should be structured in business perspective as well.

Thanks to javabrains.io, spring.io, spring-boot tutorial, edureka and developer.okta. I referred these articles to construct the above example in a simple and effective way.

I tried my best in explaining “how to build microservices” in one article. Hope it will be helpful for Microservices newbie :)

Add a comment

Related posts:

Allowance is the Powerful Antidote to Judgment

Your orientation in life determines how you see and feel your life experiences. Orientation also affects your choices. Look closely at your beliefs to discover where you are judging others and yourself.

The End of an Era

In the absence of regular school, work, or any rites of initiation or leave-taking, I tracked my life via consoles and the games I played. So even if I couldn’t remember dates, I could say like two…

Superhero vs. Animation

Two genres were consistently at the top of the Hollywood charts throughout 2016, those were Superheroes and Animation. Both released big-name films that brought many profits for their genres. Even…