生產就緒功能

Spring Modulith 提供了支援,可將關於您系統的架構資訊公開為 Spring Boot Actuator 端點,並透過擷取指標和追蹤來觀察應用程式模組之間的互動。由於生產就緒的應用程式可能同時需要這兩者,因此啟用這些功能最方便的方法是使用 Spring Modulith Insight 啟動器,如下所示

使用 Spring Modulith Insight 啟動器
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-starter-insight</artifactId>
  <version>1.2.5</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-starter-insight:1.2.5'
}

這將包含 Actuator 和可觀察性支援,以及 Spring Boot 的 Actuator 啟動,以提供 Actuator 的一般支援。請注意,您仍然需要新增其他相依性,才能將您的應用程式連接到您的監控工具,例如 ZipkinWavefront 等,通常透過 OpenTelemetryBrave。如需更多資訊,請參閱 Spring Boot 參考文件中的相應章節

應用程式模組 Actuator

應用程式模組結構可以公開為 Spring Boot Actuator。若要啟用 Actuator,請將 spring-modulith-actuator 相依性新增至專案

使用 Spring Modulith Actuator 支援
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-actuator</artifactId>
  <version>1.2.5</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.5'
}

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

執行應用程式現在將公開一個 modulith Actuator 資源

存取 Actuator HTTP 資源
GET https://127.0.0.1:8080/actuator

{
  "_links": {
    "self": {
      "href": "https://127.0.0.1:8080/actuator",
      "templated": false
    },
    "health-path": {
      "href": "https://127.0.0.1:8080/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "https://127.0.0.1:8080/actuator/health",
      "templated": false
    },
    "modulith": { (1)
      "href": "https://127.0.0.1:8080/actuator/modulith",
      "templated": false
    }
  }
}
1 已發佈的 modulith Actuator 資源。

modulith 資源遵循以下結構

表 1. 應用程式模組 Actuator 的 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

模組安排範例會如下所示

應用程式模組 Actuator 的範例回應
{
  "a": {
    "basePackage": "example.a",
    "displayName": "A",
    "dependencies": []
  },
  "b": {
    "basePackage": "example.b",
    "displayName": "B",
    "dependencies": [ {
      "target": "a",
      "types": [ "EVENT_LISTENER", "USES_COMPONENT" ]
    } ]
  }
}

觀察應用程式模組

可以攔截應用程式模組之間的互動,以建立 Micrometer span,最終進入您可以在 Zipkin 等工具中視覺化的追蹤。若要啟用檢測,請將以下執行時期相依性新增至您的專案

使用 Spring Modulith 可觀察性支援
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-observability</artifactId>
  <version>1.2.5</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-observability:1.2.5'
}
您必須根據您要將可觀察性中繼資料管道傳輸到的工具,設定額外的基礎架構相依性。如需詳細資訊,請查看相應的 Spring Boot 文件,以了解您的設定應包含哪些相依性。

這將導致應用程式模組 API 的所有 Spring 元件都以 Aspect 裝飾,Aspect 將攔截調用並為其建立 Micrometer span。範例調用追蹤如下所示

observability
圖 1. 範例模組調用追蹤

在此特定案例中,觸發付款會變更訂單的狀態,然後導致觸發訂單完成事件。引擎會非同步地接收此事件,引擎會觸發訂單上的另一個狀態變更,運作幾秒鐘,然後依序觸發訂單上的最終狀態變更。