Professional Documents
Culture Documents
Micro Services
Micro Services
Micro Services:
1) Monolith Introduction
2) Monolith Architecture case study
3) Monolith Application Development Process
4) Load balancer(Cluster)case study
5) Load Balancing Algorithm(a.Round Robin, b)IP Hashing, c)Sticky Session)
6) Monolith Drawbacks
7) Micro Services Introduction
8) Micro Services Advantages
9) Micro Services Dis-Advantages
10) Micro Services Case study
11) Micro Services Architecture
12) Micro Services Development(a.API-1, b.API-2)
13) Inter service communication case study
14) Feign Client
15) Cloud Introduction.(AWS,AZURE,PCF)
16) PCF CLI Installation
17) Deploying Micro Services to PCF using CLI
18) Auto Scaling
19) Service Registry case study(Netflix Edurekha)
20) API Gateway(Zuul Proxy)
21) Hystrix(Circuit Breaker)
22) Hystrix Dashboard
23) Spring Boot Admin services.
----------------------------------------------------
-> When all components are in same application, all requests come to same server then burden
will increase on server.
-> When burden increased on server, it will process requests slowly sometimes server might get
crash also.
-> To reduce burden on the server, people will use Load Balancers for applications.
-> Our application will be deployed to multiple servers and all those servers will be connected
Load balancer.
How Load Balancer will distribute the load?
--------------------------------------------
1) Round Robbin
2) Sticky Session
3) IP Hashing etc....
Monolith Architecture:
In our application , we will have several modules and several components also will be
available
Components:
> Presentation components: Responsible for handling user interface
> Web components: Responsible to handle user requests.
> Business components: Responsible to execute Business logic.
> Persistence components: Responsible for DB operation.
> Integration components : Responsible webservices Rest-Api
communications.
> Authorization components : Responsible to authorizing user
> Notification Components : Responsible for sending email/mobile message
notification whenever required.
If we develop all these components as a single project then it is called as monolith
Architecture Based Project.
Microservices Architecture :
Microservice is an architectural design pattern which is used to avoid the problems of
monolith Architecture.
Challenges:
1)Bounded context.
2)Lot of configuration.
3) Visibility
4) Pack of card problem
Advantages:
Easy maintenance
Fats releases(Delivery)
Easy Scale
Technology independently
Parallel Development.
Microservices Architecture:
There is no fixed architecture available for microservices
People are using microservices Architecture as per their convience
API GateWay :
An api gateway is an API management tool that sits between a client and collection of
backend services.
API gateways acts ad single Entry point for all clients.
In API gateway we can write the logic to filter user requests.
-> Run our Spring Boot Application and Access Eureka Dashboard using below URL
http://localhost:8761/
Procedure to create Service as Eureka Client
1) Create Spring Boot Project with below dependencies
1) spring-starter-web
2) spring-cloud-netflix-eureka-client
2) Configure @EnableDiscoveryClient annotation at Spring Boot start class
3) Create Rest Controller with Required methods
4) Change Embedded Container Port No if Required and Run application
5) Verify Eureka Dashboard ( Client should be registered )
Eureka Client
-> Service registry is used to register all services available in our project (REST APIs)
-> Service registry will provide a dashboard with details of services which are registered (name,
status & URL)
-> Eureka Server we are using as a Service Registry
-> If Eureka is running on 8761 then then Discovery Clients can auto register with Eureka Server
Note: Before running client application, make sure Eureka Server Project is Running.
Inter-Service Communication
In our project, if one micro-services accessing another microserice then it is called as Inter-
service communication.
Note: Both Microservices are available in Same Project.
-> We can use Rest Client logic to access one Rest API
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "HELLO-SERVICE")
public interface HelloClient {
@GetMapping(value = "/hello/{name}")
public String invokeHelloService(@PathVariable("name") String name);
Microservices Requirement
-> If one microservices is communicating with another microservice belongs to same project is
called as Inter Service Communication.
-> For Inter Service Communication we can use FeignClient
-> When we use FeignClient, we can specify name of the service instead of URL for
communication
Steps to develop
1) Create boot application with below dependencies
a) spring-boot-starter-web
b) h2
c) project lombok
d) devtools
e) spring-boot-starter-data-jpa
f) swagger & swagger-ui
@ExceptionHandler(value=CompanyNameNotFoundException.class)
public ResponseEntity<String> handleNotFoundException(){
String mug = "Company Name Not Found";
return new ResponseEntity(mug, HttpStatus.OK);
}
}
----------------------------------------------------------------
StockAmountCalculationService
This rest api will take company name and quantity as input. It should get company stock price from
'StockPriceService' api. Then it has to calculate total price for given quantity and it has to return that
as an output.
Use Case
Develop a project to Calculate stock price
API Gateway Implementation
-> API Gateway will act as Entrypoint services available in project
-> API Gateway will be called as Edge Microservice
-> If we don't have API Gateway then we need to implement request filtering logic in every
microservice. It increases Maintenense cost of the project.
-> Spring Cloud Netflix Library provided Zuul Proxy as an API Gateway.
-> Zuul Proxy is open source API Gateway
-> Apigee is commercial API Gateway provided by Google
Actuators
-> We are developing applications using Spring Boot and We are deploying our applications in
servers
-> Once application is deployed, several users will access our applications
-> When our application is running in production, it is very important to monitor our application
*** To overcome this problem, Spring Boot Provided Admin Server & Admin Client Concept **
-> If we use Admin Server, it will provide beautiful user interface to monitor and manage our REST
APIs.
Note: Our Rest apis should be registered with Admin server then our rest api is called as Spring Boot
Admin Client.
Example on Admin Server & Client
-> Spring Boot Admin server is used monitor and manage our client applications (rest apis)
-> By using Admin server at one place we can monitor and manage all our services
1)spring-boot-starter-web
2)spring-boot-starter-admin-server
Note: By default Admin server will provide UI for monitoring and managing registered clients.
1)spring-boot-starter-web
2)spring-boot-starter-admin-client
3)spring-boot-starter-actuator
1)port number
2)application name
-> Earlier people used to work with properties files but now people are using YAML or YML files to
store the data
->To avoid hardcoding in java classes we will use YML files in Spring Boot
Syntax
server:
port: 9090
port: 9090
spring:
application:
name: DEMO-APP
demo:
-----------------------------------------------------------------
@Data
@Configuration
@ConfigurationProperties(prefix = "demo")
@EnableConfigurationProperties
--------------------------------------------------------------------
@RestController
@Autowired
@GetMapping("/welcome")
return configProps.getWelcomeMsg();
}
@GetMapping("/greet")
return configProps.getGreetMsg();
-> YML files are widely used format for configuration properties
-> Spring Boot is having very good integration with YML files.
-> In Spring Boot we can use .properties and .yml files for configuration properties
-> When we create Spring Boot application, by default it will provide application.properties file
------------------------------------------------------------------
-> Config Server is used to Externalize Configuration Properties from our application.
a) spring-boot-starter-web
b) spring-cloud-config-server
3) Create GIT repository and create config-properties files in repository then configure properties in
the form of key and vale format.
EX: https://github.com/TEK-Leads/Config-Properties.git
a) server-port
b) spring.cloud.config.server.git.uri=<repo-url>
c) spring.cloud.config.server.git.cloneOnStart=true
d) management.security.enabled=false
a)spring-boot-starter-web
2)spring-cloud-starter-config
a) server-port
b) application-name
c) activate profile
@Value("${mug}")
---------------------------------------------------
----------------------------------------------------------
If we have both application.yml and bootstrap.yml in our application, Spring Boot will load
bootstrap.yml first.
Circuit Breaker
-> A circuit breaker is an automatically operated electrical switch designed to protect an electrical
circuit from damage caused by excess current from an overload or short circuit. Its basic function is
to interrupt current flow after a fault is detected.
How it is related to Spring Boot ?
Ribbon in Microservices
-> For Running Spring Boot application we will configure Port Number in application.properties or
application.yml file
Ex : server.port = 9090
-> We don't configure port number, it will consider 8080 as default port number.
-> Using Environment obj, we can capture on which port number our application is running.
-------------------------------------------------------------------------
@RestController
@Autowired
@GetMapping("/welcome")
----------------------------------------------------------------------------------------------
Right Click On Project -> Run As -> Run Configuration -> Click on Arguments Tab -> Write below
property in VM Arguments -> Click on Apply -> Click Run
-Dserver.port=9091
==================================================================================
===========
================================================
To celebrate one function in house, we don't purchase shamiyana materal. we will take for rent and
will use for our function. After function got completed, we will return that materal by paying rent
amount.
To develop one application, we need computer, we need OS, we need some softwares like JIRA,
JENKINS, Database, Application Server etc... Instead of purchasing all these softwares, we can take
them for rent on reantal basis.
Azure (Microsoft)
Ex : PCF
Circuit Breaker
-> If one microservice is not able to process the request, then request processing can't be
completed.....
-> To implement Circuit breaker, we will use Spring Cloud Netflix Hystrix Library
Steps to work with Circuit Breaker
1)spring-boot-starter-web
2)spring-cloud-starter-netflix-hystrix
3) Create Rest Controller with Required Methods (business operation method and fallback method)
4) Configure Circuit Breaker for Business Operation method using below annotation
@HystrixCommand(fallBackMethod="...")
-> Logging is the process of storing application execution details in a file to monitor in future.
-> By using log files we can understanad where is the problem and what is the problem in
application.
3) After zipkin server started, access Zipkin dashboard using below url
URL : http://localhost:9411/zipkin/
1)spring-boot-starter-web
2)sleuth
3)zipkin
4)devtools
------------------------------------------------------------------
@RestController
@GetMapping(value = "/welcome")
return mug;
-------------------------------------------------------------------
8) Send the request to Rest controller method and verify Zipkin dashboard for log details.
Cache Implementation
-> Whenever we need to access same data frequently then its better to maintain that data in cache
to improve performance of application.
-> If we read same data for multiple times from db it will decrease performance of the application.
Because DB interaction is costly operation.
1) Transactional Tables
If application is performing only read operation on the table then that table is called as Non-
Transactional table.
Note: We should implement cache for Non-Transactional table because these tables contains static
data
-> If we implement cache in one project, then it is called as Local Cache. Only that project can access
data from that local cache. It is suitable for Monolith Architecture Based Projects.
-> When we implement project using Microservices architecture we will have multiple services. If we
want cache data in all services we should go for Global Cache. It is also called as 'Distributed Cache'.
-> If we implement distributed cache, all services can access data from distributed cache only.
-> Now a days in industry, we are using Redis Cache to maintain Distributed Cache.
What is Redis?
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache
and message broker.
It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries,
bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.
URL : www.github.com/microsoftarchive/redis/releases
5) Once Redis Client CLI is started just execute 'ping' it should give PONG as response.
Note: With above steps Redis software setup is done in our machine
Command to store data in Redis in the form of KEY and VALUE
GET "KEY"
KEYS
1) CrudRepository (I)
2) JpaRepository (I)
Components
-----------
-----------------------------------------
@Entity
@Table(name="USER_TBL")
@Id
@Column(name="USER_ID")
@Column(name="USER_NAME")
}
---------------------------------------------
----------------------------------------------------------
-----------------------------------------------------------------
-------------------------------------------------------------------
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=
-> When we configure DataSource config props in application.properties file or application.yml file
Spring Boot will take care of DB connections.
----------------------------------------
-------------------application.properties-----------------------
spring.user.datasource.url=
spring.user.datasource.username=
spring.user.datasource.password=
spring.user.datasource.driver-class-name=
spring.book.datasource.username=
spring.book.datasource.password=
spring.book.datasource.driver-class-name=
------------------------------------------------------------------
package com.ashokit.config;
import java.util.HashMap;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "bookEntityManagerFactory",
transactionManagerRef = "bookTransactionManager",
basePackages = {
"com.ashokit.book.repository"
@ConfigurationProperties(prefix = "spring.book.datasource")
return DataSourceBuilder.create().build();
@Bean(name = "bookEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
bookEntityManagerFactory(EntityManagerFactoryBuilder builder,
properties.put("hibernate.hbm2ddl.auto", "update");
return builder.dataSource(dataSource)
.properties(properties)
.packages("com.ashokit.book.model")
.persistenceUnit("Book")
.build();
@Bean(name = "bookTransactionManager")
public PlatformTransactionManager
bookTransactionManager(@Qualifier("bookEntityManagerFactory") EntityManagerFactory
bookEntityManagerFactory) {
-------------------------------------------------------------
package com.ashokit.config;
import java.util.HashMap;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {
"com.ashokit.user.repository"
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.user.datasource")
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(EntityManagerFactoryBuilder builder,
properties.put("hibernate.hbm2ddl.auto", "update");
return builder.dataSource(dataSource)
.properties(properties)
.packages("com.ashokit.user.model")
.persistenceUnit("User")
.build();
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager
transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory) {