100% found this document useful (2 votes)
2K views58 pages

Spring Boot Microservices PPT Notes in PDF

This document discusses building microservices using Spring Boot and Spring Cloud. It describes creating REST APIs using Spring Boot, developing two sample microservices, and implementing synchronous and asynchronous communication between the microservices using RestTemplate, WebClient, and Spring Cloud OpenFeign. It also covers service registration and discovery using Spring Cloud Netflix Eureka.

Uploaded by

prasad velgala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (2 votes)
2K views58 pages

Spring Boot Microservices PPT Notes in PDF

This document discusses building microservices using Spring Boot and Spring Cloud. It describes creating REST APIs using Spring Boot, developing two sample microservices, and implementing synchronous and asynchronous communication between the microservices using RestTemplate, WebClient, and Spring Cloud OpenFeign. It also covers service registration and discovery using Spring Cloud Netflix Eureka.

Uploaded by

prasad velgala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 58

Building Microservices using Spring Boot

and Spring Cloud

Spring Boot REST API’s


1. Create Spring Boot REST API Basics (Learn
Important Annotations
2. Learn Creating CRUD REST API’s using
Spring Boot

Microservices Architecture using Spring boot and


Spring Cloud

Employee
Service

React
App AP
Department
Gateway Service

Organizatio
Service
I

Choose the compatible version of


Spring boot and Spring cloud

Why Spring Boot and Spring Cloud


are a good choice for Microservices?
What is Spring Cloud
1. Spring Cloud is essentially an implementation of various
design patterns to be followed while building Cloud Native
applications. Instead of reinventing the wheel, we can simply
take advantage of various Spring Cloud modules and focus
on our main business problem than worrying about
infrastructural concerns.
Create Two Microservices
Microservice 1 Microservice 2

Employee Department
Service Service

Import and Setup Two


Microservices in IntelliJ
By Ramesh Fadatare (Java Guides)
Con gure MySQL Database
in DepartmentService
By Ramesh Fadatare (Java Guides)
fi
Create Department JPA Entit
and Spring Data JPA Repository
in DepartmentService
By Ramesh Fadatare (Java Guides)

Create Spring Data JPA


Repository In DepartmentService
By Ramesh Fadatare (Java Guides)
Build Save Department REST
API in DepartmentService
By Ramesh Fadatare (Java Guides)
Build Get Department REST
API in DepartmentService
By Ramesh Fadatare (Java Guides)
Con gure MySQL Database
in EmployeeService
By Ramesh Fadatare (Java Guides)
fi
Create Department JPA Entit
In EmployeeService
By Ramesh Fadatare (Java Guides)

Create Spring Data JPA


Repository In EmployeeService
By Ramesh Fadatare (Java Guides)
Build Save Employee REST
API in EmployeeService
By Ramesh Fadatare (Java Guides)
Development Steps
1. Create EmployeeDt
2. Create Service Laye
3. Create Controller Layer
o

Build Get Employee REST


API in EmployeeService
By Ramesh Fadatare (Java Guides)
Synchronous Communication
1. The client sends a request and waits for a response from the service

2. The important point here is that the protocol (HTTP/HTTPS) is


synchronous and the client code can only continue its task when it receives
the HTTP server response

3. RestTemplate, WebClient and Spring Cloud Open Feign library

Microservice 2
Microservice 1
Http Request

Department
Employee
Service
Service
Http Response

Asynchronous Communication
1. The client sends a request and does not wait for a response from the service

2. The client will continue executing it’s task - It don’t wait for the response
from the service

3. RabbitMQ or Apache Kafka

Message Broker Microservice 2


Microservice 1

Message Message
Department
Employee Queue
Service
Service

Microservices Communicatio
3 Different Ways

Microservice 1
1. RestTemplat Queue Microservice 2
2. WebClien
3. Spring Cloud OpenFeign

Employee Department
Service Service
t

Microservices Communication using


RestTemplate
Make a REST API call from Employee-Service to Department-Service

Microservice 1 Microservice 2

Http Request

Employee Department
Service Service
Http Response

Requirements
1. Consider Employee belongs to department and employee
has a unique department code
2. Change Get Employee REST API to return Employee
along with it’s department in response.

Development Steps
1. Add departmentCode eld in Employee JPA Entit
2. Create DepartmentDto clas
3. Con gure RestTemplate as Spring Bea
4. Inject and use RestTemplate to make REST API call in
EmployeeServiceImpl class
fi
fi
s

RestTemplate class is in maintenance mode


As of 5.0, the RestTemplate class is in maintenance mode and
soon will be deprecated. So the Spring team recommended
using org.springframework.web.reactive.client.WebClient
that has a modern API and supports sync, async, and
streaming scenarios.
Microservices Communication using
WebClient
Make a REST API call from Employee-Service to Department-Service

Microservice 1 Microservice 2

Http Request

Employee Department
Service Service
Http Response

Development Steps
1. Add Spring WebFlux Dependenc
2. Con gure WebClient as Spring Bea
3. Inject and Use WebClient to Call the REST AP
4. Test using Postman Client
fi
y

Microservices Communication using


Spring Cloud OpenFeign
Make a REST API call from Employee-Service to Department-Service

Microservice 1 Microservice 2

Http Request

Employee Department
Service Service
Http Response

Development Steps
1. Add Spring cloud open feign Maven dependency to
Employee-Servic
2. Enable Feign Client using @EnableFeignClient
3. Create Feign API Clien
4. Change the getEmployeeById method to use APIClien
5. Test using Postman Client
e

Service Registry and Discovery


1. In the microservices projects, Service Registry and Discovery
play an important role because we most likely run multiple
instances of services and we need a mechanism to call other
services without hardcoding their hostnames or port numbers
2. In addition to that, in Cloud environments service instances may
come up and go down anytime. So we need some automatic
service registration and discovery mechanism
3. Spring Cloud addresses this problem by providing Spring Cloud
Net ix Eureka project to create Service Registry and Discovery.
fl
.

Spring Cloud Net ix Eureka Server


Spring Cloud Net i
Eruka Server

Employee Register with


Service

Service
Registry

Department Register with


Service

fl
x

fl
Development Steps
1. Create Spring boot project as Microservice (service-registry
2. Add @EnableEurekaServer annotatio
3. Disable Eureka Server as Eureka Clien
4. Launch Eureka Server (Demo
5. Registering Department-Service Microservice as Eureka Clien
6. Run department-service Eureka Client (Demo
7. Registering Employee-Service Microservice as Eureka Clien
8. Run employee-service Eureka Client (Demo
9. Multiple Instances of Department-Service
)

API Gateway
1. API Gateway provides a uni ed interface for a set
of microservices so that clients no need to know
about all the details of microservices internals
2. API Gateway centralize cross-cutting concerns like
security, monitoring, rate limiting et
3. Spring Cloud provides Spring Cloud Gateway to
create API Gateway fi
c

API Gateway
Spring Cloud Gateway

Employee
Service

Client AP
Gateway
Department
Service
I

Development Steps
1. Create Spring boot project as Microservice (api-
gateway
2. Register API-Gateway as Eureka Client to
Eureka Server (Service Registry
3. Con guring API Gateway Routes and Test
using Postman Client
fi
)

What we will build?


1. We can create a Spring Cloud Con g Server which provides
the con guration values for all of our microservices. We use git
as a backend to store the con guration parameters.
2. Next, we con gure the location of Spring Cloud Con g server
in our microservice so that it will load all the properties when
we start the application.
3. In addition to that, whenever we update the properties we can
invoke /refresh REST endpoint in our microservice so that it
will reload the con guration changes without requiring to
restart the application.
fi
fi
fi

fi
fi

fi
Spring Cloud Con g Server

Register as con g client


Employee
Service

Spring Clou
Con g Server
Register as con g client
Department
Service
fi
fi
fi

fi
Development Steps
1. Create Spring boot project as Microservice
(con g-server
2. Register Con g-Server as Eureka Clien
3. Set up Git Location for Con g Serve
4. Refactor Department-Service to use Con g Serve
5. Refactor Employee-Service to use Con g Serve
6. Refresh Use case
fi
fi
)

fi
r

fi
t

fi
r

Refresh Use case


1. Whenever we change con guration le then
we don’t have to restart the microservice and
it’s instance
2. We need to call spring boot actuator /refresh
API to reload the updated values from con g
server
s

fi
fi
fi
Problem using Spring Cloud Config Server
1. In order to reload the con g changes in Con g Client
applications (department-service and employee-service),
we need to trigger /refresh endpoint manually. This is not
practical and viable if you have large number of
applications
2. Spring Cloud Bus module provides a solution
3. Spring Cloud Bus module can be used to link multiple
applications with a message broker and we can broadcast
con guration changes.
fi
.

fi
fi
.

Spring Cloud Bus

RabbitMQ Register as con g client

Subscribe
Employee
Service
Messag
Broker
Spring Clou
Con g Server
Department
Subscribe
Service Register as con g client
fi
e

fi
fi

Development Steps
1. Add spring-cloud-starter-bus-amqp dependency to department-
service and employee-servic
2. Install RabbitMQ using Docke
3. RabbitMQ con guration in application.properties of department-
service and employee-servic
4. Create Simple REST API in employee-servic
5. Change department-service and employee-service properties le and
call /busrefres
6. Demo
h

fi
e

fi
Distributed Tracing
Spring Cloud Slueth
Spring Cloud Slueth Spring Cloud Slueth

Department
API Employee
Client Service
Gateway Service

Span id Span id Span id

Trace -> trace id


Distributed Tracing with Spring Cloud


Sleuth and Zipkin
1. We use Spring Cloud Sleuth for distributed tracin
2. We use Zipkin to visualize trace information
through UI
App name Trace id Span id

Development Steps
1. Implementing Distributed Tracing using
Spring Cloud Sleuth Librar
2. Using Zipkin to Visualize Trace Information
through UI

Development Steps
1. Add dependencie
2. Using @CircuitBreaker annotation to a method
(it is calling to external service
3. Fallback method implementatio
4. Add Circuit Breaker con guration in
application.properties l
5. Restart employee-service and demo
s

fi
fi
e

Retry Pattern Implementation with Resilience4j

Department
API Employee
Client Service
Gateway Service

Retry Pattern

Development Steps
1. Using @Retry annotation to a method (it is
calling to external service
2. Fallback method implementatio
3. Add Retry con guration in
application.properties l
4. Restart employee-service and demo
fi
fi
e

Ports
App name: API-GATEWAY - Port: 9191

App name: DEPARTMENT-SERVICE - Ports: 8080, 8082

App name: EMPLOYEE-SERVICE - Port: 8081

App name: CONFIG-SERVER - Port: 8888

App name: SERVICE-REGISTRY - Port: 8761

Zipkin Server: 9411


Microservices
Microservice 1 Microservice 2 Microservice 3

Employee Department Organizatio


Service Service Service

MySQ MySQ MySQ


DB DB DB
L

Steps to Create Organization Service


1. Create Organization-Service using Spring Boo
2. Con gure MySQL Databas
3. Create Organization JPA Entity and Spring Data JPA Repositor
4. Create OrganizationDto and OrganizationMappe
5. Build Save Organization REST AP
6. Build Get Organization By Code REST AP
7. Make REST API Call from Employee-Service to Organization-Servic
8. Register Organization-Service as Eureka Clien
9. Refactor Organization-Service to use Con g Serve
10. Con gure Spring Cloud Bus and Routes for Organization-Service in API-Gatewa
11.Implement distributed tracing in Organization-Service
fi
fi
e

fi
I

Requirements
Client want’s employee, department and organization details in a
response

Understanding the requirement


Consider Employee belongs to organization and employee has a unique
organization code
Change Get Employee REST API to return Employee along with it’s
organization in response.
.

Frontend React App


Department
Route
Service

axios Employee
React App API Service
Gateway
Organizatio
Service

Development Steps
1. Create React App using Create React App Tool
2. Adding Bootstrap in React Using NPM
3. Connecting React App with API-Gateway
(REST API Call
4. Develop a React Component to Display User,
Department and Organization Details
5. Run React App and Demo
)

Spring Boot Kafka Event-Driven Microservices


Architecture with Multiple Consumers
Microservice 2

order_stock
Kafka Broker OrderEvent
Stock Service
Consume
Group 1
Order Service
Topic
OrderEvent

Consume
Email Service Group 2
Microservice 1
OrderEvent

Microservice 3
r

Spring Boot RabbitMQ Event-Driven Microservices


Architecture with Multiple Queues
RabbitMQ Microservice 2
Queue

order_stock Order
Orde
Order Service Exchange Queue Stock Service
Order

order_email
Order
Microservice 1 Email
Email Service
Queue

Microservice 3
r

Reference/Credit : Microservices Architecture


from Spring of cial website

fi

You might also like