Consumer Driven Contracts for
Microservices
By Reshmi Krishna By Vinay Upadhya
@reshmi9k @vinayu
About Us
Reshmi Krishna
● Software Engineer
● Platform Architect, Pivotal Cloud Foundry
● Women In Tech Community Member
Twitter : @reshmi9k
MeetUp : Cloud-Native-New-York
Vinay Upadhya
● Software Engineer
● Platform Architect, Pivotal Cloud Foundry
● Application Architecture Evangelist
Twitter : @vinayu
Agenda
What are Consumer Driven Contracts?
What problems are we trying to solve?
Spring Cloud Contract for microservices
Demo
What are Consumer Driven Contracts?
Internal
Marketing
App
External
Reseller
App
Product
Search
Service
A
P
I
{ catalogid, name,
price,
manufacturer }
{ catalogid,
name, price }
Product Schema
{ catalogid, name, price,
manufacturer,
inStock }
Producer
Contract
Expectation
Consumer
Contract
Consumer Driven
Contract
What are Consumer Driven Contracts ( CDC )
Not a new concept but becoming more prevalent in today’s world of
microservices.
It is a pattern for evolving services and their API’s without breaking
backward or forward compatibility.
CDC driven testing is TDD applied at an architectural level of the project.
What problems are we trying to solve?
1. Provide a framework for consumers to set
expectations with consumer contracts and
drive the producer API.
2. Producer test lifecycle includes validating
consumer contracts.
3. Provide reusable producer stubs for
consumers to use in their integration tests.
4. Minimize E2E integration tests.
Spring Cloud Contract
Spring Cloud Contract provides support for CDC and service schemas in Spring Apps.
Being part of Spring Cloud it can help stub out Spring Cloud components like Eureka, Consul
etc.
Comprised of three main projects :-
Spring Cloud Contract Verifier : Library on server side to test if the server isn’t breaking any
consumer contracts.
Spring Cloud Contract Wiremock : Wrapper around wiremock to help start stubs with
wiremock json stubs.
Spring Cloud Contract Stub Runner : Helper library to pull stubs from maven and run them
What is a contract?
Demo
I want to login Can Reshmi/123 login ?
YES !!!!
OK
Demo
I want to login Can Vinay/321 login ?
No !!!!
NOT_OKAY
Typical CDC Flow
Consumer Phase 1 Producer Phase Consumer Phase 2
Consumer
Offline Work
Producer
Implementing
Feature
Consumer
Online Work
Consumer Flow 1 (Login Client)
Start TDD - writes the test for the feature
Write the missing implementation
Clone the Producer (LoginServerService) repository locally
Define the contract locally in the repo of Producer
Add the Spring Cloud Contract Verifier plugin
Run the integration tests
Producer Flow (Login Server)
Take over the PR
Write the missing implementation that will make the autogenerated tests pass
Merge PR and deploys the JARs with the app and the stubs
Consumer Flow 2 (Login Client)
Switches off the Stub Runner’s offline mode once the producer uploads the stubs
Configure Stub Runner by providing the URL to a repo with stubs
Will have its test broken if the producer makes any breaking changes of the API
Demo : Add additional parameter to login
Login Validate(UN,PWD,AC)
Response
Response
Resources
Docs : https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html
MeetUp Demo : https://github.com/reshmik/spring-cloud-contract-login-demo.git
Consumer Driven Contracts (MarcinGrzejszczak) : http://www.slideshare.net/MarcinGrzejszczak/consumer-driven-contracts-
and-your-microservice-architecture-67641511
More Samples : https://github.com/spring-cloud/spring-cloud-contract/tree/1.0.x/samples
Talk to the SpringCloudContract Team : https://gitter.im/spring-cloud/spring-cloud-contract

Consumer Driven Contracts for microservices

  • 1.
    Consumer Driven Contractsfor Microservices By Reshmi Krishna By Vinay Upadhya @reshmi9k @vinayu
  • 2.
    About Us Reshmi Krishna ●Software Engineer ● Platform Architect, Pivotal Cloud Foundry ● Women In Tech Community Member Twitter : @reshmi9k MeetUp : Cloud-Native-New-York Vinay Upadhya ● Software Engineer ● Platform Architect, Pivotal Cloud Foundry ● Application Architecture Evangelist Twitter : @vinayu
  • 3.
    Agenda What are ConsumerDriven Contracts? What problems are we trying to solve? Spring Cloud Contract for microservices Demo
  • 4.
    What are ConsumerDriven Contracts? Internal Marketing App External Reseller App Product Search Service A P I { catalogid, name, price, manufacturer } { catalogid, name, price } Product Schema { catalogid, name, price, manufacturer, inStock } Producer Contract Expectation Consumer Contract Consumer Driven Contract
  • 5.
    What are ConsumerDriven Contracts ( CDC ) Not a new concept but becoming more prevalent in today’s world of microservices. It is a pattern for evolving services and their API’s without breaking backward or forward compatibility. CDC driven testing is TDD applied at an architectural level of the project.
  • 6.
    What problems arewe trying to solve? 1. Provide a framework for consumers to set expectations with consumer contracts and drive the producer API. 2. Producer test lifecycle includes validating consumer contracts. 3. Provide reusable producer stubs for consumers to use in their integration tests. 4. Minimize E2E integration tests.
  • 7.
    Spring Cloud Contract SpringCloud Contract provides support for CDC and service schemas in Spring Apps. Being part of Spring Cloud it can help stub out Spring Cloud components like Eureka, Consul etc. Comprised of three main projects :- Spring Cloud Contract Verifier : Library on server side to test if the server isn’t breaking any consumer contracts. Spring Cloud Contract Wiremock : Wrapper around wiremock to help start stubs with wiremock json stubs. Spring Cloud Contract Stub Runner : Helper library to pull stubs from maven and run them
  • 8.
    What is acontract?
  • 9.
    Demo I want tologin Can Reshmi/123 login ? YES !!!! OK
  • 10.
    Demo I want tologin Can Vinay/321 login ? No !!!! NOT_OKAY
  • 11.
    Typical CDC Flow ConsumerPhase 1 Producer Phase Consumer Phase 2 Consumer Offline Work Producer Implementing Feature Consumer Online Work
  • 12.
    Consumer Flow 1(Login Client) Start TDD - writes the test for the feature Write the missing implementation Clone the Producer (LoginServerService) repository locally Define the contract locally in the repo of Producer Add the Spring Cloud Contract Verifier plugin Run the integration tests
  • 13.
    Producer Flow (LoginServer) Take over the PR Write the missing implementation that will make the autogenerated tests pass Merge PR and deploys the JARs with the app and the stubs
  • 14.
    Consumer Flow 2(Login Client) Switches off the Stub Runner’s offline mode once the producer uploads the stubs Configure Stub Runner by providing the URL to a repo with stubs Will have its test broken if the producer makes any breaking changes of the API
  • 15.
    Demo : Addadditional parameter to login Login Validate(UN,PWD,AC) Response Response
  • 16.
    Resources Docs : https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html MeetUpDemo : https://github.com/reshmik/spring-cloud-contract-login-demo.git Consumer Driven Contracts (MarcinGrzejszczak) : http://www.slideshare.net/MarcinGrzejszczak/consumer-driven-contracts- and-your-microservice-architecture-67641511 More Samples : https://github.com/spring-cloud/spring-cloud-contract/tree/1.0.x/samples Talk to the SpringCloudContract Team : https://gitter.im/spring-cloud/spring-cloud-contract