生产就绪特性

如果您正在对此处描述的 应用模块检测 应用自定义配置,则需要将这些配置移动到您的生产源代码中(如果尚未存在),以确保此处描述的特性能够考虑这些配置。

Spring Modulith 支持将系统的架构信息作为 Spring Boot actuator 端点公开,并通过捕获指标和跟踪来观察应用模块之间的交互。由于生产就绪应用很可能需要这两者,因此激活这些功能最便捷的方法是使用 Spring Modulith Insight starter,如下所示

使用 Spring Modulith Insight starter
  • Maven

  • Gradle

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

这将包含 actuator 和可观测性支持,以及 Spring Boot 用于 actuator 通用支持的启动器。请注意,您仍然需要添加额外的依赖项,以便将您的应用连接到 Zipkin、Wavefront 等监控工具,通常通过 OpenTelemetry 或 Brave 进行。有关更多信息,请查阅 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.3.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.3.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 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
    }
  }
}
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 spans,最终形成可以在 Zipkin 等工具中可视化的跟踪。要激活Instrumentation(插装),请将以下运行时依赖项添加到您的项目

使用 Spring Modulith 可观测性支持
  • Maven

  • Gradle

<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-observability</artifactId>
  <version>1.3.5</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-observability:1.3.5'
}
您需要根据您希望将可观测性元数据导入的工具配置额外的基础设施依赖项。详情请查阅 Spring Boot 文档中关于您的设置需要包含哪些依赖项的 相应章节

这将导致所有属于应用模块 API 的 Spring 组件都被一个 Aspect 修饰,该 Aspect 将拦截方法调用并为其创建 Micrometer spans。示例调用跟踪如下图所示

observability
图 1. 示例模块调用跟踪

在这个特定的案例中,触发支付改变了订单的状态,这随后触发了一个订单完成事件。该事件被引擎异步接收,该引擎触发订单的另一次状态变更,工作几秒钟后依次触发订单的最终状态变更。