microservice with spring boot and spring cloud

67
Microservices with Spring Boot & Spring Cloud Eberhard Wolff Freelancer / Trainer

Upload: eberhard-wolff

Post on 16-Jul-2015

1.789 views

Category:

Technology


9 download

TRANSCRIPT

Page 1: Microservice With Spring Boot and Spring Cloud

Microservices with

Spring Boot & Spring Cloud

Eberhard Wolff Freelancer / Trainer

Page 2: Microservice With Spring Boot and Spring Cloud

What are Microservices?

Page 3: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Micro Services: Definition•  Small •  Independent deployment units •  i.e. processes or VMs

•  Any technology •  Any infrastructure •  Include GUI

Micro Service

Server

Micro Service

Server

Page 4: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Components Collaborate

Micro Service

Micro Service

Link

Data Replication

REST Messaging

Page 5: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Infrastructure for Microservices

•  Lots of services

•  Need infrastructure Easy to create a new project REST integrated Messaging supported Uniform operations

Page 6: Microservice With Spring Boot and Spring Cloud

Spring Boot Demo

Page 7: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Easy to Create New Project •  One pom.xml •  …Gradle / Ant

•  Very few dependencies •  One plug in •  Versions defined for you

Page 8: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

REST Integrated•  Support in Spring MVC •  As we have seen

•  Also support for JAX-RS •  Jersey

Page 9: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Messaging Support•  Numerous Spring Boot Starter

•  AMQP (RabbitMQ) •  HornetQ (JMS) •  ActiveMQ (JMS, no starter)

Page 10: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Messaging Support•  Spring JMS abstraction •  Message driven POJOs •  Scalable •  Simplify sending JMS

•  Can use other libs, too! •  Boot can do everything plain Spring /

Java can do

Page 11: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Infrastructure for Microservices

•  More services

•  Need infrastructure Easy to create a new project REST integrated Messaging supported Simple deployment Uniform operations

✓ ✓

Page 12: Microservice With Spring Boot and Spring Cloud

Spring BootDeploy Demo

Page 13: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Deploy•  Just package everything in an

executable JAR •  …or a WAR

•  Based on Maven, Ant or Gradle

•  Build in configuration (YAML, properties etc.)

Page 14: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Deploy•  Install a basic machine

•  Install Java

•  Copy over .jar

•  Optional: Create application.properties

Page 15: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Infrastructure for Microservices

•  More services

•  Need infrastructure Easy to create a new project REST integrated Messaging supported Simple deployment Uniform operations

✓ ✓

✓ ✓

Page 16: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Boot Actuator•  Provide information about the

application •  Via http / JSON •  Can be evaluated by monitoring

tools etc. •  Another alternative approach to

monitoring

Page 17: Microservice With Spring Boot and Spring Cloud

Spring BootActuator

Demo

Page 18: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Infrastructure for Microservices

•  More services

•  Need infrastructure Easy to create a new project REST integrated Messaging supported Simple deployment Uniform operations

✓ ✓

✓ ✓ ✓

Page 19: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Deploy•  Just package everything in an

executable JAR

Page 20: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Deploy•  Just package everything in an

executable JAR •  …or a WAR

Page 21: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud

Page 22: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Based on Spring Boot

Page 23: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud•  Spring support for Amazon Web

Services •  Connector for Heroku PaaS •  …and Cloud Foundry PaaS

•  The rest of Spring Cloud is for Microservices

Page 24: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

CoordinatingMicroservices

Page 25: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Must find each other

Page 26: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Service Discovery

Eureka

Page 27: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Why Eureka?•  REST based service registry •  Supports replication •  Caches on the client •  Resilient •  Fast •  …but not consistent •  Foundation for other services

Page 28: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Eureka Client in Spring Cloud•  @EnableDiscoveryClient:

generic •  @EnableEurekaClient:

more specific •  Dependency to

spring-cloud-starter-eureka •  Automatically registers application

Page 29: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

application.properties

eureka.client.serviceUrl.defaultZone=http://host:8761/eureka/<

eureka.instance.leaseRenewalIntervalInSeconds=5<spring.application.name=catalog<eureka.instance.metadataMap.instanceId=$

{spring.application.name}:${random.value}<eureka.instance.preferIpAddress=true<

Eureka server Can include user / password

Need unique ID Load balancing

Faster updates

Docker won’t resolve host names

Used for registration In CAPITAL caps

Page 30: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Eureka Server

@EnableEurekaServer<@EnableAutoConfiguration<public'class'EurekaApplication'{'<<public'static'void'main(String[]'args)'{'<<SpringApplication.run(EurekaApplication.class,'args);'<}<

<}<

Add dependency to spring-cloud-starter-eureka-server

Page 31: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Page 32: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Eureka Demo

Page 33: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Must find each other

Route calls to a service

Page 34: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Zuul Routing

Page 35: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Routing•  One URL to the outside

•  Internal: Many Microservices

•  REST

•  Or HTML GUI

Page 36: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Customer Order Catalog

Zuul Proxy

Automatically maps route to server registered on Eureka i.e. /customer/** to CUSTOMER No configuration

Can add filters etc

Page 37: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Zuul Proxy

@SpringBootApplication@EnableZuulProxypublic class ZuulApplication {

public static void main(String[] args) { new SpringApplicationBuilder(ZuulApplication.class).

web(true).run(args);}

}

Enable Zuul Proxy

Can change route Also routing to external services possible

Page 38: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

lokaler Rechner

Vagrant VM

eureka zuul

customer-app

catalog-apporder-app

172.17.0.0/16 Netzwerk

8761

8761

8080

8080

18761 18080

Page 39: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Zuul Demo

Page 40: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Must find each other

Configuration

Route calls to a service

Page 41: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Configuration•  Spring Cloud Config •  Central configuration •  Dynamic updates •  Can use git backend

•  I prefer immutable server •  & DevOps tools (Docher, Chef…)

Page 42: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Must find each other

Configuration

Route calls to a service

Communication

Page 43: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud Bus

Page 44: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud Bus•  Pushed config updates •  …or individual message

•  I prefer a messaging solution •  Independent from Spring

Page 45: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Must find each other

Configuration

Route calls to a service

Communication

Security

Page 46: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud Security

Page 47: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Cloud Security•  Single Sign On via OAuth2 •  Forward token e.g. via

RestTemplate •  Support for Zuul

•  Very valuable!

Page 48: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

ImplementingMicroservices

Page 49: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Load Balancing

Page 50: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Load BalancingRibbon

Page 51: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Ribbon: Client Side Load Balancing

•  Decentralized Load Balancing •  No bottle neck •  Resilient •  Hard to consider metrics / health •  Data might be inconsistent

Load Balancer

Server Client

Page 52: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

RestTemplate & Load Balancing

@RibbonClient(name = "ribbonApp")…public class RibbonApp {

@Autowiredprivate RestTemplate restTemplate;

public void callMicroService() {Store store = restTemplate. getForObject("http://stores/store/1",Store.class);}

}

Enable Ribbon

Left out other annotations

Eureka name or server list

Standard Spring REST client

Can also use Ribbon API

Page 53: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Microservice Microservice

Load Balancing

Resilience

Page 54: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix Resilience

Page 55: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix•  Enable resilient applications

•  Do call in other thread pool •  Won’t block request handler •  Can implement timeout

Page 56: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix•  Circuit Breaker •  If call system fail open •  If open do not forward call •  Forward calls after a time window

•  System won’t be swamped with requests

Page 57: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix / Spring Cloud•  Annotation based approach •  Java Proxies automatically created •  Annotations of javanica libraries

•  Simplifies Hystrix dramatically •  No commands etc

Page 58: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

@HystrixCommand(fallbackMethod = "getItemsCache")public Collection<Item> findAll() {

…this.itemsCache = pagedResources.getContent();return itemsCache;

}

private Collection<Item> getItemsCache() {return itemsCache;

}<

Fallback

Page 59: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

lokaler Rechner

Vagrant VM

eureka zuul

customer-app

catalog-apporder-app

172.17.0.0/16 Netzwerk

8761

8761

8080

8080

18761 18080

Page 60: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

lokaler Rechner

Vagrant VM

eureka zuul

customer-app

catalog-app

turbine

order-app

172.17.0.0/16 Netzwerk

8761

8761

8989

8989

8080

8080

18761 18989 18080

Page 61: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix DashboardStream via http

Circuit Breaker status

Thread Pool status

Page 62: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Conclusion

Page 63: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Spring Boot for Microservices Easy to create a new project REST integrated Messaging supported Simple deployment Uniform operations

✓ ✓

✓ ✓ ✓

Page 64: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Hystrix Demo

Page 65: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Must find each other: Service Discovery

Configuration

Route calls to a service

Communication

Load Balancing

Resilience

Spring Cloud for Microservices

Page 66: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Linkshttp://projects.spring.io/spring-boot/ http://projects.spring.io/spring-cloud https://github.com/ewolff/spring-boot-demos https://github.com/ewolff/microservices https://spring.io/guides/

Page 67: Microservice With Spring Boot and Spring Cloud

Eberhard Wolff - @ewolff

Thank You!!