|
此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 Spring Modulith 2.0.4! |
生产就绪功能
| 如果您正在对此处描述的应用模块检测应用自定义配置,则需要将这些配置移至您的生产源代码中(如果尚未存在),以确保此处描述的功能能够考虑到这些配置。 |
Spring Modulith 支持将您系统的架构信息作为 Spring Boot Actuator 端点进行暴露,并通过捕获指标和追踪来观察应用模块之间的交互。 由于生产就绪的应用程序很可能同时需要这两项功能,因此激活这些特性最便捷的方式是使用 Spring Modulith Insight Starter,具体如下:
-
Maven
-
Gradle
<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 的执行器启动功能,以提供对执行器的通用支持。 请注意,您仍需添加额外的依赖项,以便将应用程序连接到监控工具,例如 Zipkin、Wavefront 等,通常通过 OpenTelemetry 或 Brave 实现。 有关更多信息,请参阅 Spring Boot 参考文档中的相应章节。
应用模块执行器
应用程序模块结构可以作为 Spring Boot Actuator 暴露出来。
要启用 Actuator,请将 spring-modulith-actuator 依赖项添加到项目中,除非您已按照上文所述声明了 spring-modulith-starter-insight:
-
Maven
-
Gradle
<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 执行器资源:
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 资源遵循以下结构:
| JSONPath | 描述 |
|---|---|
|
应用程序模块的技术名称。是 |
|
应用程序模块的可读名称。 |
|
应用程序模块的基础包。 |
|
(可选)父模块的名称。详见 xref:fundamentals.adoc#modules.nested。 |
|
嵌套模块的名称(如果有)。详见 xref:fundamentals.adoc#modules.nested。 |
|
应用程序模块的所有传出依赖项 |
|
所依赖的应用程序模块的名称。对 |
|
目标模块的依赖类型。可以是 |
一个示例模块排列如下所示:
{
"a": {
"basePackage": "example.a",
"displayName": "A",
"dependencies": []
},
"b": {
"basePackage": "example.b",
"displayName": "B",
"dependencies": [ {
"target": "a",
"types": [ "EVENT_LISTENER", "USES_COMPONENT" ]
} ]
}
}
观察应用程序模块
应用程序模块之间的交互可以被拦截以创建 Micrometer 跨度,最终形成可在 Zipkin 或 Grafana 等工具中可视化的追踪信息。
要激活该埋点功能,请向您的项目添加以下运行时依赖项,除非您已按照上文所述声明了spring-modulith-starter-insight:
-
Maven
-
Gradle
<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 Boot 文档,以了解您的设置需要包含哪些依赖项。 |
这将导致属于应用模块 API 的所有 Spring 组件被一个切面装饰,该切面会拦截调用并为它们创建 Micrometer 跨度。 下方是一个调用追踪示例:
在这种特定情况下,触发支付会改变订单的状态,进而引发订单完成事件。 该事件会被引擎异步捕获,引擎随后触发订单的另一次状态变更,运行几秒钟后,再依次触发订单的最终状态变更。
可观测性指标
Spring Modulith 以两种指标的形式,为应用程序中发布的所有领域事件注册数据。
-
module.events.published– 一个用于汇总所有事件发布的计数器。 -
module.events.published.$moduleIdentifier.$simpleEventTypeName- 用于单个事件的计数器,可进一步用领域特定的值进行丰富。
独立事件指标
姓名 |
描述 |
|
模块的标识符。 |
|
模块名称。 |
可以通过实现 ModulithEventMetricsCustomizer 向单个事件指标添加额外的标签。
@Configuration
class ObservabilityConfiguration {
@Bean
ModulithEventMetricsCustomizer modulithEventMetricsCustomizer() {
return metrics -> {
metrics.customize(MyCustomDomainEvent.class, (event, it) -> {
it.tags("someTag", event.getValue());
});
};
}
}
请注意,我们如何能够按类型引用各个领域事件,并将特定事件的值转换为指标上的值。