21
CyberAgent, Inc. All Rights Reserved JSUG 勉勉勉 SpringOne Platform 2016 勉勉勉Consumer Driven Contract and Your Microservices Architecture Yusuke Ikeda / @yukung CyberAgent, Inc.

Consumer Driven Contract and Your Microservices Architecture

Embed Size (px)

Citation preview

Page 1: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

JSUG 勉強会SpringOne Platform 2016 報告会!

Consumer Driven Contract and Your Microservices Architecture

Yusuke Ikeda / @yukungCyberAgent, Inc.

Page 2: Consumer Driven Contract and Your Microservices Architecture

● 池田 裕介(@yukung)

● 株式会社サイバーエージェント 技術本部

● サーバサイドエンジニア

● Ameba プラットフォームの API設計・運用

○ 最近は ElasticSearch v0.19 と戯れています■ ElasticSearch(0.19)で無理やりデータ復旧させた話

Java や Groovy のコミュニティによく出没します

About me

Page 3: Consumer Driven Contract and Your Microservices Architecture

SpringOne のレポート書きました● SpringOne Platform 2016 参加レポート〜開幕前日編〜

● SpringOne Platform 2016 参加レポート〜初日編〜

● SpringOne Platform 2016 2参加レポート〜 日目編〜

Page 4: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

というわけで早速行きます

Page 5: Consumer Driven Contract and Your Microservices Architecture

Consumer Driven Contract

Page 6: Consumer Driven Contract and Your Microservices Architecture

聞いたことある人?

Page 7: Consumer Driven Contract and Your Microservices Architecture

もうやってるよって人?

Page 8: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

簡単に紹介

Page 9: Consumer Driven Contract and Your Microservices Architecture

Consumer Driven Contract とは

● http://martinfowler.com/articles/consumerDrivenContracts.html

Page 10: Consumer Driven Contract and Your Microservices Architecture

Consumer Driven Contract とは

● Consumer (利用側 ) が Producer (提供側 ) に期待する機能

を、 Contract として共有し、 Producer の全 Consumer に対する責務

をまとめたもの

● 責務を全うし続けている (Contract を壊さない ) 限り、 Producer -

Consumer 間の結合が壊れていないことを保証できる

Page 11: Consumer Driven Contract and Your Microservices Architecture

もう少し噛み砕くと

● Producer

○ API が公開しているサービス

● Consumer

○ Producer の API を利用するサービス

● Contract

○ Producer と Consumer 間で合意した API の利用方法

● Consumer Driven Contract

○ Consumer が Producer の API の変更を促す(駆動する)アプ

ローチ

○ つまり、アーキテクチャレベルの TDD

Page 12: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

百聞は一見に如かず

Page 13: Consumer Driven Contract and Your Microservices Architecture

CDC ライブコーディング

● Consumer

○ 🍻をくれるサービス

○ 🍻をあげられるかどうかは、さらに他のサービスに尋ねる

● Producer

○ 🍻を買える年齢かどうかをチェックするサービス

● Feature

○ 🍻

○ 🍻

Page 14: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

DEMO

Page 15: Consumer Driven Contract and Your Microservices Architecture

CyberAgent, Inc. All Rights Reserved

Summary

Page 16: Consumer Driven Contract and Your Microservices Architecture

Summary

● Spring Cloud Contract と Consumer Driven Contract を使って

○ Consumer と Producer 間で合意しながら API を作成

○ 期待は読みやすい契約として定義され、それを Producer に対してテ

スト

○ Producer の Stub を Consumer で で で

○ Stub の設定と開始は完全に自動で行われる

Page 17: Consumer Driven Contract and Your Microservices Architecture

Summary - Consumer flow 1

● Consumer

○ TDD を開始 - 機能のためのテストを書く

○ API を変更するために Producer のコードをローカルに clone

○ clone された Producer のコードの Contract を stub に変換してローカル

に install

○ Consumer のコードの中の Stub Runner を offline mode にする

○ Producer の Stub を利用するように Stub Runner を設定

○ API をテストしながら Red - Green - Refactor を回す

○ Contract を提案する PR を Producer へ送る

Page 18: Consumer Driven Contract and Your Microservices Architecture

● Producer

○ PR を受け入れる

○ 自動生成されたテストをパスするように足りない実装をを書く

○ PR をマージし、 Application と Stub の JAR をデプロイする

Summary - Producer flow

Page 19: Consumer Driven Contract and Your Microservices Architecture

● Consumer

○ Producer のアップロードした Stub を利用するように Stub Runner の

offline mode をオフにする

○ 提供された Stub を利用するように、 Repository の URL を Stub

Runner に設定する

○ でで Producer で API の破壊的な変更を入れた場合はテストが壊れる

Summary - Consumer flow 2

Page 20: Consumer Driven Contract and Your Microservices Architecture

● Contract を Consumer から Producer へ伝える手段

● わかりやすく簡単に使える statically typed DSL

● 定義された Contract からテストを自動生成

● Stub Runnner が便利 - 実行時に Stub を Nexus/Artifactory から自動的に

ダウンロード

● Spring Cloud Integration - Integration Test に Discovery Service は必要

でで

Spring Cloud Contract Verifier を使う理由

Page 21: Consumer Driven Contract and Your Microservices Architecture

● Learn more

○ Spring Cloud Contract (Official Document)

○ Consumer-Driven Contracts Design Patternを読み漁ってみた

○ : (Cookpad tecサービス分割時の複雑性に対処する テスト戦略の話

h blog)

○ Consumer Driven Contractsで REST API/で で で で で で で で で で で (by @making)ト

● Demo source code

○ https://github.com/yukung/springone-cdc-consumer

○ https://github.com/yukung/springone-cdc-producer

Thank you for your attention!