microservices i praktiken - callistaenterprise.se · operations component net!ix, spring, elk...

44
| CALLISTAENTERPRISE.SE MICROSERVICES I PRAKTIKEN MAGNUS LARSSON 2015.05.21 från tröga monoliter till en arkitektur för kortare ledtider, högre skalbarhet och ökad feltolerans 1

Upload: others

Post on 22-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

| CALLISTAENTERPRISE.SE

MICROSERVICES I PRAKTIKEN

MAGNUS LARSSON

2015.05.21

från tröga monoliter till en arkitektur för kortare ledtider, högre skalbarhet och ökad feltolerans

1

Page 2: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  What’s the problem? •  New solutions to old problems... •  What’s a microservice?

•  New challenges with microservices •  Implementing microservices •  Demonstration

AGENDA

2

Page 3: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Well known problems with monolithic applications -  Poor scalability and resilience -  Long release cycles

•  ...we hade tried to solve these problems before (and failed?)...

•  But there are new opportunities now!

WHAT’S THE PROBLEM??

ArchiMate 2.0 syntax

Node

Process

Business Function

3

Page 4: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Microservices was first heard of in May 2011

•  Success stories from early adopters migrating from monoliths to microservices

- Amazon (http://goo.gl/LfsD67) -  eBay (http://goo.gl/dodV2c) - Gilt (http://goo.gl/yVVox9)

- Groupon (https://goo.gl/uKTtAs) - Karma (https://goo.gl/kXObAO)

- Netflix: Part 1, part 2 and Fast Delivery (https://goo.gl/MVgHM1, https://goo.gl/fDeZ5A, https://goo.gl/hN6ZCL)

-  SoundCloud: Part 1, part 2 and part 3 (https://goo.gl/Xq0Cgm, https://goo.gl/swJ8Vt, https://goo.gl/J2oN8I)

HISTORY OF MICROSERVICES

4

Page 5: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

THE SCALABILITY CHALLENGE…

5 Source: http://www.theconnectivist.com/2014/05/infographic-the-growth-of-the-internet-of-things/

Page 6: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Strong trend moving from “Big Iron” to many small servers - Typically virtual servers -  In cloud or/and on premises -  Better price/performance

NEW SOLUTIONS TO OLD PROBLEMS

6

Page 7: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Cloud computing makes it easier to manage many small servers -  IaaS: Infrastructure as a Service

»  Deliver virtual servers »  E.g. Amazon EC2, Windows Azure, Google Compute Engine et. al.

-  PaaS: Platform as a Service »  Deliver an application platform »  E.g. Heroku, Red Hat OpenShift, Pivotal Cloud Foundry et. Al. »  Note: Some PaaS can be used on premises, e.g. OpenShift and Cloud Foundry

-  Docker, the Container revolution... »  IaaS + PaaS ! CaaS ?

NEW SOLUTIONS TO OLD PROBLEMS

7

Page 8: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  How to fit monolithic applications in a number of small boxes?

NEW SOLUTIONS TO OLD PROBLEMS

8

?

Page 9: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  We need to split the monolith to make it fit... NEW SOLUTIONS TO OLD PROBLEMS

9

!

Page 10: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Splitting the monolith also makes it easier to scale... - Auto scaling provided by platforms

NEW SOLUTIONS TO OLD PROBLEMS

10

!

Page 11: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  A software component that is independently replaceable and upgradeable

•  Share nothing architecture - They don’t share databases! - Only communicate through well defined interfaces,

»  E.g. REST services or queuing mechanisms

•  Typically deployed as separate runtime processes

WHAT’S A MICROSERVICE?

11

Page 12: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

WHAT’S A MICROSERVICE?

12

Page 13: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

WHAT’S A MICROSERVICE?

Shorter release cycles So much easier to update or replace a microservice compared to a monolith.

13

Page 14: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

WHAT’S A MICROSERVICE?

How big is a microservice? •  Small enough to fit in

the head of a developer

•  Big enough to not jeopardize •  Performance •  Data consistency

14

Page 15: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

HOW DOES MICROSERVICES FIT INTO AN EXISTING SYSTEM LANDSCAPE?

16

External API Platform

Monolith A Monolith B Monolith C

Enterprise Service Bus

Partner Applications

Page 16: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Application level - An application is built up by

a number of microservices »  Deployed on a number of nodes

- Microservices are typically not exposed outside of the application

»  Dedicated Edge-services handles requests from the outside (internal and external)

»  Internal microservices can be upgraded and replaced quickly

WHAT’S A MICROSERVICE?

Internal API

External API

Application

17

OAuth 2.0

Page 17: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  System landscape level... - Coexistence with

existing applications

-  If a SOA is applied, typically an ESB is in place...

-  If an API platform is applied, external calls comes through it

WHAT’S A MICROSERVICE?

External API Platform

Monolith A Monolith B

Enterprise Service Bus

Application C (based on microservices)

18

Page 18: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

WHAT’S A MICROSERVICE?

External API Platform

Monolith A Monolith B

Enterprise Service Bus

Application C (based on microservices)

•  SOA vs. Microservices

-  SOA and microservices don’t conflict, they complement each other!

-  SOA is about how to reuse existing functionality as services...

-  Microservices is about how to make functionality to scale better with high resilience and short release cycles...

SOA

Micro- services

19

Page 19: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Managing large numbers of microservices... - Where are they and are they ok???

NEW CHALLENGES WITH MICROSERVICES

20

Page 20: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  What went wrong???

NEW CHALLENGES WITH MICROSERVICES

21

Page 21: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Minor effect if a small microservice fails than a big monolith... NEW CHALLENGES WITH MICROSERVICES - RESILIENCE

22

VS

Page 22: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

NEW CHALLENGES WITH MICROSERVICES - RESILIENCE

23

? •  Beware of chain reactions... - A.k.a “chain of failures”

Page 23: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

NEW CHALLENGES WITH MICROSERVICES - RESILIENCE

24

•  Beware of chain reactions... - A.k.a “chain of failures”

Circuit Breaker to the rescue! •  Prevents calls when too many

errors are observed

•  Directs the call to a fallback method

•  Retries the call periodically

Page 24: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Managing large numbers of microservices requires tools for - Runtime discovery of services

»  New services can auto-register at startup - Dynamic router and load balancer

»  Clients can detect new instances as they are started up - Centralized log management

»  Collects and visualize log events from distributed processes - Circuit breaker

»  Prevent problems with chain of failures -  Protecting external API’s

»  Secure external API’s using OAuth 2.0

NEW CHALLENGES WITH MICROSERVICES

25

Page 25: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Managing large numbers of microservices requires tools for - Runtime discovery of services

»  New services can auto-register at startup - Dynamic router and load balancer

»  Clients can detect new instances as they are started up - Circuit breaker

»  Prevent problems with chain of failures - Centralized log management

»  Collects and visualize log events from distributed processes -  Protecting external API’s

»  Secure external API’s using OAuth 2.0

NEW CHALLENGES WITH MICROSERVICES

Open Source tools to the rescue

26

Page 26: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  What’s the problem? •  New solutions to old problems... •  What’s a microservice?

•  New challenges with microservices •  Implementing microservices •  Demonstration

AGENDA – WHERE ARE WE?

27

Page 27: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Netflix OSS (http://goo.gl/DHOf4o) -  Since 2011, Netflix has been releasing components of

their cloud platform as free and open source software -  Obviously proven in battle...

•  Spring Cloud (http://goo.gl/vHVdEp)

-  Spring Cloud simplifies use of Netflix OSS -  Add own components, e.g. OAuth 2.0 support -  Based on Spring Boot and the “convention over configuration” paradigm

•  The ELK stack (https://goo.gl/aCHlhN) -  Elasticsearch, Logstash and Kibana -  Used for centralized log analyses

IMPLEMENT MICROSERVICES WITH OPEN SOURCE

28

Page 28: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

SOFTWARE COMPONENTS

29

Operations Component Netflix, Spring, ELK Service Discovery server Netflix Eureka Dynamic Routing and Load Balancer

Netflix Ribbon

Circuit Breaker Netflix Hystrix Monitoring Netflix Hystrix dashboard and Turbine Edge Server Netflix Zuul Central Configuration server Spring Cloud Config Server OAuth 2.0 protected API’s Spring Cloud +

Spring Security OAuth2 Centralized log analyses Logstash, Elasticsearch, Kibana (ELK)

Page 29: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  An API for product-information

•  A composite service aggregate information from three core- services

•  Plus infrastructure services for OAuth, Discovery and Edge-servers...

DEMO SYSTEM LANDSCAPE

30

Products

Product Composite

Curl

Product API

Recommendations Reviews

Page 30: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

DEMO SYSTEM LANDSCAPE

31

Legend

•  CB = Circuit Breaker (Netflix Hystrix) •  LB = Load Balancer (Netflix Ribbon)

Curl

Products Recommendations Reviews

Service Discovery

(Netflix Eureka)

Monitor Dashboard

(Netflix Turbine + Hystrix Dashboard)

OAuth Authorization

Server (spring-security)

CB/LB

OAuth token relay

Logging Analyses

(ELK stack)

Edge server (Netflix Zuul)

Product API

CB/LB

OAuth Res

Product Composite

CB/LB

Page 31: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

Product Composite

Curl

Products

Product API

Recommendations Reviews

•  Each microservice log events to local log-file

•  API service marks the request with a correlation-id

•  Correlation-id transferred between microservices

•  Easy to find correlated log-event in the log-event database

CENTRALIZED LOG ANALYSIS

32

corr-id=nnn productId=12345

Page 32: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

$ docker-compose start

DEPLOY

•  In cloud -  Using PaaS: Pivotal Web Services

(https://goo.gl/I3oDGt)

•  On premises

-  Using Docker -  Sample configuration file

33

$ cf push

-  Sample configuration file --- memory: 512M instances: 1 applications: - name: product-api-service path: product-api.jar

discovery: image: callista/discovery-server pro: image: callista/product-service links: - discovery

Page 33: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

DEPLOY

•  In cloud -  Using PaaS: Pivotal Web Services

•  On premises -  Using Docker

-  Sample configuration file

34

$ docker-compose start

$ cf push

-  Sample configuration file --- memory: 512M instances: 1 applications: - name: product-api-service path: product-api.jar

discovery: image: callista/discovery-server pro: image: callista/product-service links: - discovery

Java-jar files and Docker images are created by build scripts

Page 34: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Discovery server

•  Centralized log analysis

•  Scale up

•  Resilience

DEMO SLIDES

35

Page 35: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

THE DISCOVERY SERVER

36

Page 36: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

CENTRALIZED LOG ANALYSIS – KIBANA

37

Page 37: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Let’s scale up one of the services

SCALE UP

38

$ docker-compose scale rec=2 ... $ docker-compose ps Name ------ api_1 rec_1 rec_2 ... Products

Product Composite

Product API

Reviews Recommendations

Page 38: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  The new service instance in the discovery server SCALE UP

39

Page 39: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Get an access token from the OAuth Authentication Server

•  Call the API with the access token

CALL THE API

40

$ curl -s acme:acmesecret@docker:9999/uaa/oauth/token \ -d grant_type=password -d client_id=acme \ -d username=user -d password=password | jq . {"access_token": "e5863174-6a25-4e4d-9fe0-32532a842d88", ...}

$ curl –s 'http://docker:8765/api/product/12345' \ -H "Authorization: Bearer $TOKEN"| jq . { "productId": 12345, "name": "name", ... "recommendations": [ {...}, {...}, {...} ], "reviews": [ {...}, {...}, {...} ] }

Page 40: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Introduce an error -  The review service stops to response,

requests just hangs until requests timeout •  Try out •  Force the Circuit to open - Coming requests will fast-fail,

i.e. not wait for the timeout!

CIRCUIT BREAKER

41

Products

Product Composite

Recommendations Reviews

Page 41: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Normal calls (circuit closed):

•  Calls with a few timeouts (circuit still closed):

•  Calls with a lot of timeouts (circuit open, i.e. it will fast-fail):

CIRCUIT BREAKER

42

$ curl 'http://docker:8765/api/product/12345’ ... {"productId”: ..., "recommendations": [...], "reviews”:[...]} 0.398 ms

$ curl 'http://docker:8765/api/product/12345’ ... {"productId”: ..., "recommendations": [...], "reviews”:null} 3.295 ms

$ curl 'http://docker:8765/api/product/12345’ ... {"productId”: ..., "recommendations": [...], "reviews”:null} 0.239 ms

Page 42: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  The CAP theorem and distributed systems, eventual consistency... •  Conway’s law requires organizational changes •  Continuous Delivery, a pre-requisite for large-scale use of microservices •  How to apply TDD for microservices?

...WHAT WE DIDN’T HAVE TIME TO TALK ABOUT (THIS TIME)

43

Page 43: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

•  Microservices use new solutions to old problems regarding -  Scalability, resilience, release cycles

•  Microservices is about splitting up monoliths in units of independently replaceable and upgradeable components

•  Uses infrastructure for scaling out on many small servers -  In cloud or on premises

•  New advanced, battle-proven and open source tools for handling challenges with microservices - Netflix OSS, Spring Cloud and the ELK stack

SUMMARY

44

Page 44: MICROSERVICES I PRAKTIKEN - callistaenterprise.se · Operations Component Net!ix, Spring, ELK Service Discovery server Net"ix Eureka Dynamic Routing and Load Balancer Net"ix Ribbon

Q&A

45 ?

•  Want to know more? - Try out our blog series: Building Microservices (http://goo.gl/bSg5mC)