Slide 1

Slide 1 text

Microservices with Java, Spring Boot & Spring Cloud Eberhard Wolff Fellow, innoQ @ewolff

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

http://microservices-buch.de/ http://microservices-book.com/

Slide 4

Slide 4 text

http://microservices-book.com/primer.html FREE!!!!

Slide 5

Slide 5 text

Microservice Definition

Slide 6

Slide 6 text

Server Server Microservices: Definition > Small > Independent deployment units > Any technology > Any infrastructure Micro Service Micro Service

Slide 7

Slide 7 text

Components Collaborate Micro Service Micro Service Link Data Replication REST Messaging

Slide 8

Slide 8 text

Infrastructure > …must support lots of services > Easy to create a new project > REST integrated > Messaging supported > Uniform operations

Slide 9

Slide 9 text

Spring Boot Demo

Slide 10

Slide 10 text

Simple Infrastructure > One pom.xml > …or Gradle / Ant > Very few dependencies > One plug in > Versions defined

Slide 11

Slide 11 text

REST Integrated > Support in Spring MVC > As we have seen > Also: JAX-RS > Jersey

Slide 12

Slide 12 text

Messaging Support > Numerous Spring Boot Starter > AMQP (RabbitMQ) > HornetQ (JMS) > ActiveMQ Artemis (JMS)

Slide 13

Slide 13 text

Messaging Support > Spring JMS abstraction > Message driven POJOs > Scalable > Simplify sending JMS > Can use other libs, too! > Boot: everything Spring / Java can do

Slide 14

Slide 14 text

Infrastructure > …must support lots of services > Easy to create a new project > REST integrated > Messaging supported > Simple deployment > Uniform operations ✓ ✓ ✓

Slide 15

Slide 15 text

Deploy > Package everything in an executable JAR > …or a WAR > Based on Maven, Ant or Gradle > Add configuration

Slide 16

Slide 16 text

Spring Boot Deploy Demo

Slide 17

Slide 17 text

Deploy > Install a basic machine > Install Java > Copy over JAR > Optional: Make it a Linux Service > …just a link > Optional: Create application.properties

Slide 18

Slide 18 text

Infrastructure > …must support lots of services > Easy to create a new project > REST integrated > Messaging supported > Simple deployment > Uniform operations ✓ ✓ ✓ ✓

Slide 19

Slide 19 text

Spring Boot Actuator > Provide information about the application > Via HTTP / JSON > …or Metrics > Can be evaluated by monitoring tools etc. > E.g. Graphite, Grafana ….

Slide 20

Slide 20 text

Spring Boot Actuator Demo

Slide 21

Slide 21 text

> https://github.com/ewolff/user-registration-V2 > Logging ELK > Subdir log-analysis > Monitoring Graphite > Subdir graphite

Slide 22

Slide 22 text

Infrastructure > …must support lots of services > Easy to create a new project > REST integrated > Messaging supported > Simple deployment > Uniform operations ✓ ✓ ✓ ✓ ✓

Slide 23

Slide 23 text

Spring Cloud

Slide 24

Slide 24 text

Based on Spring Boot

Slide 25

Slide 25 text

Supports many technology stacks, Clouds, …

Slide 26

Slide 26 text

Focus: Netflix Stack

Slide 27

Slide 27 text

Spring Cloud Netflix Zuul Routing Ribbon Client Side Load Balancing Eureka Service Discovery Hystrix Resilience

Slide 28

Slide 28 text

Coordinating Microservices

Slide 29

Slide 29 text

> Must find each other Microservice Microservice

Slide 30

Slide 30 text

Service Discovery Eureka

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Eureka Client in Spring Cloud > @EnableDiscoveryClient: generic > @EnableEurekaClient: more specific > Dependency to spring-cloud-starter-eureka > Automatically registers application

Slide 33

Slide 33 text

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}:${r andom.value} eureka.instance.preferIpAddress=true Eureka server Can include user / password Need unique ID Load balancing Faster updates Dockerwon’t resolve host names Used for registration In CAPITAL caps

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

> Must find each other > Route calls to a service Microservice Microservice

Slide 37

Slide 37 text

Zuul Routing

Slide 38

Slide 38 text

Routing > One URL to the outside > Internal: Many Microservices > In particular: REST > Power through filters

Slide 39

Slide 39 text

Customer Order Catalog Zuul Proxy Automatically maps route to server registered on Eureka i.e. /customer/** to CUSTOMER No configuration Can add filters etc

Slide 40

Slide 40 text

Zuul Proxy @SpringBootApplication @EnableZuulProxy public 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

Slide 41

Slide 41 text

> Must find each other > Route calls to a service > Configuration Microservice Microservice

Slide 42

Slide 42 text

Spring Cloud Config

Slide 43

Slide 43 text

Configuration > Spring Cloud Config > Central configuration > Dynamic updates > Can use git backend > I prefer immutable server > & DevOps tools (Docker, Chef…)

Slide 44

Slide 44 text

Spring Cloud Bus > Pushed config updates > …or individual message > I prefer a messaging solution > Independent from Spring

Slide 45

Slide 45 text

> Must find each other > Route calls to a service > Configuration > Security Microservice Microservice

Slide 46

Slide 46 text

Spring Cloud Security

Slide 47

Slide 47 text

Spring Cloud Security > Single Sign On via OAuth2 > Forward token e.g. via RestTemplate > Support for Zuul > Very valuable!

Slide 48

Slide 48 text

Implementing Microservices

Slide 49

Slide 49 text

Microservice Microservice > Load Balancing

Slide 50

Slide 50 text

Load Balancing Ribbon

Slide 51

Slide 51 text

Proxy Load Balancing > Centralized Load Balancing > Can become bottle neck > Single point of failure > Configuration complex Load Balancer Server Client

Slide 52

Slide 52 text

Client Ribbon: Client Side Load Balancing > Decentralized Load Balancing > No bottle neck > Resilient > Can consider response time > Data might be inconsistent Load Balancer Server

Slide 53

Slide 53 text

RestTemplate & Load Balancing @RibbonClient(name = "ribbonApp") … public class RibbonApp { @Autowired private 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

Slide 54

Slide 54 text

Microservice Microservice > Load Balancing > Resilience

Slide 55

Slide 55 text

Hystrix Resilience

Slide 56

Slide 56 text

Hystrix > Enable resilient applications > Do call in other thread pool > Won’t block request handler > Can implement timeout

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Hystrix / Spring Cloud > Annotation based approach > Annotations of javanica libraries > Java Proxies automatically created > Simplifies Hystrix dramatically > No commands etc

Slide 59

Slide 59 text

@HystrixCommand(fallbackMethod = "getItemsCache") public Collection findAll() { … this.itemsCache = pagedResources.getContent(); return itemsCache; } private Collection getItemsCache() { return itemsCache; } Fallback

Slide 60

Slide 60 text

Stream via http Circuit Breaker status Thread Pool status

Slide 61

Slide 61 text

Conclusion

Slide 62

Slide 62 text

Infrastructure > Easy to create a new project > REST integrated > Messaging supported > Simple deployment > Uniform operations

Slide 63

Slide 63 text

Spring Cloud > Eureka: Service Discovery > Zuul: Route calls to a service > Spring Cloud Config: Configuration > Ribbon: Load Balancing > Hystrix: Resilience

Slide 64

Slide 64 text

Links > https://github.com/ewolff/microservices > https://github.com/ewolff/spring-boot-demos > https://github.com/ewolff/user-registration-V2 > http://projects.spring.io/spring-boot/ > http://projects.spring.io/spring-cloud > https://spring.io/guides/ > http://microservices-book.com

Slide 65

Slide 65 text

Thank You!! @ewolff