简介
Spring Boot Actuator 是 Spring Boot 提供的一个功能强大的模块,用于监控和管理您的应用程序。它为开发人员和运维团队提供了许多有用的端点,用于获取有关应用程序运行时状态的信息。
什么是端点?
"端点"是指提供了某种功能或信息的特定URL路径。这些URL路径可以用于通过HTTP请求与应用程序进行交互,获取各种信息或执行一些操作。Actuator 提供了一系列内建的端点,如健康检查、信息获取、指标监控等,这些端点可以用于监控和管理应用程序。
相关API
-  
/actuator/health端点:监控应用程序的健康状况,了解应用程序是否正常运行。 -  
/actuator/info端点:获取关于应用程序的信息,自定义向外部提供的应用程序信息。 -  
/actuator/metrics端点:收集应用程序的指标和性能数据,查看有关应用程序性能的详细信息。 -  
/actuator/env端点:查看应用程序的环境和配置,了解应用程序运行的环境和配置。 
举例
1、引入
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
 
2、配置打开端点
# 打开所有端点
management.endpoints.web.exposure.include=*
# 打开某几个
#management.endpoints.web.exposure.include=health,info,metrics
# actuator的服务端口可以跟项目的设置不一样
#management.server.port=9091
# 显示更详细的信息
# management.endpoint.health.show-details=always
 
3、测试
GET http://127.0.0.1:9090/actuator/health
 
返回
 
{
  "status": "UP"
}
 
代表服务正常
 这里还可以显示更详细的信息,你可以在上面的配置文件中找到注释的地方,返回的信息如下:
{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP",
      "details": {
        "database": "MySQL",
        "validationQuery": "isValid()"
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 494384795648,
        "free": 85839474688,
        "threshold": 10485760,
        "path": "/Users/zhangyu/web/web-all/java-all/zy-spring-boot/.",
        "exists": true
      }
    },
    "ping": {
      "status": "UP"
    }
  }
}
 
扩展现有端点
比如你觉得 health 端点提供的信息太少,你可以通过扩展 AbstractHealthIndicator 类创建自定义健康指示器,并在其中定义更详细的健康检查逻辑。然后,通过 /actuator/health 端点将详细信息呈现出来。
package com.zhangyu.endpoint;
import org.springframework.stereotype.Component;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        // 在此处添加自定义健康检查逻辑
        builder.up().withDetail("custom", "Custom health check passed");
    }
}
 
返回的效果可以看到我们自定义的 custom 字段了
 
{
  "status": "UP",
  "components": {
    "custom": {
      "status": "UP",
      "details": {
        "custom": "Custom health check passed"
      }
    },
    "db": {
      "status": "UP",
      "details": {
        "database": "MySQL",
        "validationQuery": "isValid()"
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 494384795648,
        "free": 85857583104,
        "threshold": 10485760,
        "path": "/Users/zhangyu/web/web-all/java-all/zy-spring-boot/.",
        "exists": true
      }
    },
    "ping": {
      "status": "UP"
    }
  }
}
 
自定义端点
当你想要完全自定义一个端点的时候,可以这样
package com.zhangyu.endpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Endpoint(id = "custom-health")
public class CustomHealthEndpoint {
    @Bean
    @ReadOperation
    public Map<String, Object> customHealth() {
        return Map.of("status", 200, "details", "测试");
    }
}
 
GET http://127.0.0.1:9090/actuator/custom-health
 
返回
{
  "status": 200,
  "details": "测试"
}
 

总结
Spring Boot Actuator 提供了强大的监控和管理功能,使开发人员和运维团队能够更好地了解和维护应用程序。通过集成 Actuator,您可以轻松地实现对应用程序的实时监控和分析。



















