LoginSignup
1
0

More than 5 years have passed since last update.

Chaos Monkey for Spring Bootの使い方

Posted at

概要

Spring BootをNetflixのChaos Monkeyのようにランダムでエラーを発生させるChaos Monkey for Spring Bootというものがあったので試してみた。

ドキュメント

説明動画

公式ドキュメント中に存在します。

github

説明

Chaos Monkey for Spring Bootは、以下の3つのエラーをSpring Bootに対してランダムで発生させることができる

  • 速度遅延
  • 例外発生
  • Spring Bootの停止

その他の主な機能は以下のとおり

  • 障害を発生させる頻度(10%単位)を指定できる
  • 速度遅延時間の範囲(単位ms)を指定できる
  • 障害を発生させるBeanの種類(@Controller, @Service, @Componentなど)を指定できる
  • Chaos Monkeyの有効/無効や設定は、actuator経由で変更できる

組み込み方法

Chaos Monkey for Spring Bootは、以下の3つのステップで導入ができる

  • pomに依存追加
  • active profilesの指定
  • applicationプロパティーの設定追加

pomに依存追加

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>chaos-monkey-spring-boot</artifactId>
    <version>2.0.0</version>
</dependency>

SNAPSHOTをご利用の場合は、ドキュメントのSnapshotsを参照

active profilesの指定

active profilesにchaos-monkeyを指定する。

application.propertiesで指定する場合:

spring.profiles.active=chaos-monkey

Spring Bootの起動時に指定する場合:

java -jar your-app.jar --spring.profiles.active=chaos-monkey

applicationプロパティーの設定追加

application.propertiesにchaos.monkey.enabledを設定(actuator経由で変更可能)

application.propertiesの場合:

chaos.monkey.enabled=true

application.xmlの場合:

chaos:
  monkey:
    enabled: true

actuator経由で設定する場合:

actuatorから利用する場合は、下の章の「actuatorを利用した設定確認、設定変更」を参照

以上、3つの設定(pom / active profiles / application.properties)でChaos Monkeyが有効になります。

起動確認

Spring Bootを起動すると起動ログに以下のAAが表示されます。

     _____ _                       __  __             _
    / ____| |                     |  \/  |           | |
   | |    | |__   __ _  ___  ___  | \  / | ___  _ __ | | _____ _   _
   | |    | '_ \ / _` |/ _ \/ __| | |\/| |/ _ \| '_ \| |/ / _ | | | |
   | |____| | | | (_| | (_) \__ \ | |  | | (_) | | | |   |  __| |_| |
    \_____|_| |_|\__,_|\___/|___/ |_|  |_|\___/|_| |_|_|\_\___|\__, |
                                                                __/ |
    _ready to do evil!                                         |___/

:: Chaos Monkey for Spring Boot                                    ::

Tips

障害の発生頻度の変更

障害発生頻度は、levelというプロパティーで設定できる。障害の発生率は10%単位で、1-10の数字で指定する。

  • 1は発生率10%、10は発生率100%

発生する障害の種類を選択

種類 内容 プロパティ
遅延 指定した時間(ms)の範囲でレイテンシを悪化させます latencyActive
例外 Exceptionを発生させます exceptionsActive
アプリケーションダウン Spring Bootアプリケーションを停止します killApplicationActive
  • 遅延の障害は、遅延範囲開始(latencyRangeStart)と遅延範囲停止(latencyRangeEnd)に指定したミリ秒の数値の範囲でランダムで遅延が発生します。中身はThread.sleepです。
  • 例外の障害は、RuntimeException("Chaos Monkey - RuntimeException")を発生させます。
  • アプリケーションダウンは、Spring Boot ApplicationをexitCode0で終了させます。

障害を発生させる場所

障害を発生させる場所は、watcherPropertiesで指定ができます。それぞれ以下のアノテーションが付いたクラスをスキャンして障害を発生させる対象を選択しています。

アノテーション Right align
@Controller controller
@RestController restController
@Service service
@Repository repository
@Component component

※ watcherの設定がすべてfalseの場合は、Chaos Monkeyが動作しないので注意してください。

actuatorを利用した設定確認、設定変更

actuatorの機能を利用可能にする

以下の設定を追加することで、actuator経由でChaos Monkeyの設定を変更することができる。

application.propertiesの場合:

management.endpoint.chaosmonkey.enabled=true
management.endpoints.web.exposure.include=chaosmonkey

application.xmlの場合:

management:
  endpoint:
    chaosmonkey:
      enabled: true
  endpoints:
    web:
      exposure:
        include: chaosmonkey

Chaos Monkeyの設定を確認する

コマンド
curl http://localhost:8080/actuator/chaosmonkey
結果
{
  "chaosMonkeyProperties": {
    "enabled": false
  },
  "assaultProperties": {
    "level": 5,
    "latencyRangeStart": 1000,
    "latencyRangeEnd": 3000,
    "latencyActive": true,
    "exceptionsActive": false,
    "killApplicationActive": false
  },
  "watcherProperties": {
    "controller": false,
    "restController": false,
    "service": true,
    "repository": false,
    "component": true
  }
}

Chaos Monkeyを有効にする

以下のようにchaosMonkeyProperties.enabledfalseになっている場合は、Chaos Monkeyは無効になっている。これをactuator経由でtrueに変更する。

{
  "chaosMonkeyProperties": {
    "enabled": false
  },
  ...
}
コマンド
curl -X POST -H "Content-type: application/json" http://localhost:8080/actuator/chaosmonkey/enable

※ POSTになっていることに注意
Content-typeapplication/jsonになっていることに注意

結果
Chaos Monkey is enabled

その他の項目(Assault)を変更する

他の項目もactuator経由で変更可能です。以下はAssaultを変更する例です。

コマンド
curl -X POST -H "Content-type: application/json" http://localhost:8080/actuator/chaosmonkey/assaults -d '{
    "level": 5,
    "latencyRangeStart": 1000,
    "latencyRangeEnd": 3000,
    "latencyActive": true,
    "exceptionsActive": false,
    "killApplicationActive": false
  }'

結果

Assault config has changed

まとめ

以上がChaos Monkey for Spring Bootの簡単な利用方法になります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0