Upgrade to Pro — share decks privately, control downloads, hide ads and more …

開発者のためのSpring Boot Actuator入門 / jsug-2019-08

Ryo Shindo
August 28, 2019

開発者のためのSpring Boot Actuator入門 / jsug-2019-08

JSUG勉強会 2019その8 Spring for Beginner
https://jsug.doorkeeper.jp/events/95750

Ryo Shindo

August 28, 2019
Tweet

More Decks by Ryo Shindo

Other Decks in Programming

Transcript

  1. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 1
    開発者のための
    Spring Boot Actuator入門
    JSUG勉強会 2019その8
    Acroquest Technology株式会社
    進藤 遼

    View Slide

  2. 自己紹介
    • 進藤 遼
    • Acroquest Technology株式会社
    • 日本Springユーザ会 スタッフ
    • Twitter: @shindo_ryo
    • Spring歴は約4年
    • 最近はエンプラ系システムでアーキテクチャ設計やったり
    Goで分散トレーシングやったり。
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 2

    View Slide

  3. Acroquestのミッション・ビジョン
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 3
    テクノロジストチームとして
    ビジネスの革新的価値創出に挑戦する
    ビジョン
    Acroquestの創り出す技術で
    地球を感動で進化させる
    ミッション

    View Slide

  4. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 4
    Spring Boot 枠のセッションなので
    Spring Boot の話をします

    View Slide

  5. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 5
    https://speakerdeck.com/masatoshitada/spring-for-spring-boot-number-jsug

    View Slide

  6. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 6
    以上!!

    View Slide

  7. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 7
    (ここから真面目に話す)

    View Slide

  8. アジェンダ
    1. Spring Bootとは
    2. Spring Boot Actuatorとは
    3. Actuatorに触ってみよう
    4. 開発に役立つ使い方
    ➢ env – アプリの設定の確認
    ➢ mappings – ControllerとURLのマッピングの確認
    ➢ loggers – ログレベルの確認/変更
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 8

    View Slide

  9. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 9
    1. Spring Boot とは

    View Slide

  10. Spring Bootとは・・・
    • 重厚長大化したSpringプロジェクト群を使った
    開発を簡単にはじめられる仕組み
    ✓ ただしシンプルであるとは言っていない (Simple is not Easy.)
    • AutoConfigurationの仕組みによって、Spring
    アプリケーションに必要な設定・Bean定義等を
    大幅に省略することができる
    ✓ dependencyに追加するだけでデフォルト設定が効く
    ✓ 設定を変えたければ、プロパティを指定するか、自分で
    Bean定義を部分的に書く
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 10

    View Slide

  11. 本質ではないポイント
    • Uber jar (組み込みTomcat)
    ✓ Bootでもwarは作れる
    ✓ とはいえ、最近のWAFはほとんどExecutable Jarをつくる
    • ymlファイルで設定を書く
    ✓ .propertiesでも.ymlでも設定する内容は同じ
    ✓ xmlはほとんど書く必要はないが、必要であれば書くという
    程度
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 11

    View Slide

  12. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 12
    2. Spring Boot Actuatorとは

    View Slide

  13. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 13
    Actuator = 駆動装置?

    View Slide

  14. 2. Spring Boot Actuator とは
    • 実行中のSpring Boot アプリケーションの
    情報を取得するための機能
    • アプリケーションに組み込むことで、アプリの設定や
    メトリクスなどをHTTP/JMXで取得できるようになる
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 14
    Spring Boot Application
    Spring Boot Actuator
    Env Metrics Health Beans
    Thread
    dump
    HTTP

    JMX

    View Slide

  15. 2. Spring Boot Actuator とは
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 15
    $ curl -XGET localhost:8080/actuator/health
    {
    "status": "UP"
    }
    共通のプレフィックス(/actuator)
    の後ろにエンドポイント名をつける
    JSON形式

    View Slide

  16. 2. Spring Boot Actuator とは
    ◼ エンドポイント一覧 (2.0~)
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 16
    auditevents
    beans
    caches
    conditions
    configprops
    env
    flyway
    health
    httptrace
    info
    integrationgraph
    loggers
    liquibase
    metrics
    mappings
    scheduledtasks
    sessions
    shutdown
    threaddump
    heapdump
    jolokia
    logfile
    prometheus

    View Slide

  17. 2. Spring Boot Actuator とは
    ◼ エンドポイント一覧 (2.0~)
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 17
    auditevents
    beans
    caches
    conditions
    configprops
    env
    flyway
    health
    httptrace
    info
    integrationgraph
    loggers
    liquibase
    metrics
    mappings
    scheduledtasks
    sessions
    shutdown
    threaddump
    heapdump
    jolokia
    logfile
    prometheus
    ヘルスチェック
    スレッドダンプ
    メトリクス
    (JVM情報など)

    View Slide

  18. 2. Spring Boot Actuator とは
    • メトリクス収集ライブラリのMicrometer
    との統合も可能
    ➢ Boot 2.0からはspring-boot-starter-actuatorに
    micrometerが含まれている
    • ただし今回は開発時に役立つActuatorの使い方を
    お話しします。
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 18

    View Slide

  19. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 19
    3. Actuator に触ってみよう

    View Slide

  20. 3. Actuatorに触ってみよう
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 20

    org.springframework.boot
    spring-boot-starter-actuator

    pom.xml
    AutoConfiguration で
    エンドポイントが登録される

    View Slide

  21. 3. Actuatorに触ってみよう
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 21
    management:
    endpoints:
    web:
    base-path: /manage
    exposure:
    include: '*'
    exclude: flyway
    endpoint:
    shutdown:
    enabled: true
    server:
    port: 8082
    application.yml
    Actuatorのエンドポイント共通
    のプレフィックスを指定する
    (デフォルトは /actuator)

    View Slide

  22. 3. Actuatorに触ってみよう
    Copyright © Acroquest Technology Co., Ltd. All rights reserved.
    management:
    endpoints:
    web:
    base-path: /manage
    exposure:
    include: '*'
    exclude: flyway
    endpoint:
    shutdown:
    enabled: true
    server:
    port: 8082
    22
    application.yml
    公開するエンドポイントを指定
    する。
    デフォルトではhealthとinfo
    のみが公開される。

    View Slide

  23. 3. Actuatorに触ってみよう
    Copyright © Acroquest Technology Co., Ltd. All rights reserved.
    management:
    endpoints:
    web:
    base-path: /manage
    exposure:
    include: '*'
    exclude: flyway
    endpoint:
    shutdown:
    enabled: true
    server:
    port: 8082
    23
    application.yml
    shutdownの公開は別設定

    View Slide

  24. 3. Actuatorに触ってみよう
    Copyright © Acroquest Technology Co., Ltd. All rights reserved.
    management:
    endpoints:
    web:
    base-path: /manage
    exposure:
    include: '*'
    exclude: flyway
    endpoint:
    shutdown:
    enabled: true
    server:
    port: 8082
    24
    application.yml
    Actuator のみポートを分ける
    ことも可能

    View Slide

  25. 3. Actuatorに触ってみよう
    ◼ 注意事項
    ✓ 本番環境にActuatorを適用する場合、
    アクセス制御は必ずつけること
    ➢ アプリ本体と同様にSpring Securityで
    Actuatorエンドポイントを保護してください
    ✓ 実際に利用するエンドポイントのみ公開すること
    ➢ プロファイルで環境ごとに公開範囲を分ける
    ➢ Dev環境以外は思い切ってOFFにするのもあり
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 25

    View Slide

  26. Copyright © Acroquest Technology Co., Ltd. All rights reserved. 26
    4. 開発に役立つ使い方

    View Slide

  27. 4. 開発に役立つ使い方
    Spring Bootで開発していると、こんな悩みありますよね?
    実際に読み込まれたプロパティが分からない
    Controllerのどのメソッドがどのエンドポイント
    に対応しているか分からない
    ログレベルを変えるのにいちいち再起動
    するのが面倒だ
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 27

    View Slide

  28. 4. 開発に役立つ使い方
    それ、Actuatorで楽になれるよ
    実際に読み込まれたプロパティが分からない
    ⇒ env
    Controllerのどのメソッドがどのエンドポイント
    に対応しているか分からない
    ⇒ mappings
    ログレベルを変えるのにいちいち再起動
    するのが面倒だ
    ⇒ loggers
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 28

    View Slide

  29. 4. 開発に役立つ使い方
    • お悩み①
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 29
    実際に読み込まれたプロパティ/
    環境変数が分からない!
    デバッガで確認するのが大変!

    View Slide

  30. 4. 開発に役立つ使い方
    ◼ env エンドポイント
    • アプリが読み込んだ環境変数やプロパティを一覧にして
    取得できる
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 30
    {
    "activeProfiles": [
    "mysql"
    ],
    "propertySources": [
    {
    "name": "applicationConfig: [classpath:/application.properties]",
    "properties": {
    "management.endpoints.web.base-path": {
    "value": "/manage",
    "origin": "class path resource [application.properties]:18:36"
    }
    }

    View Slide

  31. 4. 開発に役立つ使い方
    • お悩み②
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 31
    どんなエンドポイントがあるのか
    分からない!
    Controllerとの対応が
    全然見えない!

    View Slide

  32. 4. 開発に役立つ使い方
    ◼ mapping エンドポイント
    • エンドポイントとControllerのメソッドとの対応関係を
    一覧で取得できる
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 32
    "dispatcherServlet": [
    {
    “handler”: “public StringOwnerController.processCreationForm(Owner,BindingResult)",
    "predicate": "{POST /owners/new}",
    "details": {
    "handlerMethod": {
    "className": "org.springframework.samples.petclinic.owner.OwnerController",
    "name": "processCreationForm",
    },
    "requestMappingConditions": {
    "methods": ["POST"],
    "params": [],
    "patterns": ["/owners/new"],
    "produces": []
    }

    View Slide

  33. 4. 開発に役立つ使い方
    • お悩み③
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 33
    ログレベルの設定が複雑で
    最終的な結果が分からない・・・
    ログレベルを変えるためだけに
    アプリを再起動するのが
    面倒・・・

    View Slide

  34. 4. 開発に役立つ使い方
    ◼ loggers エンドポイント
    • アプリケーションを動かしながら、ログレベルの
    取得と変更ができる
    ➢ /loggersでログレベルの一覧取得、/loggers/{ロガー名}で
    特定のパッケージ・クラスに対するログレベルを取得
    ➢ GETで取得、POSTで変更
    ➢ ログレベルの変更は /loggers/{ロガー名} に対してのみ可能
    (ログレベルをアプリ全体で変更する場合はROOTロガーの
    ログレベルを変更する)
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 34

    View Slide

  35. 4. 開発に役立つ使い方
    ◼ ログレベルの取得
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 35
    $ curl -XGET localhost:8080/actuator/loggers/org.springframework
    {
    "configuredLevel": null,
    "effectiveLevel": "INFO"
    }
    実際に適用されているログレベルは
    effectiveLevel

    View Slide

  36. 4. 開発に役立つ使い方
    ◼ ログレベルの変更
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 36
    # ログレベルをDEBUGに変更
    $ curl -XPOST localhost:8080/actuator/loggers/org.springframework
    -H ‘Content-Type: application/json’ –d ‘{“configuredLevel”: “DEBUG”}’
    $ curl -XGET localhost:8080/actuator/loggers/org.springframework
    {"configuredLevel":"DEBUG","effectiveLevel":"DEBUG"}
    ConfiguredLevelのみ
    リクエストに入れる

    View Slide

  37. 4. 開発に役立つ使い方
    • (余談)curl が面倒であればSpring Boot Adminを
    使うのもありかも・・・
    (時間があればデモします)
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 37

    View Slide

  38. まとめ
    ✓ Spring Boot Actuatorでアプリの情報を
    簡単に取得できる!
    ✓ dependencyを追加するだけで適用できる!
    ✓ 運用だけじゃなくて開発にも役立つよ!
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 38

    View Slide

  39. Enjoy your Bootiful Life!!
    Evolve the Earth with Emotion of Technology
    Copyright © Acroquest Technology Co., Ltd. All rights reserved. 39

    View Slide