spring bootハンズオン ~spring bootで作る マイクロサービスアーキテクチャ!...
DESCRIPTION
Spring Bootハンズオン ~Spring Bootで作る マイクロサービスアーキテクチャ! #jjug_ccc #ccc_r53TRANSCRIPT
[R5-3] Spring Bootハンズオン ~Spring Bootで作る
マイクロサービスアーキテクチャ!
槙 俊明 (@making) JJUG CCC 2014 Fall 2014-11-15
ハッシュタグ
#jjug_ccc #ccc_r53
http://www.kohgakusha.co.jp/books/detail/978-4-7775-1865-4
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
演習のゴール
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
Config Repository (Gitbucket)
演習のゴール
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
Config Repository (Gitbucket)
演習1
演習のゴール
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
Config Repository (Gitbucket)
演習2
演習1
演習のゴール
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
Config Repository (Gitbucket)
演習2
演習1
演習3
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
Spring Bootとは?
フレームワークというよりプラットフォーム
現在1.1.9.RELEASE
簡単に言うと、Spring Frameworkでアプリケーションを簡単に作る
ための仕組み
もうすぐ1.2.0
Spring Bootを使うことで・・
モダンでいけてるJavaアプリケーションを 簡単に構築できる
Spring Bootの特徴•あらかじめオススメの組み合わせが決まる
•依存ライブラリを同梱するだけで自動で設定が決まる
•組み込みサーバーを同梱し、アプリを即実行可能 アプリケーション自体は
Spring MVCやSpring Batchで書く
Spring Bootの特徴•あらかじめオススメの組み合わせが決まる
•依存ライブラリを同梱するだけで自動で設定が決まる
•組み込みサーバーを同梱し、アプリを即実行可能 アプリケーション自体は
Spring MVCやSpring Batchで書く
小さなアプリを迅速に開発&デプロイするのに向いている
(≠ 大きなアプリに向かない)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.5.RELEASE</version></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build><properties> <java.version>1.8</java.version></properties>
この設定を追加するだけ
いろいろな依存関係が追加されている
package com.example;!import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;!@RestController@EnableAutoConfigurationpublic class App {! @RequestMapping("/") String home() { return "Hello World!"; }! public static void main(String[] args) { SpringApplication.run(App.class, args); }}
魔法のアノテーション
mainメソッドでアプリ実行
まずは実行•実行方法は2通り
または
$ mvn spring-boot:run
$ gradle bootRun Gradleの場合
http://localhost:8080 にアクセス
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
マイクロサービスアーキテクチャ
•小さなサービスを組み合わせて1つのアプリケーションを作成するアプローチ
•サービス間はメッセージング(HTTP/AMQP等)で通信
• James Lewisが2012年に発表した
「Java, the Unix way」という発表がマイクロサービスアーキテクチャの始まり。 • http://2012.33degree.org/talk/show/67
マイクロサービスアーキテクチャの出典
マイクロサービスアーキテクチャの出典
• Martin Fowler氏と共著で書いた記事「Microservices」で有名に
• http://martinfowler.com/articles/microservices.html
Java, the Unix way (1/4)
• 1つの機能だけ持つ、多くの小さなアプリケーションで構成
•全て頭に入っている/使い捨てできるくらいに十分小さいサイズ
Small with Single Responsibility
http://www.slideshare.net/SpringCentral/springboot-groovyより
Java, the Unix way (2/4)
•組み込みコンテナ(APサーバー)
•実行可能jar
•パッケージマネージャ(RPM等)でインストール
•Unixのサービススクリプトで実行
Containerless Unix Process
http://www.slideshare.net/SpringCentral/springboot-groovyより
Java, the Unix way (3/4)
•1アプリ1リポジトリ
•共通モジュールは外出し(OSSライブラリのような扱い)
Dedicated VCS roots
http://www.slideshare.net/SpringCentral/springboot-groovyより
Java, the Unix way (4/4)
•アプリ内メトリクス •ヘルスチェック •外部のウォッチドッグプロセス •必要に応じてスケール
Status Aware and Auto-Scaling
http://www.slideshare.net/SpringCentral/springboot-groovyより
Spring Bootは「Java, the Unix way」のほとんどの要素を満
たしている!
http://techlife.cookpad.com/entry/2014/09/08/093000
http://www.publickey1.jp/blog/14/post_246.html
2014年バスワード大賞 有力候補(適当)
IoTと良い争い?(笑)
伝統的なアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
モノリシックアーキテクチャ
伝統的なアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
モノリシックアーキテクチャ
システムが大きくなると、全体を把握するのが大変で、保守が難しい。
伝統的なアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
モノリシックアーキテクチャ
システムが大きくなると、全体を把握するのが大変で、保守が難しい。
技術の変更は難しい(リスキー) =ほぼ全体書き換え
伝統的なアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
モノリシックアーキテクチャ
システムが大きくなると、全体を把握するのが大変で、保守が難しい。
技術の変更は難しい(リスキー) =ほぼ全体書き換え
技術的に発展的成長が難しいアーキテクチャ
マイクロサービスアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
Single Responsibility Principle (SRP) に基づきドメイン単位でサービスを分割す
マイクロサービスアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
Single Responsibility Principle (SRP) に基づきドメイン単位でサービスを分割す
HTT
PやAM
QPで通信
マイクロサービスアーキテクチャ
http://www.infoq.com/articles/microservices-intro より図を拝借
Single Responsibility Principle (SRP) に基づきドメイン単位でサービスを分割す
HTT
PやAM
QPで通信
Amazon Netfilx eBay
などが採用
マイクロサービスアーキテクチャの メリット・デメリット
メリット デメリット自担当のサービスのコードを
理解しやすい システム全体を見なくなる
新しい技術を採用しやすい・失敗してもやり直しやすい
分散システムの整合をとるのが難しい
サービス単位でスケールできる サービス間の通信オーバーヘッドがある
1ーク等
他サービスの障害をふまえた設計が必要
IDE
マイクロサービスアーキテクチャの メリット・デメリット
メリット デメリット自担当のサービスのコードを
理解しやすい システム全体を見なくなる
新しい技術を採用しやすい・失敗してもやり直しやすい
分散システムの整合をとるのが難しい
サービス単位でスケールできる サービス間の通信オーバーヘッドがある
1ーク等
他サービスの障害をふまえた設計が必要
IDE
Service Registration and Discovery Distributed Configuration
Load Balancing Circuit Breaker
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
Netflix社の例• すでにマイクロサービスアーキテクチャを用いてサービスを運用
• マイクロサービスアーキテクチャに必要ないくつかのデザインパターンを実装してOSSに公開 • Service registration and discovery • Routing • Load balancing • Circuit Breaker • …
Netflix OSShttp://netflix.github.io/
Eureka
各サービスの登録・検出を担う。各サービスはそれぞれのサービスのIP/PORTを知らなくて良くなる。 負荷分散やフェールオバー等に用いられる。
https://github.com/Netflix/eureka
AWSのELBはフロントエンドの負荷分散向けであるが、 Eurekaはミドルウェア間の負荷分散に対応
AWSのマルチAZで複製可能
30秒毎のheartbeat
AWSのELBはフロントエンドの負荷分散向けであるが、 Eurekaはミドルウェア間の負荷分散に対応
AWSのマルチAZで複製可能
Hystrix
Circuit Breakerパターンを実現する処理を コマンドパターンで実装するライブラリ
https://github.com/Netflix/Hystrix
Circuit Breakerパターン• 障害が起きているサービスにアクセスし続けることで、他のサービスへの障害へ伝播することを防ぐ。→ 一定期間でエラーやタイムアウトのしきい値を超えたら、その処理を常にエラーにする。
http://springinpractice.com/2010/07/06/annotation-based-circuit-breakers-with-spring
正常
エラーやタイムアウトがしきい値を超えるとリクエストを遮断
する状態に移行する
時間が経過すると、正常に戻っているか確認
Hystrix Dashboard
Hystrix Dashboard
Turbine
Hystrixのイベントストリームを集約する
https://github.com/Netflix/turbine
Ribbon
クライアントサイドでロードバランス(ラウンドロビン)するためのライブラリ。EurekaやHTTP/TCP/
UDPクライアントと連携する。
https://github.com/Netflix/ribbon
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
マイクロサービス界のFizzBuzz
•「URL短縮サービス」をいかに簡単に作成できるか?がフレームワークのベンチマークとなっている?
• bit.lyやgoo.glみたいなやつ。
課題1: TODOを埋めてプログラムを完成させてください
String hash = ""/* TODO (1) URLをハッシュ化。ハッシュアルゴリズムには 32-bit murmur3 algorithm を使用する。 */;// ヒント: com.google.common.hash.Hashing.murmur3_32()
// TODO (2) urlMapにhashに紐づくURLを追加する。
demo.UrlShortenerクラスを編集してください。35行目
37行目
exercise/01-urlshortener/urlshortener
課題2: Redisを使ってConcurrentHashMap使用部分を書き換えましょう
$ cd software/redis-2.8.17$ make$ ./src/redis-server
Redisのビルド&起動
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
Spring Cloud
•分散システムの共通的なパターンを簡単に使えるようにしたプロジェクト
• Spring Cloud Config • Spring Cloud Netflix • Spring Cloud for Amazon Web Services • Spring Cloud Connectors 等
•現在1.0.0.M2バージョン(正式版はまだ)
http://projects.spring.io/spring-cloud/
Spring Cloud Config
• 分散システムにおけるコンフィギュレーションの仕組み(Distributed Configuration Management)を提供するプロジェクト
• ClientとServerで構成される。
• コンフィギュレーションを再読み込みする仕組みも提供する。
Spring Cloud Configの最小構成
@EnableConfigServer@EnableAutoConfiguration foo
Main Application (Client)port:8888port:8080
Config Server
Git
Properties
Config Repository
Spring Cloud Config Client
Spring Cloud Config Server
設定の取得元をGitまたはPropertiesファイル
を選択できる
Config Serverの作り方package demo;!import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.context.annotation.ComponentScan;!@EnableConfigServer @EnableAutoConfiguration@ComponentScanpublic class ConfigServer {! public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); }}
Config Serverの作り方package demo;!import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.context.annotation.ComponentScan;!@EnableConfigServer @EnableAutoConfiguration@ComponentScanpublic class ConfigServer {! public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); }}
アノテーションつけるだけ
Config Serverの作り方
• org.springframework.cloud.config.server.ConfigServerApplicationクラスが用意されているため、実はConfigServerクラスを作成する必要が無い。
Config Repositoryの指定方法
spring: platform: config: server.uri: https://github.com/making/config-repo
Config Repositoryの指定方法
spring: platform: config: server.uri: https://github.com/making/config-repo
Server側のbootstrap.ymlに spring.platform.config.server.uriプロパティ指定
コンフィギュレーション取得方法http://localhost:8888/{app}/{env}/{label}
にアクセスすることでアプリケーション毎の環境(profile)毎のコンフィギュレーションを取得できる
• app=アプリケーション名 • env=profile名 (デフォルトはdefault) • label=branch名 (デフォルトはmaster, 省略可)
Githubでの設定例
https://github.com/making/config-repohttp://localhost:8888/foo/default で取得できる
Githubでの設定例
https://github.com/making/config-repohttp://localhost:8888/foo/default で取得できる
http://localhost:8888/foo/development で取得できる(defaultの値を上書き)
Githubでの設定例
https://github.com/making/config-repohttp://localhost:8888/foo/default で取得できる
http://localhost:8888/foo/development で取得できる(defaultの値を上書き)
演習で試しましょう
Config Serverのセキュリティ
• http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_server
• Spring Securityによる認証/認可やプロパティの暗号化/復号にも対応
Clientの作り方
!<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId></dependency>!
Clientの作り方
!<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId></dependency>!
依存関係いれるだけ
Clientの使い方@RestController@ComponentScan@EnableAutoConfigurationpublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(ClientApp.class, args); }}
Clientの使い方@RestController@ComponentScan@EnableAutoConfigurationpublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(ClientApp.class, args); }}
Springの普通の プロパティ解決の仕組み
アプリケーション名の指定
spring.application.name: foo
アプリケーション名の指定
spring.application.name: foo
Client側のbootstrap.ymlに spring.application.nameプロパティ指定
設定例
設定例@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
設定例@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello 123456!
設定変更@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
設定変更@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello 123456!
設定変更@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello 123456!反映されていない!?
Refreshエンドポイント
$ curl -X POST http://localhost:8080/refresh["bar"]$ curl -X GET http://localhost:8080/env/barSpring Boot
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Refreshエンドポイント
$ curl -X POST http://localhost:8080/refresh["bar"]$ curl -X GET http://localhost:8080/env/barSpring Boot
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello 123456!
Refreshエンドポイント
$ curl -X POST http://localhost:8080/refresh["bar"]$ curl -X GET http://localhost:8080/env/barSpring Boot
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello 123456!DI済みのプロパティには
反映されない!?
Restartエンドポイント
$ curl -X POST http://localhost:8080/restart{"message":"Restarting"}
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
DIコンテナを再起動
Restartエンドポイント
$ curl -X POST http://localhost:8080/restart{"message":"Restarting"}
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello Spring Boot!
DIコンテナを再起動
Restartエンドポイント
$ curl -X POST http://localhost:8080/restart{"message":"Restarting"}
@Value("${bar:World}")String bar;!@RequestMapping("/")String home() { return "Hello " + bar + "!";}
Hello Spring Boot!再DIによりプロパティが
反映された。 (でも、起動に時間がかかる)
DIコンテナを再起動
Adhocな コンフィギュレーション変更
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud"{"bar":"Spring Cloud"}$ curl http://localhost:8080/env/barSpring Cloud
EnvエンドポイントにPOST
Adhocな コンフィギュレーション変更
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud"{"bar":"Spring Cloud"}$ curl http://localhost:8080/env/barSpring Cloud
EnvエンドポイントにPOST
$ curl -X POST http://localhost:8080/refresh[]$ curl -X GET http://localhost:8080Hello Spring Boot!
Adhocな コンフィギュレーション変更
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud"{"bar":"Spring Cloud"}$ curl http://localhost:8080/env/barSpring Cloud
EnvエンドポイントにPOST
$ curl -X POST http://localhost:8080/refresh[]$ curl -X GET http://localhost:8080Hello Spring Boot!
Refreshしてもやはり変わらず
Refreshスコープ@RestController@ComponentScan@EnableAutoConfiguration@RefreshScopepublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(App.class, args); }}
BeanのスコープをSpring Cloud Configで追加されたRefreshスコープ
に変更
Refreshスコープ@RestController@ComponentScan@EnableAutoConfiguration@RefreshScopepublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(App.class, args); }}
BeanのスコープをSpring Cloud Configで追加されたRefreshスコープ
に変更
設定例
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud Config"{"bar":"Spring Cloud Config"}$ curl http://localhost:8080/env/barSpring Cloud Config
設定例
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud Config"{"bar":"Spring Cloud Config"}$ curl http://localhost:8080/env/barSpring Cloud Config
$ curl -X POST http://localhost:8080/refresh[]$ curl -X GET http://localhost:8080Hello Spring Cloud Config!
設定例
$ curl -X POST http://localhost:8080/env -d bar="Spring Cloud Config"{"bar":"Spring Cloud Config"}$ curl http://localhost:8080/env/barSpring Cloud Config
$ curl -X POST http://localhost:8080/refresh[]$ curl -X GET http://localhost:8080Hello Spring Cloud Config!
Refreshでプロパティが 即再DIされた
Spring Cloud Netflix• Netflix OSS群とSpringとの連携モジュール
• Eureka • Hystrix • Turbine • Ribbon • Feign • Zuul • Archaius
@ComponentScan@EnableAutoConfiguration@EnableEurekaServerpublic class EurekaServer {! public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); }}
Eureka (Server)
@ComponentScan@EnableAutoConfiguration@EnableEurekaServerpublic class EurekaServer {! public static void main(String[] args) { SpringApplication.run(EurekaServer.class, args); }}
Eureka (Server)
Eureka (Client)@RestController@ComponentScan@EnableAutoConfiguration@RefreshScope@EnableEurekaClientpublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(ClientApp.class, args); }}
Eureka (Client)@RestController@ComponentScan@EnableAutoConfiguration@RefreshScope@EnableEurekaClientpublic class ClientApp { @Value("${bar:World}") String bar;! @RequestMapping("/") String home() { return "Hello " + bar + "!"; }! public static void main(String[] args) { SpringApplication.run(ClientApp.class, args); }}
Eureka(Server)のダッシュボード
Eureka(Server)のダッシュボード
Eureka Serverに登録しているサービス(Client)
Hystrix@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@EnableHystrixpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
@Componentpublic class StoreIntegration {! @HystrixCommand(fallbackMethod = "defaultStores") public Object getStores(Map<String, Object> parameters) { // do stuff that might fail }! public Object defaultStores(Map<String, Object> parameters) { return /* something useful */; }}
CircuitがOpenな状態に実行するメソッド
Circuit Breakerパターンに対応したコマンドを作成できる
Hystrix@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@EnableHystrixpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
@Componentpublic class StoreIntegration {! @HystrixCommand(fallbackMethod = "defaultStores") public Object getStores(Map<String, Object> parameters) { // do stuff that might fail }! public Object defaultStores(Map<String, Object> parameters) { return /* something useful */; }}
CircuitがOpenな状態に実行するメソッド
Circuit Breakerパターンに対応したコマンドを作成できる
Hystrix Dashboard@ComponentScan@EnableAutoConfiguration@EnableHystrixDashboardpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
Hystrix Dashboard@ComponentScan@EnableAutoConfiguration@EnableHystrixDashboardpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
Hystrix Dashboard
@EnableHystrixをつけたアプリのevent-streamを登録
Hystrix Dashboard
Ribbon@Componentpublic class MyClass { @Autowired LoadBalancerClient loadBalancer;! public Object getStores(Map<String, Object> parameters) { ServiceInstance instance = loadBalancer.choose("stores"); URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort())); // do something with the URI }}
Eurekaに登録したサービス名
@Componentpublic class MyClass { @Autowired RestTemplate restTemplate;! public Object getStores(Map<String, Object> parameters) { String results = restTemplate.getForObject("http://stores", String.class); return results; }}
RestTemplateにRibbonを使用したInterceptorが埋め込まれている
hostname:port指定不要
Spring Cloudを用いたマイクロサービスアーキテクチャのBoilerplate
UI
Service B
Config Server
Service Discovery
Circuit Breaker Monitor
Service A
load balancer
load balancer
Browser
Spring Cloudを用いたマイクロサービスアーキテクチャのBoilerplate
UI
Service B
Config Server
Service Discovery
Circuit Breaker Monitor
Service A
load balancer
load balancer
Browser
Spring Cloud Config Eureka
Ribbon
Hystrix
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
演習で扱うシステムの アーキテクチャ
Config Server (Spring Cloud
Config)
URL Shortener
Config Repository (Gitbucket)
8080
8888
8081
Config Client
コンテンツ• Spring Bootとは? • マイクロサービスアーキテクチャとは? • マイクロサービスアーキテクチャのためのNetflix OSS群 • 演習1 Spring Bootで「URL短縮サービス」を作る
• Spring Cloudとは? • 演習2 Spring Cloud Configで動的コンフィギュレーション • 演習3 Spring Cloud Netflixでマイクロサービスアーキテクチャ構築
演習で扱うシステムの アーキテクチャ
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
Config Repository (Gitbucket)
URL Shortener UIConfig Server (Spring Cloud
Config) Service Discovery
(Eureka)
Circuit Breaker Monitor (Hystrix)
URL Shortener
load balancer(Ribbon)
Browser
URL ShortenerURL Shortener
Redis
9999
Config Repository (Gitbucket)
8080
8888 8761
63797979
80838082
8081
演習で扱うシステムの アーキテクチャ
まとめ
• 後で書く