Skip to content

1. 简介

背景: 当我们在开发和部署应用程序时,监控应用程序的健康状况性能是非常重要的。

  • 如何知道应用程序是否健康?
  • 如何知道线上应用程序性能指标?

Spring Boot ActuatorSpring Boot 提供的用于监控管理应用程序的模块。

  • 它提供了一组现成的端点(Endpoint), 可以让我们方便地监控和管理应用程序。
  • 这些端点包括健康状况性能指标配置信息日志等。
  • 通过这些端点,我们可以了解应用程序的运行状况及时发现和解决问题提高应用程序的可用性和可维护

2. 添加依赖

这些 spring-boot-actuator模块提供 Spring Boot 的所有生产准备功能。

  • 推荐的启用这些功能的方法是在 spring-boot-starter-actuator “启动器”。
xml
<!-- START: actuator 监视器 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- END:  actuator 监视器 -->
  • 启动服务后,通过命令访问是否正常集成 Actuator监视器是否成功
shell
curl --request GET -sL --url '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
        }
    }
}

3. 端点

执行器端点允许您监视应用程序并与之交互。 Spring Boot 包括:

  • 许多内置端点
  • 自定义自己的端点

开启端点

yaml
# actuator 监视器配置
management:
  # 配置端点的启用
  endpoint:
    # 显示应用程序信息
    info:
      enabled: true
yml
# actuator 监视器配置
management:
  # 配置端点的启用
  endpoint:
    # 使应用程序正常关闭。 (默认情况下禁用)
    shutdown:
      enabled: true

注意事项

  • 配置了不一定生效,需要配置暴露端点后,才可以正常访问。

暴露端点

由于端点可能包含敏感信息,因此应仔细考虑开放暴露端点。

例如,通过 HTTP 公开除了 env 和 beans 终端点,请使用以下属性:

  • * 可用于选择所有端点。
yaml
management:
  endpoints:
    # web 端
    web:
      # 暴露端点
      exposure:
        # 不公共的内置 env 和 beans
        exclude: env,beans
        # 公共所有的端点
        include: '*'

配置跨域访问

yaml
# actuator 监视器配置
management:
  endpoints:
    # web 访问
    web:
      # 暴露端点
      exposure:
        # 公共所有
        include: "*"   # * 在yaml 文件属于关键字,所以需要加引号
        # 不包含的内置 env 和 beans
        exclude: env,beans
      # 跨域配置
      cors:
        # 允许GET、POST 请求
        allowed-methods: GET,POST
        # 跨域的域名
        allowed-origins: http://localhost:8080

自定义端点

默认的端点虽然可以满足大多数的需求,但一些特殊的需求还是需要能够支持自定义端点的。

  • 自定义 Endpoint 端点,只需要在我们的新建 Bean 上使用@Endpoint 注解即可, Bean 中的方法就可以通过 JMX 或者 HTTP 公开。
  • 除此之外,还可以使用 @JmxEndpoint@WebEndpoint 编写 EndPoint
  • 但这些 EndPoint 仅限于各自的公开方式。例如,@WebEndpoint 仅通过 HTTP 公开,而不通过 JMX 公开。

那么是不是类中所有的方法都支持对外公开呢?很明显不是的。Actuator 提供了三个用于方法上的注解,只有加三个注解的方法才支持对外公开,并且每个注解都有支持它的 HTTP method。

  • @ReadOperation对应 HTTP 的 GET 请求
  • @WriteOperation对应 HTTP 的 POST 请求
  • @DeleteOperation对应 HTTP 的 DELETE 请求
java
package com.calvin.spring.boot.example.actuator.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

/**
 * 自定义端点
 *
 * @author calvin
 * @date 2024/01/30
 */
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {

    /**
     * 端点自定义读取
     *
     * @param content 内容
     * @return {@link String}
     */
    @ReadOperation
    public String endpointCustomRead(String content) {
        return "请求的内容: " + content;
    }

    /**
     * 端点自定义写入
     *
     * @param content 内容
     * @return {@link String}
     */
    @WriteOperation
    public String endpointCustomWrite(String content) {
        return "写的内容: " + content;
    }

    /**
     * 端点自定义删除
     *
     * @param content 内容
     * @return {@link String}
     */
    @DeleteOperation
    public String endpointCustomDelete(String content) {
        return "删除的内容: " + content;
    }

}
  • 输出结果:
shell
curl --request GET -sL \
     --url 'http://localhost:8080/actuator/custom?content=endpointGet'

# 输出信息:
请求的内容: endpointGet

curl --request POST -sL \
     --url 'http://localhost:8080/actuator/custom?content=endpointPost'

# 输出信息:
写的内容: endpointPost


curl --request DELETE -sL \
     --url 'http://localhost:8080/actuator/custom?content=endpointDelete'

# 输出信息:
删除的内容: endpointDelete

4.视频演示

5. 参考网站