1
Spring Boot Micro-services
Presenter:
Prabhakaran Ravichandran
5 Day Session
2
Overall Agenda:
Day 1:
- JAVA 8 API s Introduction
- Micro-service Introduction
- Spring Boot Introduction
- Spring MVC vs Spring Boot vs Spring Reactive
- IoC & Bean configuration
- Hello World Implementation
Day 2:
- REST API s (POST/GET/PUT/PATCH)
- H2 embedded DB
- Controller Advice
- AOP Logging
- Cache Management
- Web Micro-service Implementation
Day 3:
- JUnit5
- Implementation (Cont. on Web Micro-service)
- Logging Standards & Configuration
- Scheduled Tasks
- Task Micro-service Implementation
Day 4:
- Spring Circuit Breaker
- Spring Retry
- Circuit Breaker Implementation
- Assignment
Day 5:
- Load Balancing
- Eureka Implementation
- Discussion of Problem points in Assignment
- Code Review process
3
Day 1 Agenda:
- JAVA 8 API s Introduction
- Micro-service Introduction
- Spring Boot Introduction
- Spring MVC vs Spring Boot vs Spring Reactive
- IoC & Bean configuration
- Hello World Implementation
4
Java 8 Application Programming Interfaces:
- JAR files which are imported as libraries with JAVA 8
- JDK & JRE needs to be set up to use JAVA 8
- Annotation based libraries are used
- All extended supports with JAVA 8 will be stopped 2030
- Recommended to move to JAVA 11
- JAVA 11+ versions only requires JDK
- JAVA 11 is free to use for non-commercial use cases
5
Micro-services:
- Partitioning Applications based on business context
- Faster development and deployment of newer services
- Easy prototyping and migration/integration with UI
- Favorable in migration to cloud or native cloud
infrastructure
(Horizontal Scaling, API Gateway, VPC, ALB, etc..,)
6
Monolithic:
One Application
User
Management
Leave
Management
Payroll
Management
Micro-Service:
User
Management
Leave
Management
Payroll
Management
All Different Micro-services Applications
(Integrated into an UI through REST
services)
UI
UI
7
Spring Boot:
- Auto configuration of Bean & Dependency
- Provides support for NO XML based configuration
- Reduction of boilerplate codes with Annotations
- Introduces spring-starter dependencies helps in
dependency management
- Advanced Repository interfaces which eliminates the need
for DAO implementation
- In-built server for packaging the application jar
- Only requires JAVA to deploy in an environment
- Much more powerful Annotations and their usages
8
Spring MVC vs Spring Boot:
- Spring MVC requires an WEB-INF configuration
- May create an over-head for boilerplate codes
- Difficult to manage dependencies and their version
compatibility
- Dependent on servers explicitly and requires one in every
environment where deployed
- Migration to different UI takes more time and requires full
testing of the application
Spring MVC + Advancement / Isolation = Spring Boot
9
Spring Reactive Introduction:
- Non-blocking in nature
- Event driven communication
- publisher/subscriber pattern
- Migration from Spring Boot requires architectural
& functional changes
10
Eclipse Configuration:
Environment:
- Lombok
(https://projectlombok.org/setup/eclipse)
Plugins:
- EclEmma Java Code Coverage
- Eclipse M2e - Maven Support in Eclipse IDE Latest
- EGit
- Junit-Tools
- MoreUnit
- SonarLint
- Spring Tools 4 (aka Spring Tool Suite 4)
11
Inversion Of Control & Application Context:
IoC:
- Auto Bean creation during start-up of application/Lazy load without
creating new Objects in the runtime
- During Concurrent usage of application reduces object creation overhead
load
- Auto configuring the object where ever requiring in the application using
annotation
Application Context:
- Resolving environment properties for the application
- A place where the created Beans/Component are made available
- Internalization for making application scan for component/bean in the code
12
Hello World Implementation:
Code Concepts:
- Spring Boot Starter dependencies
- Eager Loading & Lazy Loading of Beans
- Conditional Bean loading
- REST services without Response Entity
GitHub Link:
https://github.com/pbkn/HelloWorld
(To be continued in working session)
13
Day 2
Agenda:
- REST API s (POST/GET/PUT/PATCH)
- H2 embedded DB
- Controller Advice
- AOP Logging
- Cache Management
- Web Micro-service Implementation
14
Introduction to REST services:
- Makes use of HTTP verbs (POST, GET, PUT, DELETE, etc..,)
- Uses JSON as a de facto Standard for request & response
- Establishes the connection for interacting with other micro-services
through API calls
- Widely used because of its Stateless Transfer mechanism
- Supports Caching at Server
15
Hibernate:
- Maps POJO class to Database Tables
- Provides support for JPA/CRUD repository services
- Enables auto Table configuration with Annotations
- Column Validations and Sequence Generations with Annotations
16
H2 Embedded DB:
- Lightweight and in-Memory DB for prototyping new services
- Web DB console for Database management
- Supports Transaction Management
- Pure JAVA DB (imitating PostgreSQL)
- Able to support file-based DB for persisting Data
17
Controller Advice:
- Common Exception Handling for a Controller
- Reduces boilerplate codes by handling it in controller advice
class
- Handles controller and its subsequent flows exception as well
- Supports providing useful information in the response entity in
case of exceptions
- If more than one controller are present, a separate Controller
Advice is created for each
18
Point Cut:
- Part of Spring’s Aspect Oriented Programming
- Provides support for specific instructions in a common class
- Used to reduce boiler plate codes in application
- Generally used for logging purpose
19
Cache Management:
- Auto-Configuration for Concurrent Map Cache Manager
- Cache results directly on Service methods using Annotations
- Cache results, update and delete using keys
- Do not cache list of entity/entity unless recommended for business
- Highly recommended to handle cache at Server level rather than client
application level
- Default Spring Cache providers TTL (Time-To-Live) is life-cycle of the
application
20
Default Control Flow of Spring
Boot Application:
UI Controller Service Repository
DTO
Model
Mapping
Configuration
/Component
AOP
Logging
Controller
Advice
Entity
Table DDL
Bean/Table
creation on
start-up
Exception Handling/ Logging
Through out the application
21
H2 DB Implementation:
Code Concepts:
- H2 embedded in-memory DB
- Open API Swagger
- Controller Advice
- JPA Repository
- DTO Model Mapping
- Cache Management
GitHub Link:
https://github.com/pbkn/SpringDataH2
(To be continued in working session)
22
Day 3
Agenda:
- JUnit5
- Implementation (Cont. on Web Micro-service)
- Logging Standards & Configuration
- Scheduled Tasks
- Task Micro-service Implementation
23
JUnit & Log4j2:
- JUnit used for unit testing of functional requirements in code
- Optimally JUnit should cover all parts of code in business level
- Recommended to do only Standalone unit testing for the class with Mockito
- Integration / Application / Performance testing should not be written as JUnit
and carried out in actual infrastructure
- Log4j2 covers the Remote Code Execution of Log4j vulnerability which
impacted the globe
24
JUnit5 Implementation:
Code Concepts:
- MockMVC
- Mockito
- JPA Test
- App Context Loading
- Slf4j
GitHub Link:
https://github.com/pbkn/SpringDataH2
(To be continued in working session)
25
Logging Standards &
Configuration:
- Logging Standards account for the centralized logging
- PII (Personally Identifiable Information) should not be logged
- It is completely fine to use combination of centralized logging and in-line
logging
- Spring Boot log is configured by default and can be customized
- External recording of log in files / cloud / 3rd party services are always
recommended
26
Spring Boot Logging
Configuration:
2019-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
The following items are output:
- Date and Time: Millisecond precision
- Log Level: ERROR, WARN, INFO, DEBUG, or TRACE.
- Process ID.
- A --- separator to distinguish the start of actual log messages.
- Thread name: Enclosed in square brackets (may be truncated for console output).
- Logger name: This is usually the source class name (often abbreviated).
- The log message.
27
Scheduled Tasks:
- Scheduled Tasks run based on CRON schedule / Cloud trigger
- It is likely to be used with EVENT/QUEUE based architecture
- @Scheduled annotated method should have void return type and no
method arguments
- Exceptions are recommended to be handled at the application java file
- The application will start → do action → shutdown, whereas web micro-
service will run 24*7 listening for API call until manually shutdown
28
Task Micro-service
Implementation:
Code Concepts:
- logback file config
- .xlsx to bean
- bean to .xlsx
- scheduled CRON
GitHub Link:
https://github.com/pbkn/Logback
(To be continued in working session)
29
Day 4
Agenda:
- Spring Circuit Breaker
- Spring Retry
- Circuit Breaker Implementation
- Assignment
30
Spring Circuit Breaker:
- Circuit Breaker pattern used between micro-service to micro-service
calls/micro-service to external API call
- It will make the application fail-fast
- This pattern is best suited for enterprise applications preventing failures
in multiple points
- It has three states, OPEN, HALF-OPEN, CLOSED
- Life Cycle, CLOSED → OPEN → HALF-OPEN → CLOSED/OPEN
- It provides fallback strategy to accommodate for failure message/cache
data usage
31
Spring Circuit Breaker:
Before Circuit Breaker Pattern:
Micro A Micro B Micro C
Micro B Micro D
Failure in Micro B cascaded to Micro C & D
After Circuit Breaker Pattern:
Micro A Micro B Micro C
Micro B Micro D
Failure in Micro B failed fast and not
cascaded to Micro C & D
32
Spring Retry:
- Spring Retry is tightly coupled with Circuit Breaker Pattern
- Enables automatic retry on connection failure
- Most suitable for scheduled tasks/application with high network load
Before Retry:
Micro A Micro B Micro C
Micro B Micro D
Failure in Micro B connection to Micro C
due to network glitch
After Retry:
Micro A Micro B Micro C
Micro B Micro D
Failure in Micro B connection to Micro C
retry after connection failure
33
Circuit Breaker
Implementation:
Code Concepts:
- Spring Circuit Breaker
- Spring Retry
GitHub Link:
https://github.com/pbkn/SpringCircuitBreaker
(To be continued in working session)
34
Assignment:
Task 1:
Make a dedicated Micro-service(s) with all code concepts discussed
through out the sessions till now
Task 2:
GitHub commit your code and check for vulnerability with Snyk
35
Day 5
Agenda:
- Load Balancing
- Eureka Implementation
- Discussion of Problem points in Assignment
- Code Review process
36
Load Balancing:
- Making application available even when one instance fails
- Infra level detail to be injected via application yaml/properties
- No/Low code change is required for configuring LB
- Can be done in Server/Client Side
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
UI
I2
I3
I1
LB
CLIENT SIDE
SERVER SIDE
37
Eureka Implementation:
Code Concepts:
- Eureka Server
- Eureka Client
- Eureka Ribbon Client
GitHub Link:
https://github.com/pbkn/EurekaServer
https://github.com/pbkn/EurekaClient
https://github.com/pbkn/EurekaRibbonClient
Note:
Client Side Load Balancing is not
recommended when application is
deployed in Cloud
(To be continued in working session)
38
Discussion of Problem
points in Assignment:
(Discussion Phase)
39
Code Review process:
- Use Static Code Analysis tools like SonarQube
- Use Vulnerability scanners like Snyk
- Make use of JaCoCo (Java Code Coverage) tool to check for JUnit Test
Coverage
- Get Reviewed from Peer for code readability and standards
40
KEEP LEARNING RELEVANT
THANK YOU

Spring_Boot_Microservices-5_Day_Session.pptx

  • 1.
  • 2.
    2 Overall Agenda: Day 1: -JAVA 8 API s Introduction - Micro-service Introduction - Spring Boot Introduction - Spring MVC vs Spring Boot vs Spring Reactive - IoC & Bean configuration - Hello World Implementation Day 2: - REST API s (POST/GET/PUT/PATCH) - H2 embedded DB - Controller Advice - AOP Logging - Cache Management - Web Micro-service Implementation Day 3: - JUnit5 - Implementation (Cont. on Web Micro-service) - Logging Standards & Configuration - Scheduled Tasks - Task Micro-service Implementation Day 4: - Spring Circuit Breaker - Spring Retry - Circuit Breaker Implementation - Assignment Day 5: - Load Balancing - Eureka Implementation - Discussion of Problem points in Assignment - Code Review process
  • 3.
    3 Day 1 Agenda: -JAVA 8 API s Introduction - Micro-service Introduction - Spring Boot Introduction - Spring MVC vs Spring Boot vs Spring Reactive - IoC & Bean configuration - Hello World Implementation
  • 4.
    4 Java 8 ApplicationProgramming Interfaces: - JAR files which are imported as libraries with JAVA 8 - JDK & JRE needs to be set up to use JAVA 8 - Annotation based libraries are used - All extended supports with JAVA 8 will be stopped 2030 - Recommended to move to JAVA 11 - JAVA 11+ versions only requires JDK - JAVA 11 is free to use for non-commercial use cases
  • 5.
    5 Micro-services: - Partitioning Applicationsbased on business context - Faster development and deployment of newer services - Easy prototyping and migration/integration with UI - Favorable in migration to cloud or native cloud infrastructure (Horizontal Scaling, API Gateway, VPC, ALB, etc..,)
  • 6.
  • 7.
    7 Spring Boot: - Autoconfiguration of Bean & Dependency - Provides support for NO XML based configuration - Reduction of boilerplate codes with Annotations - Introduces spring-starter dependencies helps in dependency management - Advanced Repository interfaces which eliminates the need for DAO implementation - In-built server for packaging the application jar - Only requires JAVA to deploy in an environment - Much more powerful Annotations and their usages
  • 8.
    8 Spring MVC vsSpring Boot: - Spring MVC requires an WEB-INF configuration - May create an over-head for boilerplate codes - Difficult to manage dependencies and their version compatibility - Dependent on servers explicitly and requires one in every environment where deployed - Migration to different UI takes more time and requires full testing of the application Spring MVC + Advancement / Isolation = Spring Boot
  • 9.
    9 Spring Reactive Introduction: -Non-blocking in nature - Event driven communication - publisher/subscriber pattern - Migration from Spring Boot requires architectural & functional changes
  • 10.
    10 Eclipse Configuration: Environment: - Lombok (https://projectlombok.org/setup/eclipse) Plugins: -EclEmma Java Code Coverage - Eclipse M2e - Maven Support in Eclipse IDE Latest - EGit - Junit-Tools - MoreUnit - SonarLint - Spring Tools 4 (aka Spring Tool Suite 4)
  • 11.
    11 Inversion Of Control& Application Context: IoC: - Auto Bean creation during start-up of application/Lazy load without creating new Objects in the runtime - During Concurrent usage of application reduces object creation overhead load - Auto configuring the object where ever requiring in the application using annotation Application Context: - Resolving environment properties for the application - A place where the created Beans/Component are made available - Internalization for making application scan for component/bean in the code
  • 12.
    12 Hello World Implementation: CodeConcepts: - Spring Boot Starter dependencies - Eager Loading & Lazy Loading of Beans - Conditional Bean loading - REST services without Response Entity GitHub Link: https://github.com/pbkn/HelloWorld (To be continued in working session)
  • 13.
    13 Day 2 Agenda: - RESTAPI s (POST/GET/PUT/PATCH) - H2 embedded DB - Controller Advice - AOP Logging - Cache Management - Web Micro-service Implementation
  • 14.
    14 Introduction to RESTservices: - Makes use of HTTP verbs (POST, GET, PUT, DELETE, etc..,) - Uses JSON as a de facto Standard for request & response - Establishes the connection for interacting with other micro-services through API calls - Widely used because of its Stateless Transfer mechanism - Supports Caching at Server
  • 15.
    15 Hibernate: - Maps POJOclass to Database Tables - Provides support for JPA/CRUD repository services - Enables auto Table configuration with Annotations - Column Validations and Sequence Generations with Annotations
  • 16.
    16 H2 Embedded DB: -Lightweight and in-Memory DB for prototyping new services - Web DB console for Database management - Supports Transaction Management - Pure JAVA DB (imitating PostgreSQL) - Able to support file-based DB for persisting Data
  • 17.
    17 Controller Advice: - CommonException Handling for a Controller - Reduces boilerplate codes by handling it in controller advice class - Handles controller and its subsequent flows exception as well - Supports providing useful information in the response entity in case of exceptions - If more than one controller are present, a separate Controller Advice is created for each
  • 18.
    18 Point Cut: - Partof Spring’s Aspect Oriented Programming - Provides support for specific instructions in a common class - Used to reduce boiler plate codes in application - Generally used for logging purpose
  • 19.
    19 Cache Management: - Auto-Configurationfor Concurrent Map Cache Manager - Cache results directly on Service methods using Annotations - Cache results, update and delete using keys - Do not cache list of entity/entity unless recommended for business - Highly recommended to handle cache at Server level rather than client application level - Default Spring Cache providers TTL (Time-To-Live) is life-cycle of the application
  • 20.
    20 Default Control Flowof Spring Boot Application: UI Controller Service Repository DTO Model Mapping Configuration /Component AOP Logging Controller Advice Entity Table DDL Bean/Table creation on start-up Exception Handling/ Logging Through out the application
  • 21.
    21 H2 DB Implementation: CodeConcepts: - H2 embedded in-memory DB - Open API Swagger - Controller Advice - JPA Repository - DTO Model Mapping - Cache Management GitHub Link: https://github.com/pbkn/SpringDataH2 (To be continued in working session)
  • 22.
    22 Day 3 Agenda: - JUnit5 -Implementation (Cont. on Web Micro-service) - Logging Standards & Configuration - Scheduled Tasks - Task Micro-service Implementation
  • 23.
    23 JUnit & Log4j2: -JUnit used for unit testing of functional requirements in code - Optimally JUnit should cover all parts of code in business level - Recommended to do only Standalone unit testing for the class with Mockito - Integration / Application / Performance testing should not be written as JUnit and carried out in actual infrastructure - Log4j2 covers the Remote Code Execution of Log4j vulnerability which impacted the globe
  • 24.
    24 JUnit5 Implementation: Code Concepts: -MockMVC - Mockito - JPA Test - App Context Loading - Slf4j GitHub Link: https://github.com/pbkn/SpringDataH2 (To be continued in working session)
  • 25.
    25 Logging Standards & Configuration: -Logging Standards account for the centralized logging - PII (Personally Identifiable Information) should not be logged - It is completely fine to use combination of centralized logging and in-line logging - Spring Boot log is configured by default and can be customized - External recording of log in files / cloud / 3rd party services are always recommended
  • 26.
    26 Spring Boot Logging Configuration: 2019-03-0510:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52 2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms 2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] The following items are output: - Date and Time: Millisecond precision - Log Level: ERROR, WARN, INFO, DEBUG, or TRACE. - Process ID. - A --- separator to distinguish the start of actual log messages. - Thread name: Enclosed in square brackets (may be truncated for console output). - Logger name: This is usually the source class name (often abbreviated). - The log message.
  • 27.
    27 Scheduled Tasks: - ScheduledTasks run based on CRON schedule / Cloud trigger - It is likely to be used with EVENT/QUEUE based architecture - @Scheduled annotated method should have void return type and no method arguments - Exceptions are recommended to be handled at the application java file - The application will start → do action → shutdown, whereas web micro- service will run 24*7 listening for API call until manually shutdown
  • 28.
    28 Task Micro-service Implementation: Code Concepts: -logback file config - .xlsx to bean - bean to .xlsx - scheduled CRON GitHub Link: https://github.com/pbkn/Logback (To be continued in working session)
  • 29.
    29 Day 4 Agenda: - SpringCircuit Breaker - Spring Retry - Circuit Breaker Implementation - Assignment
  • 30.
    30 Spring Circuit Breaker: -Circuit Breaker pattern used between micro-service to micro-service calls/micro-service to external API call - It will make the application fail-fast - This pattern is best suited for enterprise applications preventing failures in multiple points - It has three states, OPEN, HALF-OPEN, CLOSED - Life Cycle, CLOSED → OPEN → HALF-OPEN → CLOSED/OPEN - It provides fallback strategy to accommodate for failure message/cache data usage
  • 31.
    31 Spring Circuit Breaker: BeforeCircuit Breaker Pattern: Micro A Micro B Micro C Micro B Micro D Failure in Micro B cascaded to Micro C & D After Circuit Breaker Pattern: Micro A Micro B Micro C Micro B Micro D Failure in Micro B failed fast and not cascaded to Micro C & D
  • 32.
    32 Spring Retry: - SpringRetry is tightly coupled with Circuit Breaker Pattern - Enables automatic retry on connection failure - Most suitable for scheduled tasks/application with high network load Before Retry: Micro A Micro B Micro C Micro B Micro D Failure in Micro B connection to Micro C due to network glitch After Retry: Micro A Micro B Micro C Micro B Micro D Failure in Micro B connection to Micro C retry after connection failure
  • 33.
    33 Circuit Breaker Implementation: Code Concepts: -Spring Circuit Breaker - Spring Retry GitHub Link: https://github.com/pbkn/SpringCircuitBreaker (To be continued in working session)
  • 34.
    34 Assignment: Task 1: Make adedicated Micro-service(s) with all code concepts discussed through out the sessions till now Task 2: GitHub commit your code and check for vulnerability with Snyk
  • 35.
    35 Day 5 Agenda: - LoadBalancing - Eureka Implementation - Discussion of Problem points in Assignment - Code Review process
  • 36.
    36 Load Balancing: - Makingapplication available even when one instance fails - Infra level detail to be injected via application yaml/properties - No/Low code change is required for configuring LB - Can be done in Server/Client Side UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB UI I2 I3 I1 LB CLIENT SIDE SERVER SIDE
  • 37.
    37 Eureka Implementation: Code Concepts: -Eureka Server - Eureka Client - Eureka Ribbon Client GitHub Link: https://github.com/pbkn/EurekaServer https://github.com/pbkn/EurekaClient https://github.com/pbkn/EurekaRibbonClient Note: Client Side Load Balancing is not recommended when application is deployed in Cloud (To be continued in working session)
  • 38.
    38 Discussion of Problem pointsin Assignment: (Discussion Phase)
  • 39.
    39 Code Review process: -Use Static Code Analysis tools like SonarQube - Use Vulnerability scanners like Snyk - Make use of JaCoCo (Java Code Coverage) tool to check for JUnit Test Coverage - Get Reviewed from Peer for code readability and standards
  • 40.