此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 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.1.0-M3</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-starter-insight:2.1.0-M3'
}

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

应用模块执行器

应用程序模块结构可以作为 Spring Boot Actuator 暴露出来。 要启用 Actuator,请将 spring-modulith-actuator 依赖项添加到项目中,除非您已按照上文所述声明了 spring-modulith-starter-insightspring-doc.cadn.net.cn

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

<!-- 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 跨度,最终形成可在 ZipkinGrafana 等工具中可视化的追踪信息。 要激活该埋点功能,请向您的项目添加以下运行时依赖项,除非您已按照上文所述声明了spring-modulith-starter-insightspring-doc.cadn.net.cn

使用 Spring Modulith 的可观测性支持
<dependency>
  <groupId>org.springframework.modulith</groupId>
  <artifactId>spring-modulith-observability-core</artifactId>
  <version>2.1.0-M3</version>
  <scope>runtime</scope>
</dependency>
dependencies {
  runtimeOnly 'org.springframework.modulith:spring-modulith-observability-core:2.1.0-M3'
}

如果您希望自定义为应用程序事件发布创建的指标,您还需要在编译作用域中声明 spring-modulith-observability-api 构件。spring-doc.cadn.net.cn

您需要根据想要用于传输可观测性元数据的工具,配置额外的基础设施依赖项。 有关详细信息,请查阅相应的 Spring Boot 文档,以了解您的设置需要包含哪些依赖项。

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

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

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

可观测性跨度

模块入口跨度

表 2. 标签键

姓名spring-doc.cadn.net.cn

描述spring-doc.cadn.net.cn

module.identifierspring-doc.cadn.net.cn

模块的标识符。spring-doc.cadn.net.cn

module.invocation-typespring-doc.cadn.net.cn

调用类型(“事件监听器”或spring-doc.cadn.net.cn

module.methodspring-doc.cadn.net.cn

在模块上执行的方法。spring-doc.cadn.net.cn

module.namespring-doc.cadn.net.cn

模块名称。spring-doc.cadn.net.cn

可观测性指标

Spring Modulith 以两种指标的形式,为应用程序中发布的所有领域事件注册数据。spring-doc.cadn.net.cn

  • module.events.published – 一个用于汇总所有事件发布的计数器。spring-doc.cadn.net.cn

  • module.events.published.$moduleIdentifier.$simpleEventTypeName - 用于单个事件的计数器,可进一步用领域特定的值进行丰富。spring-doc.cadn.net.cn

所有事件指标

表3. 低基数键

module.event.typespring-doc.cadn.net.cn

触发事件的类型。spring-doc.cadn.net.cn

module.identifierspring-doc.cadn.net.cn

模块的标识符。spring-doc.cadn.net.cn

模块名称。spring-doc.cadn.net.cn

独立事件指标

表4. 低基数键

module.identifierspring-doc.cadn.net.cn

模块的标识符。spring-doc.cadn.net.cn

模块名称。spring-doc.cadn.net.cn

可以通过实现 ModulithEventMetricsCustomizer 向单个事件指标添加额外的标签。spring-doc.cadn.net.cn

@Configuration
class ObservabilityConfiguration {

  @Bean
  ModulithEventMetricsCustomizer modulithEventMetricsCustomizer() {

    return metrics -> {

      metrics.customize(MyCustomDomainEvent.class, (event, it) -> {
        it.tags("someTag", event.getValue());
      });
    };
  }
}

请注意,我们如何能够按类型引用各个领域事件,并将特定事件的值转换为指标上的值。spring-doc.cadn.net.cn

约定

Spring Modulith 创建的指标可以通过在应用程序中提供一个 ModulithObservationConvention 作为 Bean 来自定义。spring-doc.cadn.net.cn

@Configuration
class ObservabilityConfiguration {

  @Bean
  ModulithObservationConvention observationConvention() {
    return new CustomModulithObservationConvention();
  }
}

我们的默认实现将所有调用委托给 ModulithContext 上的方法。spring-doc.cadn.net.cn