Microservices with Spring Boot - Day5
Microservices with Spring Boot - Day5
Software Required
- Java 8 or later
- Eclipse / STS
- Postman
- MySQL
Spring Boot:
It automates all the generic setup for the applications so that
you can quickly create spring related applications by
concentrating only the application level instead of
concentrating on the infrastructure level
Note: Spring Boot can be used only with build tools like Maven
or Gradle, they will take care of downloading all the libraries for
your project
Project Template
Today’s Agenda
- Configuring the project to use base machine JRE
- Creating an executable JAR & running through command
prompt
- Microservices
Configuring the project to use base machine JRE
1. Setup the version in pom.xml
2. Change the project build path to use base machine JDK
3. Change the compiler version that matches to base
machine JDK
4. Update the project
Microservice:
These are loosely coupled services which are independent from
other services
Benefits
- Services are loosely coupled
- Testing will be easier as we need to only test a particular
service we want
- We can choose any language we want
- We can scale the service which we want
- If a particular service goes down it affects only the
services that are down, and other services will be
available.
Challenges:
1. Expensive - because we need to many resources
2. Important to identify the right approach
3. Adapting to the changes
4. Global competition
Design principles in microservices
1. Service Discovery: Registers the microservices Instance Id
& physical address
2. Discovery Client: These are microservices which locates
other microservices using the instance ID, it must send
acknowledgement to the service discovery frequently
about its health status
3. Client Side Load Balancer: Takes care of distributing the
load across multiple instances of microservices
4. Fault Tolerance - Circuit breaker pattern: It takes care of
breaking the circuit to stop sending the requests to the
already down service
5. Distributed configuration
6. API gateway
7. Distributed log tracing
Note: Since we are using spring cloud & spring boot projects,
we must refer to the spring website to see the version
compatibility
pom.xml
Service Discovery Enabling
application.properties
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
….
}
Dependencies
1. Eureka Client
2. Web
3. Dev tools
Enable the microservice and Ribbon load balancer in the
application
WalletService.java
Bind the RestTemplate object here & call Account Microservice
using RestTemplate & initialize the Wallet in any one method
Now you can call this intializeWallet from the controller
application.properties
Now you can run this project & send request to wallet/balance
Creating another instance of account microservice to verify the
load-balancer job
pom.xml
Steps to implement to apply the circuit breaker to avoid fault
tolerance
1. Adding the circuit breaker to a method that is calling the
remote service, the circuit breaker must have a name that
is used to apply the properties in application.properties
2. Creating a fallback method for the remote service calling
method
3. Configurating the circuit breaker properties in the
application.properties
Ex:
@CircuitBreaker(name = “walletInstance”, fallbackMethod =
“fallback”)
public Wallet initializeWallet() {
}
public Wallet fallback(Throwable t) {
}
application.properties
resilience4j.circuitbreaker.instances.walletInstance.ring-buffer-
size-in-closed-state=10
resilience4j.circuitbreaker.instances.walletInstance.failure-rate-
threshold=50
WalletService.java
application.properties
Agenda
1. API Gateway - Zuul - pre & post processing filters
2. Security
3. Deploying microservices on cloud
4. Transaction management is done in Microservices using
saga pattern
5. Testing
API Gateway:
It is a common entry point for all the microservices including
the service discovery, you can do reverse proxy with API
gateway i.e., client would use a different URL & api gateway will
route the request to different URL/services
API gateway provides filters that can intercept the request to
perform pre & post processing
1. Pre Filters: run before the request is routed to the service
2. Route: This routes the request to the service after pre filter
3. Post Filters: run before sending the response to the client
In Spring Cloud we have a library called Zuul which is used to
create API gateway
We need another library called spring cloud starter Netflix zuul
which you can get it from the maven.
Note: Setup the project to use 2.3.6.RELEASE & Hoxton.SR9
We need to use
1. Actuator : to get the routes endpoints
2. Zuul: to add reverse proxy server
Steps:
1. Send application & user credentials to the authorization
server
http://localhost:7777/oauth/token to get the token
2. Copy the token in some place & send this token while
accessing microservice
Use this token in the postman header & send request to the
microservice with GET, POST, PUT & DELETE and see who all
can access different HTTP methods.
Sending request with the token