此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 Spring Modulith 2.0.4spring-doc.cadn.net.cn

生产就绪功能

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

Spring Modulith 支持将您系统的架构信息作为 Spring Boot Actuator 端点进行暴露,并通过捕获指标和追踪来观察应用模块之间的交互。 由于生产就绪的应用程序很可能同时需要这两项功能,因此激活这些特性最便捷的方式是使用 Spring Modulith Insight Starter,具体如下:spring-doc.cadn.net.cn

使用 Spring Modulith Insight Starters
<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-starter-insight</artifactId>
  <version>2.0.5-SNAPSHOT</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-starter-insight:2.0.5-SNAPSHOT'
}

这将包含执行器(Actuator)和可观测性支持,以及 Spring Boot 的执行器启动功能,以提供对执行器的通用支持。 请注意,您仍需添加额外的依赖项,以便将应用程序连接到监控工具,例如 ZipkinWavefront 等,通常通过 OpenTelemetryBrave 实现。 有关更多信息,请参阅 Spring Boot 参考文档中的相应章节spring-doc.cadn.net.cn

应用模块执行器

应用程序模块结构可以作为 Spring Boot Actuator 暴露出来。 要启用 Actuator,请将 spring-modulith-actuator 依赖项添加到项目中:spring-doc.cadn.net.cn

使用 Spring Modulith 的 Actuator 支持
<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-actuator</artifactId>
  <version>2.0.5-SNAPSHOT</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:2.0.5-SNAPSHOT'
}

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

运行应用程序现在将暴露一个 modulith 执行器资源:spring-doc.cadn.net.cn

访问执行器 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 个执行器资源。

modulith 资源遵循以下结构:spring-doc.cadn.net.cn

表 1. 应用程序模块执行器的 JSON 结构
JSONPath 描述

$.{moduleName}spring-doc.cadn.net.cn

应用程序模块的技术名称。是 dependencies.target 中模块引用的目标。spring-doc.cadn.net.cn

$.{moduleName}.displayNamespring-doc.cadn.net.cn

应用程序模块的可读名称。spring-doc.cadn.net.cn

$.{moduleName}.basePackagespring-doc.cadn.net.cn

应用程序模块的基础包。spring-doc.cadn.net.cn

$.{moduleName}.parentspring-doc.cadn.net.cn

(可选)父模块的名称。详见 xref:fundamentals.adoc#modules.nested。spring-doc.cadn.net.cn

$.{moduleName}.nestedspring-doc.cadn.net.cn

嵌套模块的名称(如果有)。详见 xref:fundamentals.adoc#modules.nested。spring-doc.cadn.net.cn

$.{moduleName}.dependencies[]spring-doc.cadn.net.cn

应用程序模块的所有传出依赖项spring-doc.cadn.net.cn

$.{moduleName}.dependencies[].targetspring-doc.cadn.net.cn

所依赖的应用程序模块的名称。对 {moduleName} 的引用。spring-doc.cadn.net.cn

$.{moduleName}.dependencies[].types[]spring-doc.cadn.net.cn

目标模块的依赖类型。可以是 DEFAULT(简单类型依赖)、USES_COMPONENT(Spring Bean 依赖)或 EVENT_LISTENERspring-doc.cadn.net.cn

一个示例模块排列如下所示:spring-doc.cadn.net.cn

应用程序模块执行器的示例响应
{
  "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-doc.cadn.net.cn

使用 Spring Modulith 的可观测性支持
<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-observability</artifactId>
  <version>2.0.5-SNAPSHOT</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-observability:2.0.5-SNAPSHOT'
}
您需要根据想要用于传输可观测性元数据的工具,配置额外的基础设施依赖项。 有关详细信息,请查阅相应的 Spring Boot 文档,以了解您的设置需要包含哪些依赖项。

这将导致属于应用模块 API 的所有 Spring 组件被一个切面装饰,该切面会拦截调用并为它们创建 Micrometer 跨度。 下方是一个调用追踪示例:spring-doc.cadn.net.cn

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

在这种特定情况下,触发支付会改变订单的状态,进而引发订单完成事件。 该事件会被引擎异步捕获,引擎随后触发订单的另一次状态变更,运行几秒钟后,再依次触发订单的最终状态变更。spring-doc.cadn.net.cn