本番対応機能

Spring Modulith は、Spring Boot アクチュエーターエンドポイントとしてシステムに関するアーキテクチャ情報を公開するサポートを提供するだけでなく、メトリクスとトレースをキャプチャーすることでアプリケーションモジュール間の相互作用を監視します。本番環境に対応したアプリケーションには両方が必要になる可能性が高いため、これらの機能をアクティブ化する最も便利な方法は、次のように Spring Modulith Insight スターターを使用することです。

Spring Modulith Insight スターターの使用
<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-starter-insight</artifactId>
  <version>1.2.1</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-starter-insight:1.2.1'
}

これには、アクチュエーターと可観測性のサポートに加え、アクチュエーターの一般的なサポートのための Spring Boot のアクチュエーターの起動が含まれます。アプリケーションを Zipkin (英語) Wavefront (英語) などの監視ツールに接続するには、通常 OpenTelemetry (英語) または Brave [GitHub] (英語) を介してさらに依存関係を追加する必要があることに注意してください。詳細については、Spring Boot のリファレンスドキュメントの対応するセクションを参照してください。

アプリケーションモジュールアクチュエーター

アプリケーションモジュール構造は Spring Boot アクチュエーターとして公開できます。アクチュエーターを有効にするには、spring-modulith-actuator 依存関係をプロジェクトに追加します。

Spring Modulith アクチュエーターサポートの使用
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-actuator</artifactId>
  <version>1.2.1</version>
  <scope>runtime</scope>
</dependency>

<!-- Spring Boot actuator starter required to enable actuators in general -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
  <version>…</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-actuator:1.2.1'
}

<!-- Spring Boot actuator starter required to enable actuators in general -->
dependencies {
  runtimeOnly 'org.springframework.boot:spring-boot-starter-actuator'
}

アプリケーションを実行すると、modulith アクチュエーターリソースが公開されます。

アクチュエーターの HTTP リソースへのアクセス
GET http://localhost:8080/actuator

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "modulith": { (1)
      "href": "http://localhost:8080/actuator/modulith",
      "templated": false
    }
  }
}
1modulith アクチュエーターリソースがアドバタイズされます。

modulith リソースは次の構造に従います。

表 1: アプリケーションモジュールアクチュエーターの JSON 構造
JSONPath 説明

$.{moduleName}

アプリケーションモジュールの技術名。dependencies.target のモジュール参照のターゲット。

$.{moduleName}.displayName

人間が判読できるアプリケーションモジュールの名前。

$.{moduleName}.basePackage

アプリケーションモジュールの基本パッケージ。

$.{moduleName}.dependencies[]

アプリケーションモジュールのすべての送信依存関係

$.{moduleName}.dependencies[].target

依存するアプリケーションモジュールの名前。{moduleName} への参照。

$.{moduleName}.dependencies[].types[]

ターゲットモジュールに対する依存関係の型。DEFAULT (単純型依存関係)、USES_COMPONENT (Spring Bean 依存関係)、または EVENT_LISTENER のいずれかになります。

モジュールの配置例は次のようになります。

アプリケーションモジュールアクチュエーターのレスポンス例
{
  "a": {
    "basePackage": "example.a",
    "displayName": "A",
    "dependencies": []
  },
  "b": {
    "basePackage": "example.b",
    "displayName": "B",
    "dependencies": [ {
      "target": "a",
      "types": [ "EVENT_LISTENER", "USES_COMPONENT" ]
    } ]
  }
}

アプリケーションモジュールの観察

アプリケーションモジュール間の対話をインターセプトして Micrometer スパンを作成し、最終的に Zipkin (英語) などのツールで視覚化できるトレースに至ることができます。インストルメンテーションをアクティブにするには、次のランタイム依存関係をプロジェクトに追加します。

Spring Modulith 可観測性サポートの使用
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-observability</artifactId>
  <version>1.2.1</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-observability:1.2.1'
}
可観測性メタデータをパイプするツールに応じて、追加のインフラストラクチャ依存関係を構成する必要があります。詳細については、セットアップにどの依存関係を含めるかについて、対応する Spring Boot ドキュメントを確認してください。

これにより、アプリケーションモジュールの API の一部であるすべての Spring コンポーネントが、呼び出しをインターセプトしてそれらの Micrometer スパンを作成するアスペクトで修飾されます。呼び出しトレースのサンプルを以下に示します。

observability
図 1: サンプルモジュール呼び出しトレース

この特定のケースでは、支払いをトリガーするとオーダーの状態が変更され、オーダー完了イベントがトリガーされます。これは、オーダーの別の状態変更をトリガーするエンジンによって非同期的に取得され、数秒間動作し、順番にオーダーの最終的な状態変更をトリガーします。