spring:spring-boot-starter-actuator的使用

简介

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,您可以轻松地实现对应用程序的实时监控和分析。

注意:

  • 可能要重启项目才能生效
  • 必须添加至少一个http服务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lvan的前端笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值