microservices with netflix oss & hypermedia apis - javaday kiev

78
@andreasevers Microservices & Hypermedia APIs

Upload: jworks-powered-by-ordina

Post on 16-Apr-2017

560 views

Category:

Engineering


15 download

TRANSCRIPT

Page 1: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Microservices & Hypermedia APIs

Page 2: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

WHOAMI

• Work for Ordina Belgium

•  Open source enthusiast

•  Spring contributor

•  Speaker

•  Technical lead & coding architect @ Proximus

• Marathon runner

Page 3: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 4: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Benefits

• Small, easy to understand code base • Easy to scale • Easy to throw away • Easy to deploy • Ability to use a different technology stack • Smaller teams • System resilience

Page 5: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Pitfalls

“If you can't build a monolith, what makes you think microservices are the answer?”

Simon Brown

Page 6: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Pitfalls

• Failing to adopt a contract-first approach • Assuming the wrong communication protocol •  Introducing a shared domain model • Defining inappropriate service boundaries • Neglecting DevOps and testing concerns • Disregarding the human factor • Operational complexity not under control • Failing to embrace eventual consistency

Page 7: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Netflix OSS

Page 8: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Gateway

Page 9: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Gateway – What’s the use? •  Surgical Routing

Page 10: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

•  Surgical Routing •  Stress Testing •  Canary Testing

Gateway – What’s the use?

Page 11: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Gateway

µS µS µS µS µS µS

Page 12: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

•  Surgical Routing •  Stress Testing •  Canary Testing •  Request authentication & authorization •  Choosing origin servers

Gateway – What’s the use?

Page 13: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

•  Surgical Routing •  Stress Testing •  Canary Testing •  Request authentication & authorization •  Choosing origin servers •  Routing the request to an origin •  Logging debug info •  Adding headers to the request and response •  Gathering statistics and metrics •  Filter error handling •  Generate static responses

Gateway – What’s the use?

Page 14: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

•  Surgical Routing •  Stress Testing •  Canary Testing •  Request authentication & authorization •  Choosing origin servers •  Routing the request to an origin •  Logging debug info •  Adding headers to the request and response •  Gathering statistics and metrics •  Filter error handling •  Generate static responses •  Load Shedding

Gateway – What’s the use?

Page 15: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

•  Surgical Routing •  Stress Testing •  Canary Testing •  Request authentication & authorization •  Choosing origin servers •  Routing the request to an origin •  Logging debug info •  Adding headers to the request and response •  Gathering statistics and metrics •  Filter error handling •  Generate static responses •  Load Shedding •  Dynamic behavior change

Gateway – What’s the use?

Page 16: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 17: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 18: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 19: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Service Registry

Service Registry

loyalty

user billing

billing’

loyalty user user origin

Origin 1

Origin 2 billing

loyalty origin

Page 20: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Service Registry

Service Registry

loyalty

user billing

billing’

loyalty user

billing user origin

Origin 1

Origin 2

loyalty origin

Page 21: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

billing

Service Registry

Service Registry

loyalty

billing

billing’

loyalty user user origin

loyalty origin

Origin 1

user

billing’

billing Origin 2

Page 22: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Service Registry

Service Registry

loyalty user user origin

loyalty origin

Origin 1 billing Origin

2

Service Registry

loyalty user user origin

loyalty origin

Origin 1 billing Origin

2

loyalty

Cached Registry

Page 23: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Service Registry

Page 24: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 25: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 26: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker

Backend µS

Page 27: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker

Backend µS

Page 28: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker Gateway

µScustomer µSuser µSloyalty µScustomer µSloyalty

Backends

Page 29: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker - Fallbacks

Page 30: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker

Backend µS

stream

information

Page 31: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker - Dashboard

Page 32: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker - Dashboard

Page 33: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Circuit Breaker - Dashboard

Page 34: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Config

µScustomer

µSuser

µSloyalty

Config Server

Page 35: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 36: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 37: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 38: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 39: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 40: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 41: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 42: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Metrics & Admin

Page 43: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Contracts & loose coupling We can achieve this by using Hypermedia

Page 44: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hypermedia

Hypermedia As The Engine Of Application State

Page 45: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hypermedia

h8ps://vimeo.com/20781278

Sub-constraints:•  IdenDficaDonofresources(URIs)•  ManipulaDonviarepresentaDons(request&

responsebodies)

•  Self-descripDvemessages(headers)•  HypermediaastheengineofapplicaDonstate

HTTPasapplica+onprotocol

Page 46: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hypermedia

h8ps://vimeo.com/20781278

Sub-constraints:•  IdenDficaDonofresources(URIs)•  ManipulaDonviarepresentaDons(request&

responsebodies)

•  Self-descripDvemessages(headers)•  HypermediaastheengineofapplicaDonstate

Ifyoudon’tdothis

Thenyoudon’tadheretothis

Andyouaremissingoutonthese

Page 47: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Why Hateoas?

• Updating server-side web APIs only to learn that client applications no longer work as expected without undergoing code updates

• Moving long-lived server applications to a new DNS name (e.g. from www.belgacom.be to www.proximus.be) and having to completely rewrite all of the API documentation as well as update all existing client code with all its links to the server’s APIs

•  Implementing new or modified process flow within the server-side application and discovering that existing clients break when encountering the new rules, ignore the rules, or, worse, continue to execute their own code in a way that creates invalid results on the server

Page 48: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas In Action

Page 49: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 50: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 51: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 52: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 53: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Page 54: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas in action

How would you explain to a client to get to the Nerd in the Basement painting? A.  Go to Amazon.com, in the categories go to fine arts, follow

paintings, more specifically oil paintings, and click on the one with the title Nerd in the Basement

B.  Type http://www.amazon.com/Nerd-in-the-Basement/dp/B00L849CSS/ref=lp_6685279011_1_2?s=art&ie=UTF8&qid=1431864368&sr=1-2 in your browser

Page 55: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas in action

HTML is a hypermedia format <a> is a link with method GET <form> is a link with method POST (or other if specified)

The browser understands this syntax and shows a link or a form if the server response contains these tags

Page 56: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Requirements

Communication between Client and Server depends on:

• Where does the client have to start? •  Root API •  In regular websites: the homepage

• Where am I? •  How do I interpret the current API response? •  In regular websites: the syntax of HTML is interpreted by the browser

• Where can I go? •  What does a link or form with a certain relation or class mean? •  In regular websites: link with relation “stylesheet”, form with action “login”

Page 57: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas in action

Amazon.com (and any other website in the whole world wide web) applies Hateoas. Why wouldn’t your API do the same?

Page 58: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Benefit: Runtime action discovery

GET /account/12345 HTTP/1.1 HTTP/1.1 200 OK <?xml version="1.0"?> <account>

<account_number>12345</account_number> <balance currency="usd">100.00</balance> <link rel="deposit" href="/account/12345/deposit" /> <link rel="withdraw" href="/account/12345/withdraw" /> <link rel="transfer" href="/account/12345/transfer" /> <link rel="close" href="/account/12345/close" />

</account>

Page 59: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Benefit: Runtime operation discovery

GET /account/12345 HTTP/1.1 HTTP/1.1 200 OK <?xml version="1.0"?> <account>

<account_number>12345</account_number> <balance currency="usd">-25.00</balance> <link rel="deposit" href="/account/12345/deposit" />

</account>

Page 60: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Concern: Scope

In case of one or two clients built in the same team, it is arguable whether auto-discoverability is really a necessity

Page 61: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Benefit: Non-structural Changes

“customers/1/accounts/1/products/1234” auto-discoverable through HATEOAS as “customers[1].accounts[1].products[1234]” will not break when 1234 as id is changed to “basementNerd”

Page 62: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Concern: Structural Changes

“customers/1/accounts/1/products/1234” auto-discoverable through HATEOAS as “customers[1].accounts[1].products[1234]” could break when accounts are bypassed

Page 63: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Hateoas Benefit: Changing the URI of a resource “customers/1/accounts/1/products/1234” being returned as part as the response body of “customers/1/accounts/1” will not break the client

Page 64: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Content Types

"text/html" •  Browsers know how to parse it •  Browsers understand keywords inside it

•  E.g: a + href , form + action + method , ...

"application/json" or "application/xml“

•  Clients know how to parse it •  Clients don’t understand keywords inside it •  Needs a uniform format as communication between client & server •  Needs a reference for out-of-bound (api-specific) keywords

Page 65: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Content Types

•  JSON •  NOT hypermedia-aware by default •  Needs a fixed format to support links and forms •  Many formats available

• XHTML •  IS hypermedia-aware by default •  Harder to process XHTML responses using javascript (xpath is required) •  The API responses can also be read by a human as regular HTML pages

• SVG, Atom, HTML •  Similar as XHTML but not preferred

Page 66: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

JSON Formats •  JSON-LD

•  Augmenting existing APIs without introducing breaking changes •  Needs HYDRA as a vocabulary for communicating operations •  Decoupling of API serialization format & communication format

•  HAL •  Minimal, light weight syntax and semantics •  Offers most of the benefits of using a hypermedia type •  Easy to convert existing API to HATEOAS •  Chosen and supported by Spring •  No support for specifying operations

•  Collection+JSON •  Can list queries that your collection supports and templates that clients can use to alter your

collection •  Great for publishing user editable data

•  SIREN •  Represents generic classes of items •  Supports operations •  Concept of classes, bringing a sense of type information to your API responses.

Page 67: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Considerations

Maturity

Client implementation

Caching

Versioning

Page 68: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Documentation

h8ps://speakerdeck.com/ankinson/documenDng-resTul-apis-webinar

Page 69: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

What should you document

Resources

Links

Cross-cutting concerns

Page 70: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

What shouldn’t you document

URIs

Page 71: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

What does it look like when you get it wrong?

Page 72: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

What does it look like when you get it right?

Page 73: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Swagger

Doesn’t support Hypermedia

Page 74: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Swagger

It’s URI centric

Page 75: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Swagger

It’s leaky

Page 76: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Swagger

It’s huge

Page 77: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Best practices for documentation

Write as much as possible in a format which is designed for writing Don’t use the implementation to provide the documentation Provide some guarantees that the documentation is accurate

h8ps://github.com/spring-projects/spring-restdocs

Page 78: Microservices with Netflix OSS & Hypermedia APIs - JavaDay Kiev

@andreasevers

Thank you for your attention

@andreasevers

https://github.com/oraj-360 http://registry.oraj360.cfapps.io/ https://netflix.github.io/ http://projects.spring.io/spring-cloud/ http://projects.spring.io/spring-hateoas/ https://github.com/spring-projects/spring-restdocs