微信登录

监控与管理 - Actuator - 监控应用运行状态

Java - Web - Spring 《监控与管理 - Actuator - 监控应用运行状态》

一、引言

在开发和部署 Java Web 应用程序时,了解应用程序的运行状态至关重要。Spring Boot Actuator 为我们提供了强大的监控和管理功能,它能帮助开发者在生产环境中轻松监控应用程序的健康状况、性能指标等。本文将详细介绍 Spring Boot Actuator 的使用,包括如何集成、常用端点以及自定义端点等内容,并通过示例代码进行演示。

二、集成 Spring Boot Actuator

2.1 添加依赖

首先,我们需要在 Spring Boot 项目中添加 Actuator 的依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

如果你使用的是 Gradle,可以在 build.gradle 中添加以下依赖:

  1. implementation 'org.springframework.boot:spring-boot-starter-actuator'

2.2 配置 Actuator

application.propertiesapplication.yml 中,我们可以对 Actuator 进行一些配置。例如,开启所有端点:

  1. management.endpoints.web.exposure.include=*

或者在 application.yml 中配置:

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: '*'

三、常用端点介绍

Spring Boot Actuator 提供了多个端点,用于监控和管理应用程序。以下是一些常用的端点:

端点路径 描述
/actuator/health 检查应用程序的健康状态,如数据库连接、磁盘空间等。
/actuator/info 显示应用程序的自定义信息。
/actuator/metrics 显示应用程序的各种指标,如内存使用、CPU 使用率等。
/actuator/env 显示当前应用程序的环境信息,包括系统属性、环境变量等。
/actuator/loggers 允许查看和修改应用程序的日志级别。

3.1 /actuator/health 端点示例

启动应用程序后,访问 http://localhost:8080/actuator/health,你将看到类似以下的响应:

  1. {
  2. "status": "UP"
  3. }

这表示应用程序的健康状态为正常。如果应用程序存在问题,如数据库连接失败,状态可能会显示为 DOWN,并提供详细的错误信息。

3.2 /actuator/info 端点示例

我们可以在 application.propertiesapplication.yml 中添加自定义信息,例如:

  1. info.app.name=My Spring Boot App
  2. info.app.version=1.0.0

访问 http://localhost:8080/actuator/info,你将看到以下响应:

  1. {
  2. "app": {
  3. "name": "My Spring Boot App",
  4. "version": "1.0.0"
  5. }
  6. }

3.3 /actuator/metrics 端点示例

访问 http://localhost:8080/actuator/metrics,你将看到应用程序的各种指标列表。例如,要查看 JVM 堆内存使用情况,可以访问 http://localhost:8080/actuator/metrics/jvm.memory.used,响应如下:

  1. {
  2. "name": "jvm.memory.used",
  3. "description": "The amount of used memory",
  4. "baseUnit": "bytes",
  5. "measurements": [
  6. {
  7. "statistic": "VALUE",
  8. "value": 12345678
  9. }
  10. ],
  11. "availableTags": [
  12. {
  13. "tag": "area",
  14. "values": [
  15. "heap",
  16. "nonheap"
  17. ]
  18. },
  19. {
  20. "tag": "id",
  21. "values": [
  22. "Metaspace",
  23. "Eden Space",
  24. "Survivor Space",
  25. "Tenured Gen"
  26. ]
  27. }
  28. ]
  29. }

四、自定义端点

除了使用 Actuator 提供的默认端点,我们还可以自定义端点。以下是一个简单的自定义端点示例:

  1. import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
  2. import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
  3. import org.springframework.stereotype.Component;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. @Component
  7. @Endpoint(id = "custom")
  8. public class CustomEndpoint {
  9. @ReadOperation
  10. public Map<String, String> customEndpoint() {
  11. Map<String, String> result = new HashMap<>();
  12. result.put("message", "This is a custom endpoint");
  13. return result;
  14. }
  15. }

在上述代码中,我们定义了一个名为 custom 的自定义端点,当访问 http://localhost:8080/actuator/custom 时,将返回以下响应:

  1. {
  2. "message": "This is a custom endpoint"
  3. }

五、总结

Spring Boot Actuator 为我们提供了便捷的应用程序监控和管理功能。通过集成 Actuator,我们可以轻松地获取应用程序的健康状态、性能指标等信息。同时,我们还可以自定义端点,满足特定的监控需求。希望本文能帮助你更好地使用 Spring Boot Actuator 来监控和管理你的 Java Web 应用程序。

以上就是关于 Spring Boot Actuator 监控应用运行状态的详细介绍,希望对你有所帮助!

监控与管理 - Actuator - 监控应用运行状态