---
title: Pivotal Web Services / Pivotal Cloud FoundryはSpring Bootのベストなプラットフォーム
tags: ["Cloud Foundry", "Pivotal Web Services", "Pivotal Cloud Foundry", "Spring Boot", "Java"]
categories: ["Programming", "Java", "org", "springframework", "boot"]
date: 2017-06-12T07:34:15Z
updated: 2017-06-27T08:03:40Z
---

**目次**
<!-- toc -->

### はじめに

Cloud Foundry（特に、[Pivotal Web Services (以降、PWS)](https://run.pivotal.io)及び[Pivotal Cloud Foundry (以降、PCF)](https://docs.pivotal.io/pivotalcf/1-11/installing/pcf-docs.html)）はSpring Bootアプリケーションをデプロイする先として、最適なプラットフォームになるべく機能が追加されている。

Cloud FoundryとSpring Bootの連携としては

* `cf push -p app.jar`のみでデプロイ可能
* `cloud`プロファイルの自動適用
* [Spring Cloud Connector](http://cloud.spring.io/spring-cloud-connectors/)でDBなどの接続情報設定不要化
* Spring Boot Actuatorの[Cloud Foundry向けEndpoint](http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/html/production-ready-cloudfoundry.html)
* Apps Manager(アプリケーション管理UI)の[Spring Boot Actuator連携](http://docs.pivotal.io/pivotalcf/console/using-actuators.html) (**PWS, PCFのみ**)
* GoRouterによるZipkin Headerの付与
* Hosted Zipkinとして使える[PCF Metrics](https://docs.pivotal.io/pcf-metrics)の[Trace Explorer](https://docs.pivotal.io/pcf-metrics/1-3/using.html#trace) (**PWS, PCFのみ**)
* Managed Config Server, Eureka Server, Hystrix Dashboardを提供する[Spring Cloud Services](http://docs.pivotal.io/spring-cloud-services) (**PWS, PCFのみ**)

が挙げられるが、特にわかりやすく便利なのがApps ManagerのSpring Boot Actuator連携。以前にも[紹介](https://blog.ik.am/entries/401)したが、PCF 1.11でUIが変わりさらに機能が加わるので改めて紹介する。PWSではすでに[利用可能](http://docs.run.pivotal.io/console/using-actuators.html)になっている。

### Apps ManagerにサポートされているActuatorエンドポイント

Spring Boot Actuatorには現状、デフォルトで次のエンドポイントが利用可能である。Apps Managerがサポートしているエンドポイントにマークを入れた。


| Path | Short Description | Apps Manager Support |
| --- | --- | --- |
| `/auditevents ` | 認証の成功失敗に関する監査ログを表示 |  |
| `/autoconfig` | AutoConfigurationのうち、有効になっているもの、なっていないものを列挙 |  |
| `/beans ` | Springに管理されているBean一覧を表示 |  |
| `/configprops` | `@ConfigurationProperties`のついたBeanに実際に設定されている値を表示 |  |
| `/dump` | スレッドダンプを表示 | &#127381; |
| `/env` | 環境変数や設定されたプロパティを表示 |  |
| `/health` | データベースなどのヘルスチェック結果を表示 | &#9989; |
| `/info` | アプリケーションの情報を表示 | &#9989; |
| `/loggers` | ログレベルの表示及び変更 | &#9989; |
| `/metrics` | アプリケーションのメトリクスを表示 |  |
| `/mappings ` | `@RequestMapping`で定義されているパスとContollerメソッドのマッピング一覧を表示 |  |
| `/trace` | リクエストのトレースログを表示 | &#127381; |
| `/heapdump` | GZipで圧縮された`hprof`のheapdumpファイルをダウンロード | &#127381; |

&#9989;がついているものが前回紹介した時からApps Managerがサポートしていた機能で、&#127381;がついているものがPCF 1.11で追加されるもの(PWSではすでに利用可能)。

Actuator連携の嬉しい点は**認可対策が自動で行われる**点。
Spring Boot 1.5からActuatorのエンドポイントがデフォルトで認可制御されるようになった。Actuatorは機密情報を含む場合があるため、基本的にセキュリティの設定が必須であるが、PWS / PCFにデプロイした場合は、認可設定をしたままでもApps Manager上からはアクセスできるので、開発者がケアしないといけない点が減る。

> Memo:
>
> ちなみに、`/health`と`/info`エンドポイントはデフォルトで、誰でもアクセスできるようになっている。
> ただし、`/health`に関しては、認可されてない場合は個別サービスのヘルスチェック結果は表示されず、全てを総合して`UP`か`DOWN`かだけ返す。
>
> <img src="https://user-images.githubusercontent.com/106908/27040986-972c4352-4fcd-11e7-8c71-ca7f15eb0e97.png" width="40%" />

### プロジェクトの設定

普通のSpring Bootプロジェクトを[Spring Initializr](https://start.spring.io)から作成すればいいが、Acutator連携を試したいので、`spring-boot-start-actuator`が必要。また、対応するSpring Bootのバージョンは1.5以上。

``` xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```

infoエンドポイントにアプリケーションのバージョン情報を出力させたい場合は、次のように`spring-boot-maven-plugin`に`build-info`goalを追加する必要がある。

``` xml
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<goals>
				<goal>build-info</goal>
			</goals>
		</execution>
	</executions>
</plugin>
```

また、infoエンドポントにGitのメタ情報を出力させたい場合は、次のように

``` xml
<plugin>
	<groupId>pl.project13.maven</groupId>
	<artifactId>git-commit-id-plugin</artifactId>
</plugin>
```

さらに、Git情報にremoteのurlなど詳細な情報を含めたい場合は`application.properties`に次の設定が必要である。

```
management.info.git.mode=full
```

以上の設定は特にApps Manager連携に特化したものではない。

> Memo: 
> 
> 自己証明書使ったPivotal Cloud Foundryの場合、`application.properties`に次の設定も必要。
> 
> ```
> management.cloudfoundry.skip-ssl-validation=true
> ```

### アプリケーションのデプロイ

Pivotal Web Servicesを使う場合は、アカウントを作成して

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

でログイン。

```
$ ./mvnw clean package -DskipTests=true
```

で

```
$ cf push your-app-name -p target/you-app-name-0.0.1-SNAPSHOT.jar 
```

でデプロイ完了。

<img src="https://user-images.githubusercontent.com/106908/27024982-c3048028-4f93-11e7-9794-3e3eaec2e0f8.png" width="60%" />

[Apps Manager](https://console.run.pivotal.io)にログインして、デプロイしたアプリケーションを開き、左上にSpring Bootマークが出ていたらAcutaotr連携成功。


### healthエンドポイント

一番わかりやすいhealthエンドポイントから。Apps Managerの「Instances」を開くと、`/health`エンドポイントの結果が出力される。(この例ではMySQLとRabbitMQに接続している)

<img src="https://user-images.githubusercontent.com/106908/27025558-e98df678-4f95-11e7-96eb-3418c1abe6f0.png" width="60%" />

どこかに障害が発生していると赤文字で**DOWN**が表示される。

「View JSON」をクリックすると、JSONの出力結果がそのまま表示される。

<img src="https://user-images.githubusercontent.com/106908/27026589-37d293a4-4f99-11e7-9d7e-220ad52b5f7a.png" width="40%" />


```
$ cf scale your-app-name -i 2
```

でスケールアウトしても、各インスタンスの情報がそれぞれ出力されてGood。

<img src="https://user-images.githubusercontent.com/106908/27026879-41705562-4f9a-11e7-8a74-7de319821ccf.png" width="60%" />

連携サービスが多くなれば多くなるほどこの機能は重宝する。

### infoエンドポイント

infoエンドポイントの情報は「Settings」タブの真ん中あたりの「Spring Info」に表示される。

Gitのコミット情報が表示されている。

<img src="https://user-images.githubusercontent.com/106908/27029755-9b0b2606-4fa4-11e7-8f30-f3d3abb4768a.png" width="60%" />

SHAハッシュのリンクをクリックすればRemote先(Githubなど)のコミットログに飛べる。

<img src="https://user-images.githubusercontent.com/106908/27041618-74249a7e-4fcf-11e7-8867-60ec9054b233.png" width="60%" />

地味だけど、右上にもコミットのリンクが表示されている。

![image](https://user-images.githubusercontent.com/106908/27030052-c06d1bd8-4fa5-11e7-8c80-d6509b598351.png)


「VIEW RAW JSON」をクリックすれば`/info`のJSONの出力結果がそのまま表示される。アプリケーションのバージョンはここから確認できる。

<img src="https://user-images.githubusercontent.com/106908/27029878-1820f8aa-4fa5-11e7-896f-d0399e63b432.png" width="40%" />



### loggersエンドポイント

「Logs」タブを見ると「CONFIGURE LOGGING LEVELS」ボタンが見える。

<img src="https://user-images.githubusercontent.com/106908/27041047-ca03e140-4fcd-11e7-8b00-1e8afcfea691.png" width="60%" />

このボタンを押すとログレベル変更ダイアログが出てくる。

<img src="https://user-images.githubusercontent.com/106908/27041165-212cc450-4fce-11e7-89ef-81f17db8bcc8.png" width="60%" />

変更したいロガーをFilterに入力し、


<img src="https://user-images.githubusercontent.com/106908/27041238-5e235b76-4fce-11e7-94ef-def4c15533de.png" width="60%" />

変更したいレベルをクリックすれば、指定したロガーのレベル変更される。子のロガーのレベルも変更される。

<img src="https://user-images.githubusercontent.com/106908/27041317-9c82154c-4fce-11e7-8db9-b7d977f7041c.png" width="60%" />


ちなみにSpring Bootアプリに限らず、▶︎ボタンを押せばログがWebSocketで流れてくる。

ここまでは、すでにサポートされていた機能である。

### dumpエンドポイント

ここから、新機能。まずはdumpエンドポイント。「Threds」タブを見ると、アプリケーションの現在のスレッド一覧と状態が表示される。

<img src="https://user-images.githubusercontent.com/106908/27041932-73724aa8-4fd0-11e7-824a-314eed6372d8.png" width="60%" />

スレッドをクリックすると、スレッドダンプが表示される。

<img src="https://user-images.githubusercontent.com/106908/27041901-57c1036c-4fd0-11e7-8310-5080e5e25da8.png" width="60%" />

スケールアウトしている場合は、インスタンスの指定ができる。

![image](https://user-images.githubusercontent.com/106908/27042019-be59a156-4fd0-11e7-932d-20e034acbbda.png)

スレッドの状態でフィルタリングもできる。

<img src="https://user-images.githubusercontent.com/106908/27042038-c9729124-4fd0-11e7-880f-247b679b5ef4.png" width="40%" />

「Download」を押せばスレッドダンプがダウンロードされるし、「REFRESH」ボタンを押せばスレッドダンプが更新される。

### traceエンドポイント

次にtraceエンドポント。「Trace」タブを見ると、直近のリクエストログされる。

<img src="https://user-images.githubusercontent.com/106908/27042388-c2c77d7a-4fd1-11e7-91e8-7431cc23f793.png" width="60%" />

行を選択すると、そのリクエストのレスポンスタイムとリクエストヘッダ、レスポンスヘッダが表示される。

<img src="https://user-images.githubusercontent.com/106908/27042464-0127a37e-4fd2-11e7-854b-02cdf40d0f92.png" width="60%" />


デフォルトでは全インスタンスのリクエストログが集約されるが、インスタンスを指定することもできる。「REFRESH」ボタンを押せばリクエストログが更新される。

![image](https://user-images.githubusercontent.com/106908/27042580-619dfc3a-4fd2-11e7-84f0-76c2223b4628.png)


### heapdumpエンドポイント

最後にheapdumpエンドポイント。「Overview」タブの「Instances」欄で右端のボタンをクリックするとそのインスタンスのHeap Dump、Trace、Thread Dumpへのリンクが表示される。

<img src="https://user-images.githubusercontent.com/106908/27043028-c107a242-4fd3-11e7-8deb-48857b638a7e.png" width="60%" />

「Heap Dump」をクリックすると、GZip形式でHeap Dumpがダウンロードできる。


<img src="https://user-images.githubusercontent.com/106908/27043388-f41ab182-4fd4-11e7-93e8-3e0686f91113.png" width="60%" />

展開したファイルは`hprof`形式である。

<img src="https://user-images.githubusercontent.com/106908/27043344-cd5bc518-4fd4-11e7-927b-0c06534d973c.png" width="60%" />

[Eclipse Memory Analyzer](http://www.eclipse.org/mat/)などでそのまま解析可能である。

<img src="https://user-images.githubusercontent.com/106908/27043567-850d4740-4fd5-11e7-8831-4308e9a287d5.png" width="60%" />


<img src="https://user-images.githubusercontent.com/106908/27043572-8a91ca06-4fd5-11e7-98d7-428e21f641b8.png" width="60%" />

解析したいタイミングでWeb UIからHeap Dumpをダウンロードできるのはとても便利。

### まとめ

[Pivotal Web Services](https://run.pivotal.io)及び[Pivotal Cloud Foundry 1.11](https://docs.pivotal.io/pivotalcf/1-11/installing/pcf-docs.html)のSpring Boot Actuator連携を紹介した。
今後もActuator連携は強化されていくはず。個人的にはメトリクス連携が早く欲しい。

Actuator連携以外にも冒頭で示したようにCloud FoundryとSpring Bootの連携は多くあり、このブログで他の機能も紹介していく予定である。

> **追記**
> 
> ↓の動画に言いたいことが全て詰まっていました。
> 
> <iframe width="560" height="315" src="https://www.youtube.com/embed/9rPjLaOkEUo" frameborder="0" allowfullscreen></iframe>

Cloud FoundryはSpring Bootのベストなプラットフォームなので、Spring Bootでアプリを書いたらまずは[Pivotal Web Services](https://run.pivotal.io)にデプロイしてみてほしい。
