0% found this document useful (0 votes)
33 views9 pages

Micro Services

The document compares Monolith and Microservices architectures, outlining their advantages and disadvantages. Monolith applications are easier to develop and deploy but face scalability and reliability issues, while Microservices offer flexibility and scalability but come with challenges like configuration and testing. It also provides a mini project implementation guide for Microservices architecture using various components like Eureka Server, Admin Server, and API Gateway.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views9 pages

Micro Services

The document compares Monolith and Microservices architectures, outlining their advantages and disadvantages. Monolith applications are easier to develop and deploy but face scalability and reliability issues, while Microservices offer flexibility and scalability but come with challenges like configuration and testing. It also provides a mini project implementation guide for Microservices architecture using various components like Eureka Server, Admin Server, and API Gateway.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

+++++++++++++++++++++++

Monlith Vs Microservices
+++++++++++++++++++++++

Application can be developed in 2 ways

1) Monolith Architecture

2) Microservices Architecture

-> If we develop all the functionalities in one single application then it is


called as Monolith Application

++++++++++
Advantages
++++++++++
1) Development is easy
2) Deployment is easy
3) Performance
4) Easy Testing
5) Easy Debugging
6) KT is easy

++++++++++++++
Dis-Advantages
++++++++++++++

1) Single point of failure


2) Whole Application Re-Deployment
3) Scalability ( Increasing & Decreasing resources based on demand )
4) Reliability (Strong)
5) Availability (Zero Downtime)

=> If we develop the functionalities in multiple services/apis then it is called as


Microservices Architecture Based Application.

=> Every Microservice will have its own goal

++++++++++
Advantages
++++++++++

1) Loosely coupled
2) Fast Development
3) Quick Releases
4) Flexibility
5) Scalability
6) No Single Point of failure
7) Technology independence

++++++++++++++
Challenges
++++++++++++++

1) Bounded context (identifying no. of services to develop)

2) Lot of configurations
3) Visibility

4) Testing is difficult

5) Debugging

########################
Microservices Architecture
########################

-> Microservices is an architectural design pattern to develop our applications

-> There is no fixed architecture for Microservices Based Applications

-> People are customizing Microservices Architecture according to their requirement

*********** Let us see generalized architecture of Microservices ***************

1) Service Registry

2) Admin Server

3) Zipkin Server

4) Services (REST APIs)

5) FeignClient

6) API Gateway

-> ServiceRegistry is used to register all our backend services/apis


-> Service Registry will maintain services names, urls and status of each service
-> We can use EurekaServer as a service registry

Note: EurekaServer provided by Spring Cloud Netflix Library

-> AdminServer is used to monitor and manage all our backend services at one place
-> AdminServer will provide user interface to monitor and manage our services
-> Using AdminServer user interface we can access Actuator Endpoints of our
services at one place

Note: AdminServer and Admin Client provided by 'Code Centric' company (we can
integrate with boot)

-> ZipkinServer is used for Distributed Log Tracing


-> ZipkinServer will provide user interface to monitor application execution
details
-> How many services involved and which service took more time to process the
request can be monitored using Zipkin
Note: Zipkin is third party open source server (can be integrated with spring boot)

-> Backend services are nothing but REST APIs (which are also called as
Microservices)
-> Backend REST APIs contains actual business logic of our application
-> One project will have multiple REST APIs in the backend
-> Each Backend api will act as client for Service Registry + Admin Server + Zipkin
Server

-> With in the same application If one backend api communicating with another
backend api then it is called as Interservice communication

-> FeignClient will be used to perform Interservice Communication

Note: Based on requirement our backend apis can communicate with 3 rd party apis
using RestTemplate or WebClient

-> Api Gateway will act as an Entry point for our backend apis
-> It acts as mediator between endusers and backend apis
-> API Gateway contains Filters and Routing
-> Filters we can use to authenticate incoming requests
-> Routing will decide which request should go to which backend api

Note: In previous versions we have Zuul Proxy as API Gateway but now it got removed
from latest version of boot

-> Spring Cloud Gateway we can use as API Gateway for our application

#####################################
Microservices Mini Project Implementation
#####################################

*********** Step- 1) Create Service Registry Application using Eureka Server


*******************

1) Create Spring Boot app with below dependencies

a) 'Spring-cloud-starter-netflix-eureka-server'
b) web starter
c) devtools

2) Configure @EnableEurekaServer annotation at boot start class

3) Configure below properties in application.yml file

server:
port: 8761

eureka:
client:
register-with-eureka: false

4) Run the application and access in browser

URL : http://localhost:8761/

*************************** Step-2 ) Create Spring Boot Application with Admin


Server ************************************

1) Create Boot application with below dependencies

a) web-starter
b) devtools
c) admin-server (code centric)

2) Configure @EnableAdminServer annotation at boot start class

3) Configure Embedded Container Port Number (we can use any port)

4) Run the application and access the application in browser

URL : http://localhost:port/

*************************** Step-3) Download & Run Zipkin Server


*****************************************

1) Download zipkin jar from below URL

URL : https://search.maven.org/remote_content?g=io.zipkin&a=zipkin-
server&v=LATEST&c=exec

2) Run the zipkin server jar file using below command

$ java -jar <zipkin-server-jar>

************************** Step-4) Develop REST API (WELCOME API)


**********************************

1) Create boot application with below dependencies

- eureka-discovery-client
- admin-client
- zipkin client
- sleuth (It is for logging)
- web-starter
- devtools
- actuatoR

2) Configure @EnableDiscoveryClient annotation at start class (It will search and


register with Eureka)
3) Create Rest Controller with required methods

4) Configure below properties in application.yml

- server port
- admin server url
- actuator endpoints
- applicaion name

---
server:
port: 8081
spring:
application:
name: WELCOME-API
boot:
admin:
client:
url: http://localhost:1111/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
...

5) Run the application and check Eureka Dashboard, Admin Server Dashboard and
Zipkin Dashboard

*************************************Step-5) Develop REST API (GREET


API)**********************************************

1) Create boot application with below dependencies

- eureka-discovery-client
- admin-client
- zipkin client
- sleuth (It is for logging)
- web-starter
- devtools
- actuator
- feign-client

2) Configure @EnableDiscoveryClient & @EnableFeignClients annotations at start


class

3) Create FeginClient to access WELCOME-API

@FeignClient(name = "WELCOME-API")
public interface WelcomeApiClient {
@GetMapping("/welcome")
public String invokeWelcomeApi();

4) Create Rest Controller with required methods

5) Configure below properties in application.yml

- server port
- admin server url
- actuator endpoints
- application name

---
server:
port: 9091
spring:
application:
name: GREET-API
boot:
admin:
client:
url: http://localhost:1111/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: '*'
...

6) Run the application and check Eureka Dashboard, Admin Server Dashboard and
Zipkin Dashboard

***************************************Step-6 :: Develop API-Gateway


Application****************************************************

1) Create boot application with below dependencies

- cloud-gateway
- eureka-client
- web-starter
- devtools

2) Configure @EnableDiscoveryClient annotation at boot start class

3) Configure Server Port & API Routings in application.yml file

---
server:
port: 3333
spring:
application:
name: API-GATEWAY
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: one
uri: lb://WELCOME-API
predicates:
- Path=/welcome
- id: two
uri: lb://GREET-API
predicates:
- Path=/greet
---

4) Run the application and Test it.

***********************************************************************************
***********************************

=> We can access Client sent request information using Filter

=> The client request information we can use to validate that request

-> Create below Filter in API Gateway (It will execute for every request)

@Component
public class MyPreFilter implements GlobalFilter {

Logger logger = LoggerFactory.getLogger(MyPreFilter.class);

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain
chain) {
logger.info("filter() method executed....");

// Access request information


ServerHttpRequest request = exchange.getRequest();

HttpHeaders headers = request.getHeaders();

Set<String> keySet = headers.keySet();

keySet.forEach(key -> {
List<String> values = headers.get(key);
System.out.println(key + "::" + values);
});

return chain.filter(exchange);
}
}
-----------------------------------------------------------------------------------
------------------------------------------

-> When we send request to REST API using POSTMAN, it will send POSTMAN Token in
reuqest header. Using this token we can differentiate request came from other apps
or from POSTMAN.

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------

###################
Load Balancing
###################

-> If we run our application on Single Server then all requests will be sent to
single server
-> Burden will be increased on the server
-> When burden got increased request processing gets delayed
-> Sometimes our server might crash due to heavy load

********** To overcome above problems we will use Load Balancing concept


*****************

-> Load Balancing is the process of distributing load to multiple servers

##################################
LBR implementation in Mini Project
#################################

-> Make below changes in WelcomeApi Rest Controller

@RestController
public class WelcomeRestController {

@Autowired
private Environment env;

@GetMapping("/welcome")
public String welcomeMsg() {

String port = env.getProperty("server.port");

String msg = "Welcome to Ashok IT..!!" + " (Port :: " + port + ")";

return msg;
}
}

-> Run Welcome API with 3 instances

-> Righ Click on API


-> Run As -> Run Configurations
-> Select Application
-> Arguments
-> VM Arguments (-Dserver.port=8082)
-> Apply & Run it

-> Check Eureka Dashboard

You might also like