Skip to content

Latest commit

 

History

History
247 lines (163 loc) · 10.5 KB

spring-cloud-services.md

File metadata and controls

247 lines (163 loc) · 10.5 KB

Spring Cloud Servicesの利用

Pivotal Cloud FoundrySpring Cloud Servicesを利用することで、

  • Config Server
  • Service Registry
  • Circuit Breaker Dashboard

はプラットフォームが提供するため、自前で管理不要になります。 かつこれらのサービスへのアクセスをOAuth 2によって自動で認可制御されます。

image

config-servereureka-serverは不要なので削除して構いません。

本ページで作成するソースコードとスクリプトはこちら(06_spring-cloud-servicesブランチ)から参照可能です。 また、Spring Cloud Servicesのマニュアルはこちらです。

アプリケーション一部修正

Spring Cloud Servicesへのアクセスを自動設定するためのSpring Cloud Services Startersを使うように設定します。

本稿作成時点で、Spring Cloud Servicesに対応しているSpring Bootのバージョンが1.2、Spring CloudのバージョンがAngelであるため、アプリケーションを一部変更します。

また、Spring Cloud Services用のコンフィギュレーションを使用するようにbootstrap.propertiesを変更します(差分)。

manifest.ymlもPivotal Cloud Foundry用に変更します (buildpack, memory増加, Hystrix Dashboardサービスのバインド)。

Members

  1. membershippom.xmlこの内容に変更
  2. membershipsrc/main/resources/bootstrap.propertiesこの内容に変更
  3. membershipmanifest.ymlこの内容に変更

Recommendations

  1. recommendationspom.xmlこの内容に変更
  2. recommendationssrc/main/resources/bootstrap.propertiesこの内容に変更
  3. recommendationssrc/main/java/com/metflix/RecommendationsApplication.javaこの内容に変更
  4. recommendationsmanifest.ymlこの内容に変更

UI

  1. uipom.xmlこの内容に変更
  2. uisrc/main/resources/bootstrap.propertiesこの内容に変更
  3. uisrc/main/java/com/metflix/UiApplication.javaこの内容に変更
  4. uimanifest.ymlこの内容に変更

アプリケーションのビルド

membershiprecommendationsuiプロジェクトをビルドします

$ cd $WORKSHOP/membership
$ ./mvnw clean package -Dmaven.test.skip=true
$ cd $WORKSHOP/recommendations
$ ./mvnw clean package -Dmaven.test.skip=true
$ cd $WORKSHOP/ui
$ ./mvnw clean package -Dmaven.test.skip=true

Tips

スクリプトによる作業自動化の準備を行えば、

$ cd $WORKSHOP
$ ./scripts/build.sh

を実行することで、全アプリケーションのビルドが行われます。

Pivotal Cloud Foundryへデプロイ

ログイン

※ アカウントの作成は講師が行います。

$ cf login -a api.run.pez.pivotal.io

Service (Spring Cloud Services)の作成

Config Serverサービスの作成

GitのURLをhttps://github.com/making/metflix-configから「Config Serverの導入」でForkしたURLに変更してください。

$ cf create-service p-config-server standard config-server -c '{"git":{"uri":"https://github.com/making/metflix-config", "label":"spring-cloud-services"}}' 
Service Registryサービスの作成
$ cf create-service p-service-registry standard eureka-server
Hystrix Dashboardサービスの作成
$ cf create-service p-circuit-breaker-dashboard standard hystrix-dashboard

Pivotal Cloud Foundryへアプリケーションをデプロイ

Manifestマニフェストファイルを使ってPivotal Cloud Foundryにjarをデプロイします。

$ cd $WORKSHOP/membership
$ cf push membership-tmaki
$ cd $WORKSHOP/recommendations
$ cf push recommendations-tmaki
$ cd $WORKSHOP/ui
$ cf push ui-tmaki

※ 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してください。

Tips

スクリプトによる作業自動化の準備を行えば、

$ cd $WORKSHOP
$ ./scripts/deploy.sh

を実行することで、Serviceの作成及び、アプリケーションのデプロイが行われます。

動作確認

cf appscf servicesの結果が以下のようになっていることを確認。

$ cf apps

name                    requested state   instances   memory   disk   urls   
recommendations-tmaki   started           1/1         768M     1G     recommendations-tmaki.cfapps.pez.pivotal.io   
ui-tmaki                started           1/1         768M     1G     ui-tmaki.cfapps.pez.pivotal.io   
membership-tmaki        started           1/1         768M     1G     membership-tmaki.cfapps.pez.pivotal.io   
$ cf services

name                service                       plan       bound apps                                          last operation   
eureka-server       p-service-registry            standard   recommendations-tmaki, ui-tmaki, membership-tmaki   create succeeded   
config-server       p-config-server               standard   recommendations-tmaki, ui-tmaki, membership-tmaki   create succeeded   
hystrix-dashboard   p-circuit-breaker-dashboard   standard   recommendations-tmaki, ui-tmaki                     create succeeded 

http://ui-tmaki.cfapps.pez.pivotal.ioにアクセス (tmakiを自分のアカウントまたはイニシャルに変更してください)。

image

以下のコマンドで、システムに負荷をかけてください。

$ while true;do curl -u making:metflix http://ui-tmaki.cfapps.pez.pivotal.io > /dev/null; done

Apps Managerにログイン。

image

自分のSpaceを確認

image

config-serverのManageリンクをクリック

image

eureka-serverのManageリンクをクリック

image

hystrix-dashboardのManageリンクをクリック。Turbineが有効になっており、Hystrixのストリームが集約されていることを確認

image

Membershipサービスのスケールアウト

Membershipサービスを3台にスケールアウトさせましょう。

$ cf scale -i 3 membership-tmaki

Service Registryのインスタンス数が3になっていることを確認

image

Circuit Breaker Dashboardのスループットが変わらない(=メトリクスが集約されている)ことを確認

image

スクリプトによる作業自動化

  • アプリケーションのビルド
  • Spring Cloud Servicesの作成とアプリケーションのデプロイ

こちらのスクリプトを使えば自動で行えます。

$ cd $WORKSHOP
$ rm -rf scripts
$ mkdir scripts
$ cd scripts
$ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/common.sh
$ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/build.sh
$ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/deploy.sh
$ curl -OL https://github.com/making/metflix/raw/06_spring-cloud-services/scripts/cleanup.sh
$ chmod +x *.sh
$ cd ..

common.shに定義されている変数suffixtmakiから自分のアカウントまたはイニシャルに変更してください。 また、変数git_urlhttps://github.com/making/metflix-configから「Config Serverの導入」でForkしたURLに変更してください。

以下のコマンドでデプロイしたアプリケーションの削除とUser Provided Serviceの削除を行うことができます。

$ cd $WORKSHOP
$ ./scripts/cleanup.sh