|
对于最新稳定版本,请使用 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>1.3.12</version>
<scope>runtime</scope>
</dependency>
dependencies {
runtimeOnly 'org.springframework.modulith:spring-modulith-starter-insight:1.3.12'
}
这将包含执行器(Actuator)和可观测性支持,以及 Spring Boot 的执行器启动功能,以提供对执行器的通用支持。 请注意,您仍需添加额外的依赖项,以便将应用程序连接到监控工具,例如 Zipkin、Wavefront 等,通常通过 OpenTelemetry 或 Brave 实现。 有关更多信息,请参阅 Spring Boot 参考文档中的相应章节。
应用模块执行器
应用程序模块结构可以作为 Spring Boot Actuator 暴露出来。
要启用 Actuator,请将 spring-modulith-actuator 依赖项添加到项目中:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-actuator</artifactId>
<version>1.3.12</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.12'
}
<!-- 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 | 描述 |
|---|---|
|
应用程序模块的技术名称。是 |
|
应用程序模块的可读名称。 |
|
应用程序模块的基础包。 |
|
应用程序模块的所有传出依赖项 |
|
所依赖的应用程序模块的名称。对 |
|
目标模块的依赖类型。可以是 |
一个示例模块排列如下所示:
{
"a": {
"basePackage": "example.a",
"displayName": "A",
"dependencies": []
},
"b": {
"basePackage": "example.b",
"displayName": "B",
"dependencies": [ {
"target": "a",
"types": [ "EVENT_LISTENER", "USES_COMPONENT" ]
} ]
}
}
观察应用程序模块
应用程序模块之间的交互可以被拦截以创建 Micrometer span,最终形成可在 Zipkin 等工具中可视化的追踪信息。 要激活该功能,请在您的项目中添加以下运行时依赖:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-observability</artifactId>
<version>1.3.12</version>
<scope>runtime</scope>
</dependency>
dependencies {
runtimeOnly 'org.springframework.modulith:spring-modulith-observability:1.3.12'
}
| 您需要根据想要用于传输可观测性元数据的工具,配置额外的基础设施依赖项。 有关详细信息,请查阅相应的 Spring Boot 文档,以了解您的设置需要包含哪些依赖项。 |
这将导致属于应用模块 API 的所有 Spring 组件被一个切面装饰,该切面会拦截调用并为它们创建 Micrometer 跨度。 下方是一个调用追踪示例:
在这种特定情况下,触发支付会改变订单的状态,进而引发订单完成事件。 该事件会被引擎异步捕获,引擎随后触发订单的另一次状态变更,运行几秒钟后,再依次触发订单的最终状态变更。