KEMBAR78
Developing Microservices using Spring - Beginner's Guide | ODP
Developing
Microservices
using Spring
-
Beginners Guide
* Disclaimer: There are various topics covered in this session, each topic is worth a 2 day workshop session in itself, so this will cover only the very basics and try
to provide a birds eye view of the various concepts.
Agenda
● Microservices
● What ?
● Why ?
● Challenges of a distributed system
● Building Microservices using Spring
● Spring Boot
● Spring Cloud & Netflix OSS
● Conclusion
All Roads Lead to Microservices
Cloud
aPaaS
Continuous Delivery
Dev Ops
Agile
Microservices
Microservices – What ?
The term "Microservice Architecture" has sprung up over the
last few years to describe a particular way of designing
software applications as suites of independently deployable
services.
-Martin Fowler
http://martinfowler.com/articles/microservices.html
Microservices are loosely coupled service oriented
architecture with bounded contexts.
- Adrian Cockroft
Monolith Application
● Imaginary E-Commerce App
● Very efficient inter process calls between
components
● Good architecture, until it needs to scale
during busy shopping season.
Monolith Application
● You want to scale only 'Shopping
Cart' and 'Checkout' Modules,
but instead have to scale the
entire App
● Other Challenges with multiple
database and Session
management
Monolith Application – Good Parts
● Contains Everything
● Single Deployment
● Minimum viable product can
be reached quickly
● Easy (or Familiar) to understand
Monolith Application – Bad Parts
● Complex Architecture
● Difficult to Scale
(cannot scale only selected
modules)
● Long term commitment
to one technical stack
Microservices
● Distillation of SOA
● Single Context
● Smart Services / Dumb Pipes
● RESTful Communications
● Smaller codebase – easy to
reason about
● Easy to scale
Microservices
Challenges of distributed system
● Configuration Management
● Service Registration & Discovery
● Load Balancing
● Fault Tolerance
● Monitoring
● Concurrent API Integration & Transformation
*(This is not a complete list...)
Spring Boot
● Built on top of Spring framework
● Opinionated convention over configuration
● Creates stand-alone “Production” ready app
● Embedded Tomcat / Jetty
● Various Starters POMs to simplify configuration
● https://start.spring.io/ - Spring Initializr template project (We can
replace our kick start scripts with something like this)
Spring Boot
● It can get pretty small..
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
Exercise 1 : Spring Boot Hello World
Spring Boot
● Retrieve data from database – Spring Data REST
● Entity Class
@Entity
@Table(name="users")
public class User implements Serializable{... }
● Repository Class
@Repository
public interface UserRepository extends JpaRepository<User, Integer> { }
● Main Class
@SpringBootApplication
public class SampleDataJpaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleDataJpaApplication.class, args);
}
}
● The same project runs in local and Cloud - PWS
Exercise 2 : Spring Data REST
Spring Cloud
● Writing a single service with Spring Boot is easy.
● But things gets complex with managing multiple services
● http://cloud.spring.io/spring-cloud-netflix/ , to the rescue.
● Similar to Spring Data umbrella project.
Spring Cloud Netflix
● Configuration Management – Spring Cloud Config
● Service Registration & Discovery – Eureka
● Load Balancing – Feign & Ribbon
● Fault Tolerance (Circuit Breaker) – Hysterix
● Monitoring – Hysterix Dashboard
● Concurrent API Integration & Transformation - RxJava
Spring Cloud Config
● Distributed configuration management.
● Provides server and client-side support for externalized configuration.
● Store Configuration properties (YAML content) in remote repository (Git
supported by default)
● Curl -X POST http://localhost:8888/bus/refresh, to get the latest values from
the config server.
Exercise 3 : Config Server
Cloud Bus (AMQP / RabbitMQ)
Git (default)
properties
Config Server
/refresh
/refresh
Service Registry & Discovery - Eureka
● Eureka is Netflix OSS Project for Service Discovery Server & Client.
● Eureka Server – can be configured to be highly available, supports server
replication for resiliency.
● Clients registers with Eureka and sends heartbeat messages to Server.
● Eureka Server
@EnableEurekaServer
class Eureka {
}
● Eureka Client
@EnableDiscoveryClient
public class Application {... }
● Producer Consumer example
Exercise 4 : Eureka – Producer / Consumer
Load balancing - Ribbon
● Ribbon is a Netflix OSS Project, it provides software load balancers with
cluster of servers.
● Provides basic functionality like supply the public DNS name of servers
to client, rotate among the list of servers according to certain logic.
● Load balancer components
Rule – logic to determine which server to return from the serverlist.
Ping – to ensure server liveness
ServerList – can be static or dynamic
● Spring RestTemplate has been added with little bit of magic and made
Ribbon enabled.
● FeignClient is alternative to Spring RestTemplate, Feign is declarative
web service client, makes writing web service clients easier.
Exercise 5 : Producer / Consumer example with load balancer / Feign
Fault Tolerance - Hystrix
● Hystrix – Circuit Breaker Pattern
● Its a state machine, with 3 states – Closed, Open and Half-Open.
● Failures does not cascade up to the top of the call stack.
Exercise 6 : Hystrix in action
Monitoring
● Hystrix Dashboard
Import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard
@EnableHystrixDashboard
class HystrixDashboard { … }
Exercise 7 : Hystrix Dashboard in action
Concurrent API Integration - RxJava
● RxJava is Java implementation of MS
Reactive Extensions.
● A library for composing asynchronous
and event-based programs by using
Observable sequences.
● Netflix created RxJava to simplify
server side concurrency, a single
“heavy” client request that is executed
in parallel on the server.
● The service layer API method return an Observable<T> getData(), this can be
asynch or synch, and its transparent to the caller of this service.
● Can be achieved using Java Futures, but it supports one value, not for a
sequences. Also calling Future.get() will be a blocking.
● Callbacks can be used for asynchronous execution and works well for single level
of execution, but things gets complicated with nested Callbacks (callback hell !).
Conclusion
● Microservices Architectural Style needs certain infrastructure competencies
like, Rapid Provisioning, Continuous Delivery, Basic Monitoring, etc.,
● There are many new design patterns to learn when implementing a
distributed system.
● Spring Cloud Netflix implements certain patterns and Spring Boot makes it
easy to use.
References
● http://martinfowler.com/articles/microservices.html
● http://martinfowler.com/bliki/MicroservicePrerequisites.html
● http://projects.spring.io/spring-boot/
● http://projects.spring.io/spring-cloud/
● http://cloud.spring.io/spring-cloud-netflix/
● http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html
● https://github.com/ReactiveX/RxJava
● http://www.infoq.com/presentations/reactive-arch-grails
● http://www.infoq.com/presentations/Netflix-API-rxjava-hystrix
Images
● https://www.flickr.com/photos/bhophoto/384574407/

Developing Microservices using Spring - Beginner's Guide

  • 1.
    Developing Microservices using Spring - Beginners Guide *Disclaimer: There are various topics covered in this session, each topic is worth a 2 day workshop session in itself, so this will cover only the very basics and try to provide a birds eye view of the various concepts.
  • 2.
    Agenda ● Microservices ● What? ● Why ? ● Challenges of a distributed system ● Building Microservices using Spring ● Spring Boot ● Spring Cloud & Netflix OSS ● Conclusion
  • 3.
    All Roads Leadto Microservices Cloud aPaaS Continuous Delivery Dev Ops Agile Microservices
  • 4.
    Microservices – What? The term "Microservice Architecture" has sprung up over the last few years to describe a particular way of designing software applications as suites of independently deployable services. -Martin Fowler http://martinfowler.com/articles/microservices.html Microservices are loosely coupled service oriented architecture with bounded contexts. - Adrian Cockroft
  • 5.
    Monolith Application ● ImaginaryE-Commerce App ● Very efficient inter process calls between components ● Good architecture, until it needs to scale during busy shopping season.
  • 6.
    Monolith Application ● Youwant to scale only 'Shopping Cart' and 'Checkout' Modules, but instead have to scale the entire App ● Other Challenges with multiple database and Session management
  • 7.
    Monolith Application –Good Parts ● Contains Everything ● Single Deployment ● Minimum viable product can be reached quickly ● Easy (or Familiar) to understand
  • 8.
    Monolith Application –Bad Parts ● Complex Architecture ● Difficult to Scale (cannot scale only selected modules) ● Long term commitment to one technical stack
  • 9.
    Microservices ● Distillation ofSOA ● Single Context ● Smart Services / Dumb Pipes ● RESTful Communications ● Smaller codebase – easy to reason about ● Easy to scale
  • 10.
  • 11.
    Challenges of distributedsystem ● Configuration Management ● Service Registration & Discovery ● Load Balancing ● Fault Tolerance ● Monitoring ● Concurrent API Integration & Transformation *(This is not a complete list...)
  • 12.
    Spring Boot ● Builton top of Spring framework ● Opinionated convention over configuration ● Creates stand-alone “Production” ready app ● Embedded Tomcat / Jetty ● Various Starters POMs to simplify configuration ● https://start.spring.io/ - Spring Initializr template project (We can replace our kick start scripts with something like this)
  • 13.
    Spring Boot ● Itcan get pretty small.. @RestController class ThisWillActuallyRun { @RequestMapping("/") String home() { "Hello World!" } } Exercise 1 : Spring Boot Hello World
  • 14.
    Spring Boot ● Retrievedata from database – Spring Data REST ● Entity Class @Entity @Table(name="users") public class User implements Serializable{... } ● Repository Class @Repository public interface UserRepository extends JpaRepository<User, Integer> { } ● Main Class @SpringBootApplication public class SampleDataJpaApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SampleDataJpaApplication.class, args); } } ● The same project runs in local and Cloud - PWS Exercise 2 : Spring Data REST
  • 15.
    Spring Cloud ● Writinga single service with Spring Boot is easy. ● But things gets complex with managing multiple services ● http://cloud.spring.io/spring-cloud-netflix/ , to the rescue. ● Similar to Spring Data umbrella project.
  • 16.
    Spring Cloud Netflix ●Configuration Management – Spring Cloud Config ● Service Registration & Discovery – Eureka ● Load Balancing – Feign & Ribbon ● Fault Tolerance (Circuit Breaker) – Hysterix ● Monitoring – Hysterix Dashboard ● Concurrent API Integration & Transformation - RxJava
  • 17.
    Spring Cloud Config ●Distributed configuration management. ● Provides server and client-side support for externalized configuration. ● Store Configuration properties (YAML content) in remote repository (Git supported by default) ● Curl -X POST http://localhost:8888/bus/refresh, to get the latest values from the config server. Exercise 3 : Config Server Cloud Bus (AMQP / RabbitMQ) Git (default) properties Config Server /refresh /refresh
  • 18.
    Service Registry &Discovery - Eureka ● Eureka is Netflix OSS Project for Service Discovery Server & Client. ● Eureka Server – can be configured to be highly available, supports server replication for resiliency. ● Clients registers with Eureka and sends heartbeat messages to Server. ● Eureka Server @EnableEurekaServer class Eureka { } ● Eureka Client @EnableDiscoveryClient public class Application {... } ● Producer Consumer example Exercise 4 : Eureka – Producer / Consumer
  • 19.
    Load balancing -Ribbon ● Ribbon is a Netflix OSS Project, it provides software load balancers with cluster of servers. ● Provides basic functionality like supply the public DNS name of servers to client, rotate among the list of servers according to certain logic. ● Load balancer components Rule – logic to determine which server to return from the serverlist. Ping – to ensure server liveness ServerList – can be static or dynamic ● Spring RestTemplate has been added with little bit of magic and made Ribbon enabled. ● FeignClient is alternative to Spring RestTemplate, Feign is declarative web service client, makes writing web service clients easier. Exercise 5 : Producer / Consumer example with load balancer / Feign
  • 20.
    Fault Tolerance -Hystrix ● Hystrix – Circuit Breaker Pattern ● Its a state machine, with 3 states – Closed, Open and Half-Open. ● Failures does not cascade up to the top of the call stack. Exercise 6 : Hystrix in action
  • 21.
    Monitoring ● Hystrix Dashboard Importorg.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard @EnableHystrixDashboard class HystrixDashboard { … } Exercise 7 : Hystrix Dashboard in action
  • 22.
    Concurrent API Integration- RxJava ● RxJava is Java implementation of MS Reactive Extensions. ● A library for composing asynchronous and event-based programs by using Observable sequences. ● Netflix created RxJava to simplify server side concurrency, a single “heavy” client request that is executed in parallel on the server. ● The service layer API method return an Observable<T> getData(), this can be asynch or synch, and its transparent to the caller of this service. ● Can be achieved using Java Futures, but it supports one value, not for a sequences. Also calling Future.get() will be a blocking. ● Callbacks can be used for asynchronous execution and works well for single level of execution, but things gets complicated with nested Callbacks (callback hell !).
  • 23.
    Conclusion ● Microservices ArchitecturalStyle needs certain infrastructure competencies like, Rapid Provisioning, Continuous Delivery, Basic Monitoring, etc., ● There are many new design patterns to learn when implementing a distributed system. ● Spring Cloud Netflix implements certain patterns and Spring Boot makes it easy to use.
  • 24.
    References ● http://martinfowler.com/articles/microservices.html ● http://martinfowler.com/bliki/MicroservicePrerequisites.html ●http://projects.spring.io/spring-boot/ ● http://projects.spring.io/spring-cloud/ ● http://cloud.spring.io/spring-cloud-netflix/ ● http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html ● https://github.com/ReactiveX/RxJava ● http://www.infoq.com/presentations/reactive-arch-grails ● http://www.infoq.com/presentations/Netflix-API-rxjava-hystrix Images ● https://www.flickr.com/photos/bhophoto/384574407/