├── prerequisite.md ├── README.md ├── circuit-breaker.md ├── service-registry.md ├── config-server.md ├── cloud-foundry.md ├── spring-cloud-services.md ├── spring-boot.md └── distributed-tracing.md /prerequisite.md: -------------------------------------------------------------------------------- 1 | ## 事前準備 2 | 3 | 1. JDK、git、curl、CF CLI、Pivotal Web Servicesのアカウント作成。[こちら](https://github.com/Pivotal-Japan/cf-workshop/blob/master/prerequisite.md)参照 (Golangのインストールは不要)。 4 | 1. [Spring Tool Suite](https://spring.io/tools)のインストール 5 | 1. [GitHubのアカウント作成](https://github.com/join) 6 | 1. [JSONView](https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc) (Google Chromeを使用している場合) 7 | 8 | **※ 以降、Windowsの場合は[Git Bash](https://git-scm.com/downloads)を使用して作業ください。** 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cloud-native-workshop 2 | 3 | 本ワークショップでは「Metflix」というダミー動画配信サービスライクなシステムをマイクロサービスアーキテクチャで構築しSpring BootとSpring Cloudの使い方を学びます。またこのマイクロサービスをCloud Foundryにデプロイする方法を学びます。 4 | 5 | 今回作成するアプリケーションは以下のような一見普通のWebアプリケーションですが、 6 | 7 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/4c6b6ab4-875c-2017-d6b9-81c2aaed5053.png) 8 | 9 | フロントエンドのUIからMembership Service(会員サービス)とRecommendations Service(リコメンデーションサービス)が呼び出されています。 10 | 11 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/a987c8a0-8d97-1f5b-12e5-c07bebf8fec7.png) 12 | 13 | Membership ServiceとRecommendations ServiceはREST APIであり、以下のような呼び出し関係になっています。 14 | 15 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2288f2a3-08e3-949f-89bd-4ec33e58b963.png) 16 | 17 | このマイクロサービスアーキテクチャを支えるために、 18 | 19 | * Config Service 20 | * Service Registry 21 | * Circuit Breaker 22 | 23 | を使用します。 24 | 25 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c30432b0-2a63-fdd6-71ba-d8071cd55c76.png) 26 | 27 | このシステムをSpring BootとSpring Cloud (+ Netflix OSS)で作成します。 28 | 29 | 個々のサービスをSpring Bootで開発し、Config Service、Service Registry、Circuit BreakerをSpring Cloudで実現でします。 30 | 31 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ffa8c4ce-6470-c116-1eee-1e92c95b3019.png) 32 | 33 | 34 | なお、本サンプルコード作成にあたり、SpringOne2GX 2015で発表された「[Spring Cloud at Netflix](https://github.com/netflix-spring-one)」の内容を利用しています。 35 | 36 | ### Schedule 37 | 38 | * [事前準備](prerequisite.md) 39 | * AM 10:00 - AM 10:50 Introduction 40 | * AM 10:50 - AM 11:00 Break 41 | * AM 11:00 - AM 12:00 [Spring Boot](spring-boot.md) 42 | * AM 12:00 - PM 1:00 Lunch 43 | * PM 1:00 - PM 1:30 [Config Server](config-server.md) 44 | * PM 1:30 - PM 2:00 [Service Registry](service-registry.md) 45 | * PM 2:00 - PM 2:30 [Circuit Breaker](circuit-breaker.md) 46 | * PM 2:30 - PM 2:45 Break 47 | * PM 2:45 - PM 3:45 [Deploy to Cloud Foundry](cloud-foundry.md) 48 | * PM 3:45 - PM 4:00 Break 49 | * PM 4:00 - PM 5:00 [Spring Cloud Services](spring-cloud-services.md) 50 | 51 | Extra Chapter 52 | * [Distributed Tracing](distributed-tracing.md) 53 | 54 | ### TODO 55 | 56 | - [ ] Support Intelligent Routing (Zuul) 57 | - [x] Support Distibuted Tracing (Spring Cloud Sleuth + Zipkin) 58 | - [ ] Support OAuth2 (Spring Cloud Security) 59 | - [ ] Support CI/CD (Concourse CI) 60 | 61 | 62 | ### 利用規約 63 | 64 | 無断で本ドキュメントの一部または全部を改変したり、本ドキュメントを用いた二次的著作物を作成することを禁止します。ただし、ドキュメント修正のためのPull Requestは大歓迎です。 65 | 66 | -------------------------------------------------------------------------------- /circuit-breaker.md: -------------------------------------------------------------------------------- 1 | ## Circuit Breakerの導入 2 | 3 | 次にCircuit Breakerの導入して、障害の伝播を防ぎます。 4 | 5 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/d7b3f0be-64e0-8a03-d83b-09c8102236bb.png) 6 | 7 | 本ページで作成するソースコードは[こちら](https://github.com/making/metflix/tree/04-circuit-breaker)(`04-circuit-breaker`ブランチ)から参照可能です。 8 | 9 | 10 | ### Hystrix Commandの作成 11 | 12 | #### 作業手順 13 | 14 | #### Recommendations 15 | 16 | 1. `recommendations`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/recommendations/pom.xml)に変更 17 | 1. `recommendations`の`src/main/java/com/metflix/RecommendationsApplication.java`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/recommendations/src/main/java/com/metflix/RecommendationsApplication.java)に変更 18 | 19 | 20 | #### UI 21 | 22 | 1. `ui`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/ui/pom.xml)に変更 23 | 1. `ui`の`src/main/java/com/metflix/UiApplication.java`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/ui/src/main/java/com/metflix/UiApplication.java)に変更 24 | 25 | #### 動作確認 26 | 27 | `recommendations`と`ui`を再起動。 28 | 29 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/7a12e579-0f53-139a-6ce4-065ecdc697e4.png) 30 | 31 | [http://localhost:8080](http://localhost:8080)にアクセス 32 | 33 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/8e1eba55-739c-4f24-bbe5-4d8490af424b.png) 34 | 35 | 次に`membership`サービスを停止。 36 | 37 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/0a62766f-fb90-b660-3a9b-3850030a1608.png) 38 | 39 | 再度、[http://localhost:8080](http://localhost:8080)にアクセスし、Recommendationsの結果が変わっていることを確認。 40 | 41 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/0b9c18b3-9c75-6a42-5c13-701f8cb90b57.png) 42 | 43 | `membership`サービスを起動。 44 | 45 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/3d04f3bd-420e-67b5-5824-3bc32311c98f.png) 46 | 47 | しばらくして[http://localhost:8080](http://localhost:8080)にアクセスすると復旧していることを確認 48 | 49 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/dde8de71-12b9-7d14-c30a-ef66c01c9008.png) 50 | 51 | 同様に`recommendations`サービスを停止させると、Recommendationsの結果が空になっていることを確認。 52 | 53 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/fb5ad815-df20-5827-ce80-4168f8f46324.png) 54 | 55 | 動作を確認したら`recommendations`サービスを起動。 56 | 57 | [http://localhost:3333/hystrix.stream](http://localhost:3333/hystrix.stream)にアクセス。メトリクスのイベントストリームが流れてくることを確認。 58 | 59 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/971293f3-c060-ba6e-eb84-ed0aad9692be.png) 60 | 61 | 62 | ### Circuit Breaker Dashboardの作成 63 | 64 | 65 | #### 作業手順 66 | 67 | 1. File -> New -> Spring Starter Project 68 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 69 | 70 | 1. * Name : `hystrix-dashboard` 71 | * Group: `com.metflix` 72 | * Artifact: `hystrix-dashboard` 73 | * Package: `com.metflix` 74 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2311a692-5d1d-02f1-da5d-7566802822b2.png) 75 | 76 | 1. * Cloud Circuit Breaker -> `Hystrix Dashboard` 77 | * Cloud Config -> `Config Client` 78 | * Ops -> `Actuator` 79 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/8c774962-d0cc-3e53-a204-ecc6f3018b57.png) 80 | 81 | 82 | 1. workspaceを確認 83 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e68451c3-ffe0-3f84-d6cd-6166e0d2c1ec.png) 84 | 85 | 1. `src/main/java/com/metflix/HystrixDashboardApplication.java`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/hystrix-dashboard/src/main/java/com/metflix/HystrixDashboardApplication.java)に変更 86 | 1. `src/main/resources/application.properties`を**削除** 87 | 1. `src/main/resources`を右クリック -> New -> File 88 | 1. File name: `bootstrap.properties` 89 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/04-circuit-breaker/hystrix-dashboard/src/main/resources/bootstrap.properties)に変更 90 | 91 | #### 動作確認 92 | 93 | Package Explorerの`HystrixDashboardApplication.java`を右クリック -> Run As -> Spring Boot App 94 | 95 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e324f409-ee24-7d5f-710f-4bcb114a69b9.png) 96 | 97 | 98 | コンソールを確認 99 | 100 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/617b4f3f-2806-2445-3aeb-0a66e7cb95ee.png) 101 | 102 | 103 | [http://localhost:7979/hystrix](http://localhost:7979/hystrix)にアクセス 104 | 105 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2e2b16b3-273a-2b1d-fbaa-359a878b582c.png) 106 | 107 | `http://localhost:3333/hystrix.stream`を入力してMonitor Streamをクリック。 108 | 109 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/bbf99ff3-76a9-e9d4-b96b-0c2395619014.png) 110 | 111 | `http://localhost:4444/hystrix.stream`も同様 112 | 113 | > **Tips** 114 | > 115 | > 複数のストリームを集約するには別途[Turbine](http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_turbine)の設定が必要です。 116 | > また、PaaSのようにスケーアウトした各インスタンスへのURLが同一な場合は`/hystrix.stream`にアクセスしてメトリクスをpullするのではなく、AMQPを使ってpushする必要があります。この場合は、別途[Turbine AMQP](http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_turbine_amqp)の設定が必要です。 117 | -------------------------------------------------------------------------------- /service-registry.md: -------------------------------------------------------------------------------- 1 | ## Service Registryの導入 2 | 3 | これまで各サービスのREST APIにアクセスするために物理的なURL(ホスト名 + ポート番号)を直接記述していましたが、サービスをService Registryに登録させることにより、論理的なURL(サービスID)でサービスにアクセスできるようにします。 4 | 5 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/22156b30-224a-a93d-d77a-92c773803314.png) 6 | 7 | 8 | 本ページで作成するソースコードは[こちら](https://github.com/making/metflix/tree/03-service-registry)(`03-service-registry`ブランチ)から参照可能です。 9 | 10 | 11 | ### Eureka Serverの作成 12 | 13 | #### 作業手順 14 | 15 | 1. File -> New -> Spring Starter Project 16 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 17 | 18 | 1. * Name : `eureka-server` 19 | * Group: `com.metflix` 20 | * Artifact: `eureka-server` 21 | * Package: `com.metflix` 22 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e0591ec2-0c42-5dd2-2277-6964a2a08d22.png) 23 | 24 | 1. * Cloud Config -> `Config Client` 25 | * Cloud Discovery -> `Eureka Server` 26 | * Ops -> `Actuator` 27 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ce2f3274-a5ac-520b-7334-f683fec95372.png) 28 | 29 | 1. workspaceを確認 30 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/bd7454a2-f36b-b9a1-cbc5-0155fea1aab7.png) 31 | 32 | 1. `src/main/java/com/metflix/EurekaServerApplication.java`を[この内容](https://github.com/making/metflix/blob/03-service-registry/eureka-server/src/main/java/com/metflix/EurekaServerApplication.java)に変更 33 | 1. `src/main/resources/application.properties`を**削除** 34 | 1. `src/main/resources`を右クリック -> New -> File 35 | 1. File name: `bootstrap.properties` 36 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/03-service-registry/eureka-server/src/main/resources/bootstrap.properties)に変更 37 | 38 | #### 動作確認 39 | 40 | Package Explorerの`EurekaServerApplication.java`を右クリック -> Run As -> Spring Boot App 41 | 42 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2d8b20d2-53e1-6246-46e5-7f09501db079.png) 43 | 44 | 45 | コンソールを確認 46 | 47 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5d34f076-1bb4-661e-4925-7400bb3a8a73.png) 48 | 49 | [http://localhost:8761](http://localhost:8761)にアクセス 50 | 51 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5416f09e-62e5-05be-e994-a44da61147c9.png) 52 | 53 | 54 | 55 | ### Eureka Clientの設定 56 | 57 | #### 作業手順 58 | 59 | 60 | 61 | ##### Membership 62 | 63 | 1. `membership`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/03-service-registry/membership/pom.xml)に変更 64 | 1. `membership`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/03-service-registry/membership/src/main/resources/bootstrap.properties)に変更 65 | 1. `membership`の`src/main/java/com/metflix/MembershipApplication.java`を[この内容](https://github.com/making/metflix/blob/03-service-registry/membership/src/main/java/com/metflix/MembershipApplication.java)に変更 66 | 67 | ##### Recommendations 68 | 69 | 1. `recommendations`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/03-service-registry/recommendations/pom.xml)に変更 70 | 1. `recommendations`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/03-service-registry/recommendations/src/main/resources/bootstrap.properties)に変更 71 | 1. `recommendations`の`src/main/java/com/metflix/RecommendationsApplication.java`を[この内容](https://github.com/making/metflix/blob/03-service-registry/recommendations/src/main/java/com/metflix/RecommendationsApplication.java)に変更 72 | 73 | 74 | ##### UI 75 | 76 | 1. `ui`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/03-service-registry/ui/pom.xml)に変更 77 | 1. `ui`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/03-service-registry/ui/src/main/resources/bootstrap.properties)に変更 78 | 1. `ui`の`src/main/java/com/metflix/UiApplication.java`を[この内容](https://github.com/making/metflix/blob/03-service-registry/ui/src/main/java/com/metflix/UiApplication.java)に変更 79 | 80 | #### 動作確認 81 | 82 | `membership`、`recommendations`、`ui`を再起動 83 | 84 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/fca24355-f7ee-13c3-ec02-810fcb9df433.png) 85 | 86 | [http://localhost:8761](http://localhost:8761)にアクセス 87 | 88 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/8b3bd476-bae8-4b30-cee1-7b4be9cd7d37.png) 89 | 90 | [http://localhost:8080](http://localhost:8080)にアクセス 91 | 92 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1df21287-d81a-b3d5-f9c1-41a049fd0320.png) 93 | 94 | 95 | Config Serverに設定した[各サービスのURL](https://github.com/making/metflix-config/blob/service-registry/application.properties)が物理名(ホスト名+ポート番号)ではなく論理名になっていることに着目してください。 96 | 97 | ### Membershipサービスのスケールアウト 98 | 99 | Membershipサービスを3台にスケールアウトさせましょう。 100 | 101 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1f40a35a-d1c4-78a6-a338-d0758931c892.png) 102 | 103 | ``` console 104 | $ cd $WORKSHOP/membership 105 | $ ./mvnw clean package 106 | $ PORT=4445 java -jar target/membership-0.0.1-SNAPSHOT.jar 107 | ``` 108 | 109 | 別ターミナルでも 110 | 111 | ``` console 112 | $ cd $WORKSHOP/membership 113 | $ PORT=4446 java -jar target/membership-0.0.1-SNAPSHOT.jar 114 | ``` 115 | 116 | [http://localhost:8761](http://localhost:8761)にアクセス 117 | 118 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c0b41df4-c6bc-5b7e-e16b-74e47f698fab.png) 119 | 120 | 121 | [http://localhost:8080](http://localhost:8080)に何度かアクセスして、各Membershipサービスのログを見てそれぞれにアクセスがあることを確認してください。 122 | -------------------------------------------------------------------------------- /config-server.md: -------------------------------------------------------------------------------- 1 | ## Config Serverの導入 2 | 3 | 次にConfig Serverを導入し、コンフィギュレーションの一元管理を 4 | 5 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/9c2b2738-134d-7274-63ad-49d729cc633c.png) 6 | 7 | 8 | 9 | 本ページで作成するソースコードは[こちら](https://github.com/making/metflix/tree/02-config-server)(`02-config-server`ブランチ)から参照可能です。 10 | 11 | ### Config Serverの作成 12 | 13 | #### 作業手順 14 | 15 | 1. File -> New -> Spring Starter Project 16 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 17 | 18 | 1. * Name : `config-server` 19 | * Group: `com.metflix` 20 | * Artifact: `config-server` 21 | * Package: `com.metflix` 22 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/93c85cef-734f-704a-2118-42fc6c23ece4.png) 23 | 24 | 1. * Cloud Config -> `Config Server` 25 | * Ops -> `Actuator` 26 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/114e0715-9955-156c-9214-179aa782ea3d.png) 27 | 28 | 1. workspaceを確認 29 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1d41a637-4c37-7049-c318-a830b13c8567.png) 30 | 31 | 1. `src/main/java/com/metflix/ConfigServerApplication.java`を[この内容](https://github.com/making/metflix/blob/02-config-server/config-server/src/main/java/com/metflix/ConfigServerApplication.java)に変更 32 | 33 | 1. `src/main/resources/application.properties`を[この内容](https://github.com/making/metflix/blob/02-config-server/config-server/src/main/resources/application.properties)に変更 34 | 35 | 1. GitHubの[metflix-config](https://github.com/making/metflix-config)プロジェクトを自分のアカウントでFork 36 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/0a9be48e-95d2-2052-48e7-784457edfe76.png) 37 | 38 | 1. `application.properties`の`spring.cloud.config.server.git.uri`の値を`https://github.com/making/metflix-config.git`から自分のレポジトリに変更 39 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/979db871-3c62-3bd7-ca36-d4cf754994e3.png) 40 | 41 | #### 動作確認 42 | 43 | Package Explorerの`ConfigServerApplication.java`を右クリック -> Run As -> Spring Boot App 44 | 45 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/44108da3-ed21-2ff5-158b-d5072e498973.png) 46 | 47 | コンソールを確認 48 | 49 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/40f2e380-e60f-8149-243d-b8edac273188.png) 50 | 51 | [http://localhost:8888/env](http://localhost:8888/env)にアクセス 52 | 53 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1ea61cf5-dd6f-9216-cbbc-7fb020ce1a3f.png) 54 | 55 | [http://localhost:8888/membership/default](http://localhost:8888/membership/default)にアクセス 56 | 57 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/9be1eb77-11b0-ca5f-430c-f85948c7dea3.png) 58 | 59 | [http://localhost:8888/recommendations/default](http://localhost:8888/recommendations/default)にアクセス 60 | 61 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/6738d31c-c6a5-2364-2e44-dba1b41f29e9.png) 62 | 63 | [http://localhost:8888/ui/default](http://localhost:8888/ui/default)にアクセス 64 | 65 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/684a2bc4-2ef0-99ab-e5b6-e06528c3830b.png) 66 | 67 | 68 | ### Config Clientの設定 69 | 70 | #### 作業手順 71 | 72 | ##### Membership 73 | 74 | 1. `membership`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/02-config-server/membership/pom.xml)に変更 75 | 1. `membership`の`src/main/resources/application.properties`を**削除** 76 | 1. `membership`の`src/main/resource`を右クリック -> New -> File 77 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1ff28795-1f72-6cc8-784c-a76b3a2d57ed.png) 78 | 1. File name: `bootstrap.properties` 79 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/10a175ec-6d42-4874-01a3-ab378f37f726.png) 80 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/02-config-server/membership/src/main/resources/bootstrap.properties)に変更 81 | 82 | ##### Recommendations 83 | 84 | 1. `recommendations`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/02-config-server/recommendations/pom.xml)に変更 85 | 1. `recommendations`の`src/main/resources/application.properties`を**削除** 86 | 1. `recommendations`の`src/main/resource`を右クリック -> New -> File 87 | 1. File name: `bootstrap.properties` 88 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/02-config-server/recommendations/src/main/resources/bootstrap.properties)に変更 89 | 90 | ##### UI 91 | 92 | 1. `ui`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/02-config-server/ui/pom.xml)に変更 93 | 1. `ui`の`src/main/resources/application.properties`を**削除** 94 | 1. `ui`の`src/main/resource`を右クリック -> New -> File 95 | 1. File name: `bootstrap.properties` 96 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/02-config-server/ui/src/main/resources/bootstrap.properties)に変更 97 | 1. `src/main/java/com/metflix/UiApplication.java`を[この内容](https://github.com/making/metflix/blob/02-config-server/ui/src/main/java/com/metflix/UiApplication.java)に変更 98 | 1. `src/main/resources/templates/index.html`を[この内容](https://github.com/making/metflix/blob/02-config-server/ui/src/main/resources/templates/index.html)に変更 99 | 100 | #### 動作確認 101 | 102 | `membership`、`recommendations`、`ui`を再起動 103 | 104 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/cc78ba6a-da3e-f9da-f977-06f2f61f9d80.png) 105 | 106 | [http://localhost:8080](http://localhost:8080)にアクセス。 107 | 108 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/4dcd9005-b622-4b08-a824-83f3597611c7.png) 109 | 110 | 111 | 112 | Forkした`metflix-config`プロジェクトの`ui.properties`を編集 113 | 114 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/d059d414-9ac3-3060-f98b-6556da84851e.png) 115 | 116 | 以下の内容を追加 117 | 118 | ``` properties 119 | message=It's A Beautiful Day 120 | ``` 121 | 122 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5e644723-92ff-7cee-311c-eb9e5ba04418.png) 123 | 124 | 以下のコマンドを実行して、GitHub上のプロパティ変更をロード 125 | 126 | ``` console 127 | $ curl -XPOST localhost:8080/refresh 128 | ["message"] 129 | ``` 130 | 131 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/13037851-2869-4a3c-6f94-b4c5208a4d22.png) 132 | 133 | 134 | [http://localhost:8080](http://localhost:8080)を再読み込みして変更が反映されていることを確認(**アプリケーションの再起動は不要**)。 135 | 136 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5b5b0f9e-d431-464b-23ed-4ad53e60d7ad.png) 137 | 138 | 以下のコマンドで一時的なプロパティ変更も可能 139 | 140 | ``` console 141 | $ curl -XPOST localhost:8080/env -d message="Message is updated" 142 | {"message":"Message is updated"} 143 | $ curl -XPOST localhost:8080/refresh 144 | [] 145 | ``` 146 | 147 | [http://localhost:8080](http://localhost:8080)を再読み込みして変更が反映されていることを確認。 148 | 149 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/02400789-5410-7d86-000e-04b248b9ea29.png) 150 | -------------------------------------------------------------------------------- /cloud-foundry.md: -------------------------------------------------------------------------------- 1 | ## Cloud Foundryにデプロイ 2 | 3 | ここまで作成してきたサービスをCloud Foundryにデプロイします。 4 | 5 | まずは全てのサービスを停止してください。 6 | 7 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/281de1b2-85c5-bd8a-a611-ebe1a488e409.png) 8 | 9 | 今回は[Pivotal Web Services](https://run.pivotal.io/)にデプロイしますが、無償枠ではアプリケーション合計でメモリを2GBまでしか使用できないため、各アプリケーションに以下のメモリを配分します。併せて、アプリケーションのURLも記載します。 10 | 11 | **以降、`tmaki`の部分を自分のアカウント名またはイニシャルに置き換えてください。** 12 | 13 | | Application | Memory (MB) | URL | 14 | |-----------|------------|------| 15 | | `config-server` | 340 | http://config-server-tmaki.cfapps.io | 16 | | `eureka-server` | 358 | http://eureka-server-tmaki.cfapps.io | 17 | | `membership` | 450 | http://member-tmaki.cfapps.io | 18 | | `recommendations` | 450 | http://recommendations-tmaki.cfapps.io | 19 | | `ui` | 450 | http://ui-tmaki.cfapps.io | 20 | 21 | `hystrix-dashboard`はメモリ不足のためデプロイしません。 22 | またデプロイするアプリケーションに割くメモリ量が小さいため、動作が不安定です。 23 | 24 | 本ページで作成するソースコードとスクリプトは[こちら](https://github.com/making/metflix/tree/05_cloud-foundry)(`05_cloud-foundry`ブランチ)から参照可能です。 25 | 26 | #### Pivotal Web Servicesにログイン 27 | 28 | ``` console 29 | $ cf login -a api.run.pivotal.io 30 | ``` 31 | 32 | #### User Provided Serviceの作成 33 | 34 | 35 | まずはデプロイするConfig ServerとEureka Serverをバックエンドサービスとして、Membershipサービス、Recommendationsサービス、UIサービスにアタッチできるように、User Provided Serviceを作成します。 36 | 37 | ``` console 38 | $ cf create-user-provided-service config-server -p '{"uri":"http://config-server-tmaki.cfapps.io"}' 39 | $ cf create-user-provided-service eureka-server -p '{"uri":"http://eureka-server-tmaki.cfapps.io"}' 40 | ``` 41 | 42 | > **Tips** 43 | > 44 | > [スクリプトによる作業自動化](#スクリプトによる作業自動化)の準備を行えば、 45 | > 46 | > ``` console 47 | > $ cd $WORKSHOP 48 | > $ ./scripts/deploy.sh 49 | > ``` 50 | > 51 | > を実行することで、アプリケーションのデプロイと同時にUser Provided Serviceの作成もできるため、ここではUser Provided Serviceの作成をスキップしても構いません。 52 | 53 | #### `bootstrap.properties`の変更 54 | 55 | Config Serverとしてバックエンドサービスを使えるように`bootstrap.properties`を変更します。 56 | 57 | 1. `eureka-sever`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/05_cloud-foundry/eureka-server/src/main/resources/bootstrap.properties)に変更 58 | 1. `membership`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/05_cloud-foundry/membership/src/main/resources/bootstrap.properties)に変更 59 | 1. `recommendations`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/05_cloud-foundry/recommendations/src/main/resources/bootstrap.properties)に変更 60 | 1. `ui`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/05_cloud-foundry/ui/src/main/resources/bootstrap.properties)に変更 61 | 62 | ### アプリケーションのビルド 63 | 64 | `bootstrap.properties`を変更したら全てのプロジェクトをビルドします 65 | 66 | ``` console 67 | $ cd $WORKSHOP/config-server 68 | $ ./mvnw clean package -Dmaven.test.skip=true 69 | ``` 70 | 71 | ``` console 72 | $ cd $WORKSHOP/eureka-server 73 | $ ./mvnw clean package -Dmaven.test.skip=true 74 | ``` 75 | 76 | ``` console 77 | $ cd $WORKSHOP/membership 78 | $ ./mvnw clean package -Dmaven.test.skip=true 79 | ``` 80 | 81 | ``` console 82 | $ cd $WORKSHOP/recommendations 83 | $ ./mvnw clean package -Dmaven.test.skip=true 84 | ``` 85 | 86 | ``` console 87 | $ cd $WORKSHOP/ui 88 | $ ./mvnw clean package -Dmaven.test.skip=true 89 | ``` 90 | 91 | > **Tips** 92 | > 93 | > [スクリプトによる作業自動化](#スクリプトによる作業自動化)の準備を行えば、 94 | > 95 | > ``` console 96 | > $ cd $WORKSHOP 97 | > $ ./scripts/build.sh 98 | > ``` 99 | > 100 | > を実行することで、全アプリケーションのビルドが行われます。 101 | 102 | 103 | ### Manifestファイルのダウンロード 104 | 105 | Cloud Foundryに各アプリケーションをデプロイするためのManifestマニフェストファイルを用意します。 106 | 107 | [`config-server`の`manifest.yml`](https://github.com/making/metflix/blob/05_cloud-foundry/config-server/manifest.yml) 108 | 109 | ``` console 110 | $ cd $WORKSHOP/config-server 111 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/config-server/manifest.yml 112 | ``` 113 | 114 | [`eureka-server`の`manifest.yml`](https://github.com/making/metflix/blob/05_cloud-foundry/eureka-server/manifest.yml) 115 | 116 | ``` console 117 | $ cd $WORKSHOP/eureka-server 118 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/eureka-server/manifest.yml 119 | ``` 120 | 121 | [`membership`の`manifest.yml`](https://github.com/making/metflix/blob/05_cloud-foundry/membership/manifest.yml) 122 | 123 | ``` console 124 | $ cd $WORKSHOP/membership 125 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/membership/manifest.yml 126 | ``` 127 | 128 | [`recommendations`の`manifest.yml`](https://github.com/making/metflix/blob/05_cloud-foundry/recommendations/manifest.yml) 129 | 130 | ``` console 131 | $ cd $WORKSHOP/recommendations 132 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/recommendations/manifest.yml 133 | ``` 134 | 135 | [`ui`の`manifest.yml`](https://github.com/making/metflix/blob/05_cloud-foundry/ui/manifest.yml) 136 | 137 | ``` console 138 | $ cd $WORKSHOP/ui 139 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/ui/manifest.yml 140 | ``` 141 | 142 | ### Cloud Foundryへアプリケーションをデプロイ 143 | 144 | Manifestマニフェストファイルを使ってCloud Foundryにjarをデプロイします。 145 | 146 | ``` console 147 | $ cd $WORKSHOP/config-server 148 | $ cf push config-server-tmaki 149 | ``` 150 | 151 | ``` console 152 | $ cd $WORKSHOP/eureka-server 153 | $ cf push eureka-server-tmaki 154 | ``` 155 | 156 | ``` console 157 | $ cd $WORKSHOP/membership 158 | $ cf push membership-tmaki 159 | ``` 160 | 161 | ``` console 162 | $ cd $WORKSHOP/recommendations 163 | $ cf push recommendations-tmaki 164 | ``` 165 | 166 | ``` console 167 | $ cd $WORKSHOP/ui 168 | $ cf push ui-tmaki 169 | ``` 170 | 171 | 172 | 173 | > **Tips** 174 | > 175 | > [スクリプトによる作業自動化](#スクリプトによる作業自動化)の準備を行えば、 176 | > 177 | > ``` console 178 | > $ cd $WORKSHOP 179 | > $ ./scripts/deploy.sh 180 | > ``` 181 | > 182 | > を実行することで、User Provided Service及び、アプリケーションのデプロイが行われます。 183 | 184 | 185 | ### 動作確認 186 | 187 | `cf apps`、`cf services`の結果が以下のようになっていることを確認。 188 | 189 | ``` console 190 | $ cf apps 191 | 192 | name requested state instances memory disk urls 193 | config-server-tmaki started 1/1 340M 1G config-server-tmaki.cfapps.io 194 | eureka-server-tmaki started 1/1 358M 1G eureka-server-tmaki.cfapps.io 195 | membership-tmaki started 1/1 450M 1G membership-tmaki.cfapps.io 196 | recommendations-tmaki started 1/1 450M 1G recommendations-tmaki.cfapps.io 197 | ui-tmaki started 1/1 450M 1G ui-tmaki.cfapps.io 198 | ``` 199 | 200 | ``` console 201 | $ cf services 202 | 203 | name service plan bound apps last operation 204 | config-server user-provided eureka-server-tmaki, membership-tmaki, recommendations-tmaki, ui-tmaki 205 | eureka-server user-provided membership-tmaki, recommendations-tmaki, ui-tmaki 206 | ``` 207 | 208 | [http://ui-tmaki.cfapps.io](http://ui-tmaki.cfapps.io)にアクセス (`tmaki`を自分のアカウントまたはイニシャルに変更してください)。 209 | 210 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/47c85439-418e-6fea-6e01-4816243a1241.png) 211 | 212 | 213 | ### スクリプトによる作業自動化 214 | 215 | * アプリケーションのビルド 216 | * User Provided Serviceの作成とアプリケーションのデプロイ 217 | 218 | は[こちら](https://github.com/making/metflix/tree/05_cloud-foundry/scripts)のスクリプトを使えば自動で行えます。 219 | 220 | ``` console 221 | $ cd $WORKSHOP 222 | $ mkdir scripts 223 | $ cd scripts 224 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/scripts/common.sh 225 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/scripts/build.sh 226 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/scripts/deploy.sh 227 | $ curl -OL https://github.com/making/metflix/raw/05_cloud-foundry/scripts/cleanup.sh 228 | $ chmod +x *.sh 229 | $ cd .. 230 | ``` 231 | 232 | `common.sh`に定義されている変数`suffix`を`tmaki`から自分のアカウントまたはイニシャルに変更してください。 233 | 234 | 235 | 以下のコマンドでデプロイしたアプリケーションの削除とUser Provided Serviceの削除を行うことができます。 236 | 237 | ``` console 238 | $ cd $WORKSHOP 239 | $ ./scripts/cleanup.sh 240 | ``` 241 | -------------------------------------------------------------------------------- /spring-cloud-services.md: -------------------------------------------------------------------------------- 1 | ## Spring Cloud Servicesの利用 2 | 3 | [Pivotal Cloud Foundry](https://pivotal.io/platform)の[Spring Cloud Services](https://pivotal.io/app-framework#coordinateanything)を利用することで、 4 | 5 | * Config Server 6 | * Service Registry 7 | * Circuit Breaker Dashboard 8 | 9 | はプラットフォームが提供するため、自前で管理不要になります。 10 | かつこれらのサービスへのアクセスをOAuth 2によって自動で認可制御されます。 11 | 12 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5621efaf-63e5-235a-c25b-c04f91f9e22a.png) 13 | 14 | `config-server`と`eureka-server`は不要なので削除して構いません。 15 | 16 | 本ページで作成するソースコードとスクリプトは[こちら](https://github.com/making/metflix/tree/06_spring-cloud-services)(`06_spring-cloud-services`ブランチ)から参照可能です。 17 | また、Spring Cloud Servicesのマニュアルは[こちら](http://docs.pivotal.io/spring-cloud-services/index.html)です。 18 | 19 | ### アプリケーション一部修正 20 | 21 | Spring Cloud Servicesへのアクセスを自動設定するための[Spring Cloud Services Starters](https://github.com/pivotal-cf/spring-cloud-services-starters)を使うように設定します。 22 | 23 | 本稿作成時点で、Spring Cloud Servicesに対応しているSpring Bootのバージョンが1.2、Spring CloudのバージョンがAngelであるため、アプリケーションを一部変更します。 24 | 25 | また、[Spring Cloud Services用のコンフィギュレーション](https://github.com/making/metflix-config/tree/spring-cloud-services)を使用するように`bootstrap.properties`を変更します([差分](https://github.com/making/metflix-config/compare/service-registry...spring-cloud-services))。 26 | 27 | `manifest.yml`もPivotal Cloud Foundry用に変更します (buildpack, memory増加, Hystrix Dashboardサービスのバインド)。 28 | 29 | #### Members 30 | 31 | 1. `membership`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/membership/pom.xml)に変更 32 | 1. `membership`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/membership/src/main/resources/bootstrap.properties)に変更 33 | 1. `membership`の`manifest.yml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/membership/manifest.yml)に変更 34 | 35 | #### Recommendations 36 | 37 | 1. `recommendations`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/recommendations/pom.xml)に変更 38 | 1. `recommendations`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/recommendations/src/main/resources/bootstrap.properties)に変更 39 | 1. `recommendations`の`src/main/java/com/metflix/RecommendationsApplication.java`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/recommendations/src/main/java/com/metflix/RecommendationsApplication.java)に変更 40 | 1. `recommendations`の`manifest.yml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/recommendations/manifest.yml)に変更 41 | 42 | 43 | #### UI 44 | 45 | 1. `ui`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/ui/pom.xml)に変更 46 | 1. `ui`の`src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/ui/src/main/resources/bootstrap.properties)に変更 47 | 1. `ui`の`src/main/java/com/metflix/UiApplication.java`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/ui/src/main/java/com/metflix/UiApplication.java)に変更 48 | 1. `ui`の`manifest.yml`を[この内容](https://github.com/making/metflix/blob/06_spring-cloud-services/ui/manifest.yml)に変更 49 | 50 | 51 | ### アプリケーションのビルド 52 | 53 | `membership`、`recommendations`、`ui`プロジェクトをビルドします 54 | 55 | 56 | ``` console 57 | $ cd $WORKSHOP/membership 58 | $ ./mvnw clean package -Dmaven.test.skip=true 59 | ``` 60 | 61 | ``` console 62 | $ cd $WORKSHOP/recommendations 63 | $ ./mvnw clean package -Dmaven.test.skip=true 64 | ``` 65 | 66 | ``` console 67 | $ cd $WORKSHOP/ui 68 | $ ./mvnw clean package -Dmaven.test.skip=true 69 | ``` 70 | 71 | > **Tips** 72 | > 73 | > [スクリプトによる作業自動化](#スクリプトによる作業自動化)の準備を行えば、 74 | > 75 | > ``` console 76 | > $ cd $WORKSHOP 77 | > $ ./scripts/build.sh 78 | > ``` 79 | > 80 | > を実行することで、全アプリケーションのビルドが行われます。 81 | 82 | ### Pivotal Cloud Foundryへデプロイ 83 | 84 | #### ログイン 85 | 86 | ※ アカウントの作成は講師が行います。 87 | 88 | ``` console 89 | $ cf login -a api.run.pez.pivotal.io 90 | ``` 91 | 92 | 93 | #### Service (Spring Cloud Services)の作成 94 | 95 | ##### Config Serverサービスの作成 96 | 97 | GitのURLを`https://github.com/making/metflix-config`から「[Config Serverの導入](config-server.md)」でForkしたURLに変更してください。 98 | 99 | ``` console 100 | $ cf create-service p-config-server standard config-server -c '{"git":{"uri":"https://github.com/making/metflix-config", "label":"spring-cloud-services"}}' 101 | ``` 102 | 103 | ##### Service Registryサービスの作成 104 | 105 | ``` console 106 | $ cf create-service p-service-registry standard eureka-server 107 | ``` 108 | 109 | ##### Hystrix Dashboardサービスの作成 110 | 111 | ``` console 112 | $ cf create-service p-circuit-breaker-dashboard standard hystrix-dashboard 113 | ``` 114 | 115 | ### Pivotal Cloud Foundryへアプリケーションをデプロイ 116 | 117 | Manifestマニフェストファイルを使ってPivotal Cloud Foundryにjarをデプロイします。 118 | 119 | ``` console 120 | $ cd $WORKSHOP/membership 121 | $ cf push membership-tmaki 122 | ``` 123 | 124 | ``` console 125 | $ cd $WORKSHOP/recommendations 126 | $ cf push recommendations-tmaki 127 | ``` 128 | 129 | ``` console 130 | $ cd $WORKSHOP/ui 131 | $ cf push ui-tmaki 132 | ``` 133 | 134 | ※ Spring Cloud Servicesの各サービスは初期化に時間がかかり、初期化完了前にアプリケーションにバインドしようとするとエラー(`Server error, status code: 502, error code: 10001, message: Service broker error: Service instance is not running and available for binding.`)が発生します。しばらく待ってから再度`cf push`してください。 135 | 136 | > **Tips** 137 | > 138 | > [スクリプトによる作業自動化](#スクリプトによる作業自動化)の準備を行えば、 139 | > 140 | > ``` console 141 | > $ cd $WORKSHOP 142 | > $ ./scripts/deploy.sh 143 | > ``` 144 | > 145 | > を実行することで、Serviceの作成及び、アプリケーションのデプロイが行われます。 146 | 147 | ### 動作確認 148 | 149 | `cf apps`、`cf services`の結果が以下のようになっていることを確認。 150 | 151 | ``` console 152 | $ cf apps 153 | 154 | name requested state instances memory disk urls 155 | recommendations-tmaki started 1/1 768M 1G recommendations-tmaki.cfapps.pez.pivotal.io 156 | ui-tmaki started 1/1 768M 1G ui-tmaki.cfapps.pez.pivotal.io 157 | membership-tmaki started 1/1 768M 1G membership-tmaki.cfapps.pez.pivotal.io 158 | ``` 159 | 160 | ``` console 161 | $ cf services 162 | 163 | name service plan bound apps last operation 164 | eureka-server p-service-registry standard recommendations-tmaki, ui-tmaki, membership-tmaki create succeeded 165 | config-server p-config-server standard recommendations-tmaki, ui-tmaki, membership-tmaki create succeeded 166 | hystrix-dashboard p-circuit-breaker-dashboard standard recommendations-tmaki, ui-tmaki create succeeded 167 | ``` 168 | 169 | [http://ui-tmaki.cfapps.pez.pivotal.io](http://ui-tmaki.cfapps.pez.pivotal.io)にアクセス (`tmaki`を自分のアカウントまたはイニシャルに変更してください)。 170 | 171 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/de0265b0-e3c9-69e5-5c49-70e73841b9fc.png) 172 | 173 | 174 | 以下のコマンドで、システムに負荷をかけてください。 175 | 176 | ``` console 177 | $ while true;do curl -u making:metflix http://ui-tmaki.cfapps.pez.pivotal.io > /dev/null; done 178 | ``` 179 | 180 | 181 | [Apps Manager](https://apps.run.pez.pivotal.io)にログイン。 182 | 183 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/105dd0ec-260a-5bc4-9958-45e85f9e942c.png) 184 | 185 | 自分のSpaceを確認 186 | 187 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/8c83bec0-53da-f894-3421-49e00fcda247.png) 188 | 189 | `config-server`のManageリンクをクリック 190 | 191 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/d2157386-88fe-83c0-cfc7-3b3a569f12d7.png) 192 | 193 | `eureka-server`のManageリンクをクリック 194 | 195 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/97b1cfda-8dd5-2ce6-455e-978d18082502.png) 196 | 197 | 198 | `hystrix-dashboard`のManageリンクをクリック。Turbineが有効になっており、Hystrixのストリームが集約されていることを確認 199 | 200 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e8caed31-f951-478a-296a-d8f2bb9834d5.png) 201 | 202 | ### Membershipサービスのスケールアウト 203 | 204 | Membershipサービスを3台にスケールアウトさせましょう。 205 | 206 | ``` console 207 | $ cf scale -i 3 membership-tmaki 208 | ``` 209 | 210 | Service Registryのインスタンス数が3になっていることを確認 211 | 212 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/50c48cfc-b3aa-6113-f32b-e994bdfc8135.png) 213 | 214 | Circuit Breaker Dashboardのスループットが変わらない(=メトリクスが集約されている)ことを確認 215 | 216 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5e406d84-c097-dfa3-5e8e-6c78b95cf11e.png) 217 | 218 | 219 | ### スクリプトによる作業自動化 220 | 221 | * アプリケーションのビルド 222 | * Spring Cloud Servicesの作成とアプリケーションのデプロイ 223 | 224 | は[こちら](https://github.com/making/metflix/tree/06_spring-cloud-services/scripts)のスクリプトを使えば自動で行えます。 225 | 226 | ``` console 227 | $ cd $WORKSHOP 228 | $ rm -rf scripts 229 | $ mkdir scripts 230 | $ cd scripts 231 | $ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/common.sh 232 | $ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/build.sh 233 | $ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/deploy.sh 234 | $ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/cleanup.sh 235 | $ chmod +x *.sh 236 | $ cd .. 237 | ``` 238 | 239 | `common.sh`に定義されている変数`suffix`を`tmaki`から自分のアカウントまたはイニシャルに変更してください。 240 | また、変数`git_url`を`https://github.com/making/metflix-config`から「[Config Serverの導入](config-server.md)」でForkしたURLに変更してください。 241 | 242 | 以下のコマンドでデプロイしたアプリケーションの削除とUser Provided Serviceの削除を行うことができます。 243 | 244 | ``` console 245 | $ cd $WORKSHOP 246 | $ ./scripts/cleanup.sh 247 | ``` 248 | -------------------------------------------------------------------------------- /spring-boot.md: -------------------------------------------------------------------------------- 1 | ## Spring Bootを始めよう 2 | 3 | 1. Mebership Service 4 | 1. Recommendations Service 5 | 1. UI 6 | 7 | の順にサービスをSpring Bootで作成していきます。 8 | 9 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c000d7ca-fc98-c0f5-c516-04c67861d419.png) 10 | 11 | 本ページで作成するソースコードは[こちら](https://github.com/making/metflix)(masterブランチ)から参照可能です。 12 | 13 | ### Membership Serviceの作成 14 | 15 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1d6e62fb-06b4-a72e-d7f8-eddda08a9595.png) 16 | 17 | #### 作業手順 18 | 19 | 1. File -> New -> Spring Starter Project 20 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 21 | 22 | 1. * Name : `membership` 23 | * Group: `com.metflix` 24 | * Artifact: `membership` 25 | * Package: `com.metflix` 26 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e4f01929-5c76-f477-bb25-f0db2364193b.png) 27 | 28 | 1. * Ops -> `Actuator` 29 | * Web -> `Web` 30 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/b56f00fd-3317-9fc8-5a03-d692b96be2e4.png) 31 | 1. workspaceを確認 32 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/22412502-2a9e-9f39-fc73-e7354e0ca3b9.png) 33 | 1. `src/main/java/com/metflix/MembershipApplication.java`を[この内容](https://github.com/making/metflix/blob/master/membership/src/main/java/com/metflix/MembershipApplication.java)に変更 34 | 1. `src/main/resources/application.properties`を[この内容](https://github.com/making/metflix/blob/master/membership/src/main/resources/application.properties)に変更 35 | 36 | > **Tips** 37 | > 38 | > 雛形プロジェクトは 39 | > ``` console 40 | > curl start.spring.io/starter.tgz \ 41 | > -d groupId=com.metflix \ 42 | > -d artifactId=membership \ 43 | > -d packageName=com.metflix \ 44 | > -d baseDir=membership \ 45 | > -d dependencies=web,actuator \ 46 | > -d applicationName=MembershipApplication | tar -xzvf - 47 | > ``` 48 | > でも生成可能です。この場合、File -> Import -> Maven -> Existing Maven Projectsで生成したフォルダを選択し、プロジェクトをインポートしてください。 49 | 50 | #### 動作確認 51 | 52 | Package Explorerの`MembershipApplication.java`を右クリック -> Run As -> Spring Boot App 53 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/b0e427bf-c6cc-cdd6-b3ea-3b64fbbad960.png) 54 | 55 | Consoleを確認 56 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2c5d4ab3-b3fd-d0ae-5ff4-7cdd645e35f6.png) 57 | 58 | [http://localhost:4444/api/members/making](http://localhost:4444/api/members/making)にアクセス 59 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/67e96d7c-1c5b-5ce7-7738-144d8f7681ea.png) 60 | 61 | [http://localhost:4444/api/members/tichimura](http://localhost:4444/api/members/tichimura)にアクセス 62 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ffbddb30-e975-9c68-7cad-6aa6e6c9745f.png) 63 | 64 | アカウントを作成 65 | 66 | ``` console 67 | $ curl -XPOST http://localhost:4444/api/members -H 'Content-Type: application/json' -d '{"user":"yamada","age":20}' 68 | ``` 69 | 70 | 作成したアカウントにアクセス 71 | 72 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/63a0084b-bac0-18c4-d4fe-2e592338bb9b.png) 73 | 74 | Boot Dashboardでアプリケーションの制御を行える。 75 | 76 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/61186a8e-82f9-95f2-cba3-bb0ccaf429d6.png) 77 | 78 | 79 | 80 | ### Recommendations Serviceの作成 81 | 82 | 83 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/46b64e20-b054-23a8-2ddf-565ce0bc731b.png) 84 | 85 | #### 作業手順 86 | 87 | 1. File -> New -> Spring Starter Project 88 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 89 | 90 | 1. * Name : `recommendations` 91 | * Group: `com.metflix` 92 | * Artifact: `recommendations` 93 | * Package: `com.metflix` 94 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/20e595bd-1c6e-03d7-cfbf-ace23b30ca00.png) 95 | 96 | 1. * Ops -> `Actuator` 97 | * Web -> `Web` 98 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/b56f00fd-3317-9fc8-5a03-d692b96be2e4.png) 99 | 1. workspaceを確認 100 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2e31cf1a-1475-a684-826d-fc113c49e002.png) 101 | 102 | 1. `src/main/java/com/metflix/RecommendationsApplication.java`を[この内容](https://github.com/making/metflix/blob/master/recommendations/src/main/java/com/metflix/RecommendationsApplication.java)に変更 103 | 1. `src/main/resources/application.properties`を[この内容](https://github.com/making/metflix/blob/master/recommendations/src/main/resources/application.properties)に変更 104 | 105 | > **Tips** 106 | > 107 | > 雛形プロジェクトは 108 | > ``` console 109 | > curl start.spring.io/starter.tgz \ 110 | > -d groupId=com.metflix \ 111 | > -d artifactId=recommendations \ 112 | > -d packageName=com.metflix \ 113 | > -d baseDir=recommendations \ 114 | > -d dependencies=web,actuator \ 115 | > -d applicationName=RecommendationsApplication | tar -xzvf - 116 | > ``` 117 | > でも作成可能です。 118 | 119 | #### 動作確認 120 | 121 | Package Explorerの`RecommendationsApplication.java`を右クリック -> Run As -> Spring Boot App 122 | 123 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/1f4b9fc1-6e1f-b725-e373-c150a3cc7f49.png) 124 | 125 | Consoleを確認 126 | 127 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ec81c23c-d7b9-0d9c-cf54-65c36ab421e6.png) 128 | 129 | [http://localhost:3333/api/recommendations/making](http://localhost:3333/api/recommendations/making)にアクセス 130 | 131 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e5fc5994-9e9c-5f1f-5908-5d6313cae7a2.png) 132 | 133 | Membershipサービスにアクセスがあることを確認 134 | 135 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/b077db35-37cd-5ba5-12f9-40fe827018d0.png) 136 | 137 | 138 | [http://localhost:3333/api/recommendations/tichimura](http://localhost:3333/api/recommendations/tichimura)にアクセス 139 | 140 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/9daf2eeb-d6ba-aac8-f8bb-83a69b4a0658.png) 141 | 142 | ### UIの作成 143 | 144 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/f8dce7cf-0e1d-2910-7f27-f92a128abb02.png) 145 | 146 | #### 作業手順 147 | 148 | 1. File -> New -> Spring Starter Project 149 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 150 | 151 | 1. * Name : `ui` 152 | * Group: `com.metflix` 153 | * Artifact: `ui` 154 | * Package: `com.metflix` 155 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/5afbc202-33f6-077d-cc9b-2a879905514a.png) 156 | 157 | 158 | 1. * Core -> `Security` 159 | * Ops -> `Actuator` 160 | * Web -> `Web` 161 | * Template Engines -> `Thymeleaf` 162 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/a6b922ac-5f30-897a-8312-263311084910.png) 163 | 164 | 1. workspaceを確認 165 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/892c14ab-4ccc-a474-2136-f6ac9637442c.png) 166 | 167 | 1. `src/main/java/com/metflix/UiApplication.java`を[この内容](https://github.com/making/metflix/blob/master/ui/src/main/java/com/metflix/UiApplication.java)に変更 168 | 1. `src/main/resources/application.properties`を[この内容](https://github.com/making/metflix/blob/master/ui/src/main/resources/application.properties)に変更 169 | 1. `src/main/resource/templates`を右クリック -> New -> File 170 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/610f6cd8-1828-98ee-13c0-436531ff3aa9.png) 171 | 1. Filename: `index.html` 172 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/8e13987c-1c85-4c42-f674-c6144fc52f5b.png) 173 | 1. `src/main/resources/templates/index.html`を[この内容](https://github.com/making/metflix/blob/master/ui/src/main/resources/templates/index.html)に変更 174 | 175 | > **Tips** 176 | > 177 | > 雛形プロジェクトは 178 | > ``` console 179 | > curl start.spring.io/starter.tgz \ 180 | > -d groupId=com.metflix \ 181 | > -d artifactId=ui \ 182 | > -d packageName=com.metflix \ 183 | > -d baseDir=ui \ 184 | > -d dependencies=web,thymeleaf,security,actuator \ 185 | > -d applicationName=UiApplication | tar -xzvf - 186 | > ``` 187 | > でも作成可能です。 188 | 189 | 190 | ### 動作確認 191 | 192 | Package Explorerの`UiApplication.java`を右クリック -> Run As -> Spring Boot App 193 | 194 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/a2dfaf28-0ced-eea1-335c-55df59cdddcf.png) 195 | 196 | コンソールを確認 197 | 198 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/bf43ccdd-fcc0-f44e-ba85-e5528a4f990f.png) 199 | 200 | [http://localhost:8080](http://localhost:8080)にアクセス 201 | 202 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/560242ca-3477-1f95-de59-e0ea83864ed3.png) 203 | 204 | 205 | * ユーザー名: `making` (他のユーザー名でも可) 206 | * パスワード: `metflix` 207 | 208 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/a66d0daa-a731-343f-64df-2a544960cb50.png) 209 | 210 | MembershipサービスとRecommendationsサービスにアクセスがあることを確認 211 | 212 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e022f986-0052-c5aa-8440-21e707c3fd03.png) 213 | 214 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c7f1868b-ad25-6d27-a0dd-f0351f65499b.png) 215 | 216 | ### 実行可能jarの作成 217 | 218 | Boot Dashboardから3つのサービスを停止してください。 219 | 220 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/f87f2d08-ca8a-62d8-da24-13807acfe973.png) 221 | 222 | STSで"Run As -> Spring Boot App"を実行する代わりに、`./mvnw package`で実行可能jarファイルを作成して、これを実行することもできます。 223 | 224 | ``` console 225 | $ export WORKSHOP= 226 | $ cd $WORKSHOP/membership 227 | $ ./mvnw clean package -DskipTests=true 228 | $ java -jar target/membership-0.0.1-SNAPSHOT.jar 229 | ``` 230 | 231 | ``` console 232 | $ cd $WORKSHOP/recommendations 233 | $ ./mvnw clean package -DskipTests=true 234 | $ java -jar target/recommendations-0.0.1-SNAPSHOT.jar 235 | ``` 236 | 237 | 238 | ``` console 239 | $ cd $WORKSHOP/ui 240 | $ ./mvnw clean package -DskipTests=true 241 | $ java -jar target/ui-0.0.1-SNAPSHOT.jar 242 | ``` 243 | -------------------------------------------------------------------------------- /distributed-tracing.md: -------------------------------------------------------------------------------- 1 | ## Distributed Tracingの導入 2 | 3 | 次に、Distributed Tracingの考え方を導入し、1リクエスト中の各サービスの呼び出しに関してどこでどのくらいの時間がかかっているのかを把握しましょう。 4 | トレージングデータを収集、検索、視覚化するサーバーとして[Zipkin](http://zipkin.io/)を利用します。 5 | 各サービスからトレージングデータを透過的に送信するクライアント(instrumentation)として[Spring Cloud Sleuth](https://cloud.spring.io/spring-cloud-sleuth/)を利用します。 6 | 7 | 本ページで作成するソースコードは[こちら](https://github.com/making/metflix/tree/07_distributed-tracing)(`07_distributed-tracing`ブランチ)から参照可能です。 8 | 9 | ### Zipkin Serverの作成 10 | 11 | #### 作業手順 12 | 13 | 1. File -> New -> Spring Starter Project 14 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/642499a3-0f6f-8e3d-6f65-499808937abf.png) 15 | 16 | 1. * Name : `zipkin-server` 17 | * Group: `com.metflix` 18 | * Artifact: `zipkin-server` 19 | * Package: `com.metflix` 20 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/6d22969f-ea9c-dad5-038a-52293444629e.png) 21 | 22 | 1. * Cloud Config -> `Config Client` 23 | * Cloud Tracing -> `Zipkin UI`, `Zipkin Server` 24 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/af03d5b7-35f0-f1e9-2919-4eba4ffe451d.png) 25 | 26 | 27 | 1. workspaceを確認 28 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/3c606669-de0e-71d6-4b13-a73e42281f87.png) 29 | 30 | 1. `src/main/java/com/metflix/ZipkinServerApplication.java`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/zipkin-server/src/main/java/com/metflix/ZipkinServerApplication.java)に変更 31 | 1. `src/main/resources/application.properties`を**削除** 32 | 1. `src/main/resources`を右クリック -> New -> File 33 | 1. File name: `bootstrap.properties` 34 | 1. `src/main/resources/bootstrap.properties`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/zipkin-server/src/main/resources/bootstrap.properties)に変更 35 | 1. forkした[`metflix-confg`](https://github.com/making/metflix-config)プロジェクトの[`service-registry`](https://github.com/making/metflix-config/tree/service-registry)ブランチに`zipkin-server.properties`を作成して、[この内容](https://github.com/making/metflix-config/blob/service-registry/zipkin-server.properties)に変更 36 | スクリーンショット 0028-06-18 18.45.53.png 37 | 38 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/23aad312-50a5-18ba-cf6d-514fb2dd4958.png) 39 | 40 | 41 | 42 | #### 動作確認 43 | 44 | Package Explorerの`ZipkinServerApplication.java`を右クリック -> Run As -> Spring Boot App 45 | 46 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c1644acd-0fad-0650-0a96-71da2f05a573.png) 47 | 48 | コンソールを確認。9411番ポートで起動していない場合はConfig Serverの設定が正しくありません。 49 | 50 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/22ca8466-6be6-6724-f41e-e74706111cc8.png) 51 | 52 | [http://localhost:9411](http://localhost:9411)にアクセス 53 | 54 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/76b6a36f-e4f4-e285-2fec-8ab444354a32.png) 55 | 56 | 57 | > Zipkin Serverは以下のように実行可能jarをダウンロードすることでも実行可能です。 58 | > 59 | > ``` console 60 | > $ wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' 61 | > $ java -jar zipkin.jar 62 | > ``` 63 | > ストレージの変更やプロパティの変更を行う場合は、Zipkin Server用のプロジェクトを作成した方がメンテナンスしやすいです。今回はConfig Clientの設定を追加したため、あえてプロジェクトを作成しました。 64 | 65 | ### Zipkin Client (Instrumentation)の設定 66 | 67 | Zipkinクライアントとして、各サービスにSpring Cloud Sleuth(`org.springframework.cloud:spring-cloud-starter-zipkin`)を依存関係を追加します。この依存関係を追加することにより、Springアプリケーションからの外部へのリクエスト情報(Span)が蓄積され、`org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter`クラスが非同期にZipkin Serverに情報を送信するための設定が自動で行われます。Instrument対象は[こちら](http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_integrations)。 68 | 69 | `ZipkinSpanReporter`の実装としてデフォルトでは`org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter`(HTTPで送信)が使用されます。 70 | リクエスト情報は`org.springframework.cloud.sleuth.Sampler`実装クラスによってサンプリングされます。ここでは`org.springframework.cloud.sleuth.sampler.PercentageBasedSampler`が使用され、リクエストの10%がZipkin Serverに送信されます。この割合は`spring.sleuth.sampler.percentage`プロパティで変更できます(デフォルトは`0.1`)。 71 | 72 | #### 作業手順 73 | 74 | ##### Membership 75 | 76 | 1. `membership`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/membership/pom.xml)に変更 77 | 78 | ##### Recommendations 79 | 80 | 1. `recommendations`の`pom.xml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/recommendations/pom.xml)に変更 81 | 82 | 83 | ##### UI 84 | 85 | #### 動作確認 86 | 87 | 1. `membership`、`recommendations`、`ui`を再起動 88 | 89 | [http://localhost:8080](http://localhost:8080)に何回かアクセスしてください。 90 | 91 | ログに`[appname,traceId,spanId,exportable]`が含まれることを確認してください。 92 | 93 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/cc6bab4b-68bc-448f-ba63-13f49b3f68a0.png) 94 | 95 | [http://localhost:9411](http://localhost:9411)にアクセスし、「`Find Traces`」ボタンを押してください。 96 | 97 | スクリーンショット 0028-06-18 20.05.58.png 98 | 99 | 複数のTraceが表示されます。一つクリックしてください。 100 | 101 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/37bc3e13-f046-ed5d-fc24-bcb1e846199b.png) 102 | 103 | 選択したTraceの中の各Spanでどのくらいの時間を要しているのかが視覚的にわかります(「`Expand All`」ボタンを押すと見やすいです)。 104 | 105 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2bbea156-eca0-276e-960b-5c91ef18a03f.png) 106 | 107 | 108 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c5e1ed12-f2e1-f94e-685e-fcac9e0aca63.png) 109 | 110 | 「Dependencies」タブをクリックするとサービスの依存関係がわかります。 111 | 112 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/f91b18b0-26ad-2a85-8f14-5a268e59af20.png) 113 | 114 | 115 | 次にサンプリング率を変更して全てのリクエストが収集されるようにしましょう。 116 | 117 | forkした[`metflix-confg`](https://github.com/making/metflix-config)プロジェクトの[`service-registry`](https://github.com/making/metflix-config/tree/service-registry)ブランチの`application.properties`に 118 | 119 | ``` properties 120 | spring.sleuth.sampler.percentage=1.0 121 | ``` 122 | 123 | を追加してください。 124 | 125 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/746f1fe6-c87e-068d-6149-24be9e2f8e0f.png) 126 | 127 | 各サービスを再起動するか`/refresh`にPOSTでリクエストを送ってください。開発中はこの設定が便利です。 128 | 129 | ### Cloud Foundryにデプロイ 130 | 131 | 「[Cloud Foundryにデプロイ](cloud-foundry.md)」でデプロイしたサービスにZipkin Serverを連携させましょう。 132 | 133 | #### Zipkin Serverのデプロイ 134 | 135 | ``` console 136 | $ cd $WORKSHOP/zipkin-server 137 | $ ./mvnw clean package -Dmaven.test.skip=true 138 | ``` 139 | 140 | `zipkin-server`プロジェクト直下に`manifest.yml`を作成して[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/zipkin-server/manifest.yml)を設定し、`cf push`でデプロイします。(`tmaki`の部分は自分のアカウント名またはイニシャルに置き換えてください。) 141 | 142 | ``` console 143 | $ cf push zipkin-server-tmaki 144 | ``` 145 | 146 | 次にこのZipkin ServerをService Instanceとして他のサービスにバインドできるようにUser Provider Serviceを作成します。 147 | 148 | ``` console 149 | $ cf create-user-provided-service zipkin-server -p '{"uri":"http://zipkin-server-tmaki.cfapps.io"}' 150 | ``` 151 | 152 | #### Membership, Recommendations, UIのデプロイ 153 | 154 | `membership`, `recommendations`, `ui`の接続先のZipkin ServerのURLをUser Provided Serviceの`credintials`の値を使うように、Config Serverのプロパティをを設定します。またCloud Foundry上では`spring.sleuth.sampler.percentage`が`0.1`になるように設定します。 155 | Cloud Foundryで起動する場合にのみ適用されるように`application-cloud.properties`を、[この内容](https://github.com/making/metflix-config/blob/service-registry/application-cloud.properties)を変更してください。 156 | 157 | 158 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ded2c553-29ed-b641-c1c3-bc4e492553bf.png) 159 | 160 | 161 | 次に各サービスの`manifest.yml`に`zipkin-server`サービスをバインドするように設定します。 162 | 163 | 164 | ##### Membership 165 | `manifest.yml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/membership/manifest.yml)に変更。 166 | 167 | ``` console 168 | $ cd $WORKSHOP/membership 169 | $ ./mvnw clean package -Dmaven.test.skip=true 170 | $ cf push membership-tmaki 171 | ``` 172 | 173 | ##### Recommendations 174 | `manifest.yml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/recommendations/manifest.yml)に変更。 175 | 176 | ``` console 177 | $ cd $WORKSHOP/recommendations 178 | $ ./mvnw clean package -Dmaven.test.skip=true 179 | $ cf push recommendations-tmaki 180 | ``` 181 | 182 | ##### UI 183 | `manifest.yml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing/ui/manifest.yml)に変更。 184 | 185 | ``` console 186 | $ cd $WORKSHOP/ui 187 | $ ./mvnw clean package -Dmaven.test.skip=true 188 | $ cf push ui-tmaki 189 | ``` 190 | 191 | #### 動作確認 192 | 193 | [http://ui-tmaki.cfapps.io](http://ui-tmaki.cfapps.io)にアクセス (tmakiを自分のアカウントまたはイニシャルに変更してください)。 194 | 195 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/b6cf24c7-56f1-daae-8b0e-c2b9dfd2d8cd.png) 196 | 197 | [http://zipkin-server-tmaki.cfapps.io](http://zipkin-server-tmaki.cfapps.io)にアクセス 198 | 199 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/f86fc70b-2cb3-fcb7-ecd0-3b0b1872df47.png) 200 | 201 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/2bd913cd-1434-750b-888b-adabd41bf96c.png) 202 | 203 | 204 | #### StrageをMySQLに変更 205 | 206 | デフォルトではZipkin ServerのStrageはインメモリ実装(`zipkin.storage.InMemoryStorage`)なので、再起動するとデータが揮発します。また、アプリケーションのヒープを圧迫するので、開発中以外には利用できません。 207 | 208 | ZipkinはStrageの実装に[MySQL](https://github.com/openzipkin/zipkin/tree/master/zipkin-storage/mysql)、[Cassandra](https://github.com/openzipkin/zipkin/tree/master/zipkin-storage/cassandra)、[Elasticsearch](https://github.com/openzipkin/zipkin/tree/master/zipkin-storage/elasticsearch)を選択できます。 209 | 210 | ストレージをMySQLに変更しましょう。依存関係に`io.zipkin.java:zipkin-autoconfigure-storage-mysql`を追加すれば自動で設定されます。(`zipkin-server`プロジェクトの`pom.xml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing-mysql/zipkin-server/pom.xml)に変更してください。) 211 | 212 | Zipkin ServerのストレージとしてMySQLを使うためのプロパティをConfig Serverに設定します。Cloud Foundryで起動する場合にのみ適用されるように`zipkin-server-cloud.properties`を作成して、[この内容](https://github.com/making/metflix-config/blob/service-registry/zipkin-server-cloud.properties)を設定してください。 213 | 214 | ![image](https://qiita-image-store.s3.amazonaws.com/0/1852/7f41f790-92c2-ed25-5f6f-e5f06c30f2a8.png) 215 | 216 | 217 | Pivotal Web Servicesで使えるMySQLサービス(`cleardb`)のフリープラン(`spark`)で`zipkin-mysql`というService Instanceを作成します。 218 | 219 | ``` console 220 | $ cf create-service cleardb spark zipkin-mysql 221 | ``` 222 | 223 | `zipkin-mysql`サービスをバインドするように`manifest.yml`を[この内容](https://github.com/making/metflix/blob/07_distributed-tracing-mysql/zipkin-server/manifest.yml)に変更して、`cf push`でデプロイしましょう。 224 | 225 | ``` console 226 | $ cd $WORKSHOP/zipkin-server 227 | $ ./mvnw clean package -Dmaven.test.skip=true 228 | $ cf push zipkin-server-tmaki 229 | ``` 230 | 231 | 232 | これで、Zipkin Serverのデータが永続化され、再起動しても消えません。 233 | 234 | > ZipkinのストレージとしてのMySQLは[パフォーマンスの問題](https://github.com/openzipkin/zipkin#mysql)が知られています。高負荷下での利用を検討している場合は、CassandraやElasticsearchも検討してください。Pivotal Web ServicesではElasticsearchのサービスとして`searchly`を利用できます。 235 | > 236 | > ``` console 237 | > $ cf create-service searchly starter zipkin-elasticsearch 238 | > ``` 239 | > `zipkin-autoconfigure-storage-mysql`の代わりに`zipkin-storage-elasticsearch`を使用してください。 240 | 241 | ### Spring Cloud Streamとの連携 242 | 本章で説明したZipkin ClientはHTTPのみに対応していましたが、`spring-cloud-sleuth-zipkin-stream`を使用することによりSpring Cloud Streamによるメッセージングのトレーシングも可能になります。またZipkin ServerへのレポートもSpring Cloud StreamがサポートするbinderであるRabbitMQまたはKafkaを経由して行われます。 243 | 244 | この場合は、Zipkin Serverの実装は`ZipkinServerApplication`クラスに`@EnableZipkinServer`の代わりに`@EnableZipkinStreamServer`をつけます。 245 | 246 | 247 | 248 | --------------------------------------------------------------------------------