developing microservices using spring - beginner's guide

24
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.

Upload: mohanraj-thirumoorthy

Post on 19-Jul-2015

181 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Developing Microservices using Spring - Beginner's Guide

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.

Page 2: Developing Microservices using Spring - Beginner's Guide

Agenda● Microservices

● What ?● Why ?

● Challenges of a distributed system

● Building Microservices using Spring● Spring Boot● Spring Cloud & Netflix OSS

● Conclusion

Page 3: Developing Microservices using Spring - Beginner's Guide

All Roads Lead to Microservices

Cloud

aPaaS

Continuous Delivery

Dev Ops

Agile

Microservices

Page 4: Developing Microservices using Spring - Beginner's Guide

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

Page 5: Developing Microservices using Spring - Beginner's Guide

Monolith Application

● Imaginary E-Commerce App

● Very efficient inter process calls between components

● Good architecture, until it needs to scale during busy shopping season.

Page 6: Developing Microservices using Spring - Beginner's Guide

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

Page 7: Developing Microservices using Spring - Beginner's Guide

Monolith Application – Good Parts

● Contains Everything

● Single Deployment

● Minimum viable product can

be reached quickly

● Easy (or Familiar) to understand

Page 8: Developing Microservices using Spring - Beginner's Guide

Monolith Application – Bad Parts

● Complex Architecture

● Difficult to Scale

(cannot scale only selected

modules)

● Long term commitment

to one technical stack

Page 9: Developing Microservices using Spring - Beginner's Guide

Microservices

● Distillation of SOA

● Single Context

● Smart Services / Dumb Pipes

● RESTful Communications

● Smaller codebase – easy to reason about

● Easy to scale

Page 10: Developing Microservices using Spring - Beginner's Guide

Microservices

Page 11: Developing Microservices using Spring - Beginner's Guide

Challenges of distributed system

● Configuration Management● Service Registration & Discovery● Load Balancing● Fault Tolerance● Monitoring● Concurrent API Integration & Transformation

*(This is not a complete list...)

Page 12: Developing Microservices using Spring - Beginner's Guide

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)

Page 13: Developing Microservices using Spring - Beginner's Guide

Spring Boot

● It can get pretty small..

@RestController

class ThisWillActuallyRun {

@RequestMapping("/")

String home() {

"Hello World!"

}

}

Exercise 1 : Spring Boot Hello World

Page 14: Developing Microservices using Spring - Beginner's Guide

Spring Boot● Retrieve data from database – Spring Data REST

● Entity Class

@Entity@Table(name="users")public class User implements Serializable{... }

● Repository Class

@Repositorypublic interface UserRepository extends JpaRepository<User, Integer> { }

● Main Class

@SpringBootApplicationpublic 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

Page 15: Developing Microservices using Spring - Beginner's Guide

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.

Page 16: Developing Microservices using Spring - Beginner's Guide

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

Page 17: Developing Microservices using Spring - Beginner's Guide

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

Page 18: Developing Microservices using Spring - Beginner's Guide

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

Page 19: Developing Microservices using Spring - Beginner's Guide

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

Page 20: Developing Microservices using Spring - Beginner's Guide

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

Page 21: Developing Microservices using Spring - Beginner's Guide

Monitoring

● Hystrix Dashboard

Import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard

@EnableHystrixDashboardclass HystrixDashboard { … }

Exercise 7 : Hystrix Dashboard in action

Page 22: Developing Microservices using Spring - Beginner's Guide

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 !).

Page 23: Developing Microservices using Spring - Beginner's Guide

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.

Page 24: Developing Microservices using Spring - Beginner's Guide

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/