consumer-driven contracts: avoid microservices integration hell! (mucon london - nov 2016)

21
Consumer-driven contracts Avoid microservices integration hell! @PierreVincent Nov 8th, 2016

Upload: pierre-vincent

Post on 15-Apr-2017

388 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Consumer-driven contractsAvoid microservices integration hell!

@PierreVincent

Nov 8th, 2016

Page 2: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Pierre VincentTechnical Team Lead at Newsweaver

techblog.newsweaver.com

@PierreVincent

Page 3: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

From this... … to this.

Page 4: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

LoginService

UserService

APIGET /users/pierre

{ "user": "pierre", "name": "Pierre Vincent", "role": "publisher"}

200 OK

How do we test this?

Page 5: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Running in Prod Tests Pass in Build

Implement changes

Deployed in Prod

User Service

LoginService

!

✓ ✓ ✓ ✓

{ ... "role": "editor"}

{ ... "roles": ["publisher","editor"]}

Running in Prod

✓ ✓

Page 6: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Maybe we should have tested before deploying to production…?

Tested what though?

Page 7: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

UsersService

LoginService

IP CheckService

Token Gen

Service

Cert/KeyService

LoginFrontend

We only wanted to

test this bit!

Page 8: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Contract

LoginService

UserService

API

Authentication Team Users Team

Page 9: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

[docs.pact.io]

PACT Specification

Verification philosophy: Tolerant Reader

Implementation guidelines

Implementations

Page 10: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

LoginService

UserService

API

PactMock

Server

Authentication Team Users Team

PactConsumerUnit Test

Define interaction

Trigger interaction

GeneratePact

PactProvider

Test

SharePact

Replay interaction

Replay & Verify

Play &Record

Page 11: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Consumer

Provider

Provider State

Request

ExpectedResponse

Login Service

User Service

Given that user 'pierre' exists

Method GETPath /users/pierreHeaders

Accept: application/json

Status 200Headers

Content-Type: application/jsonBody

{ "user": "pierre", "name": "Pierre Vincent", "role": "publisher"}

Interaction

Page 12: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

UserService

API

1. Set Provider State

2. Send Request

3. Verify Response

Interaction Verification Test

INSERT INTO users [...]

GET /users/pierreAccept: application/json

{ "user": "pierre", ...}

{ "user": "pierre", ...}

?

Page 13: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

AuthenticationDev Team

UsersDev Team

CMSDev Team

BillingDev Team

Pact Broker

PACTPACT PACTPACT

PACTPACT

PACTPACT

PACT

PACT

Page 14: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Pact Broker

DependencyGraphs

Living documentation by example

✓ Versioning

✓ Tagging

✓ REST ApiBuild/Deployment Pipeline integration

Page 15: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Provider pipeline

Implement changes Get Pacts from Broker Replay & Verify Interactions

Deploy Service

Build

Deploy to EU

PROD-EU

Get Pacts from Broker Replay & Verify Interactions

Stop deployment of incompatible Provider

Stop introduction of breaking change

PROD-US

PROD-EU

Page 16: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Consumer pipeline

Implement changes

Generate Pacts (Build)

Push Pacts to Broker Tag Pacts

Each Provider verifies Pacts Deploy Service Tag Pacts

Build

Deploy to EU

HEAD

Stop deployment of incompatible

Consumer

HEAD

PROD-EU

Page 17: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Caution: side-effects may include teams collaborating on API design

Page 18: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Provider state setup for each interaction

Confidence comes from coverage:don’t limit to happy paths

Automation withindeployment pipeline isn’t trivial

What’s the catch…?

Page 19: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

After 2 years of CDCs...

Increased confidence when coding & deploying

✓Collaborative API design

✓Living API documentation

Page 20: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

Pact docs.pact.io, github.com/pact-foundationPact Broker github.com/bethesque/pact_broker

Try it out!

“Pact Matrix” (Beth Skurrie) rea.tech/enter-the-pact-matrix-or-how-to-decouple-the-release-cycles-of-your-microservices

Why should you use CDC for microservices integration teststechblog.newsweaver.com/why-should-you-use-consumer-driven-contracts-for-microservices-integration-tests

Sharing CDCs with Pact Brokertechblog.newsweaver.com/sharing-consumer-driven-contracts-with-pact-broker

More reading...

Page 21: Consumer-driven contracts: avoid microservices integration hell! (MuCon London - Nov 2016)

techblog.newsweaver.com

Questions?

@PierreVincent