当前位置:   article > 正文

Spring boot——Actuator 详解_springboot actuator

springboot actuator

一、什么是 Actuator

Spring Boot Actuator 模块提供了生产级别的功能,比如健康检查,审计,指标收集,HTTP 跟踪等,帮助我们监控和管理Spring Boot 应用。

这个模块是一个采集应用内部信息暴露给外部的模块,上述的功能都可以通过HTTP 和 JMX 访问。

因为暴露内部信息的特性,Actuator 也可以和一些外部的应用监控系统整合(Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic等)。

这些监控系统提供了出色的仪表板,图形,分析和警报,可帮助你通过一个统一友好的界面,监视和管理你的应用程序。

Actuator使用Micrometer与这些外部应用程序监视系统集成。这样一来,只需很少的配置即可轻松集成外部的监控系统。

  1. Micrometer 为 Java 平台上的性能数据收集提供了一个通用的 API,应用程序只需要使用 Micrometer 的通用 API 来收集性能指标即可。
  2. Micrometer 会负责完成与不同监控系统的适配工作。这就使得切换监控系统变得很容易。

需要注意的是:

SpringBoot 1.x 和 2.x 的 Actuator 监控設定差超多,不僅提供的 endpoint 路徑不一樣,連 application.properties 的配置也不一樣,此處介紹的為 SpringBoot 2.x 版本。

二、集成 Actuator

如果要使用 SpringBoot Actuator 提供的监控功能,需要先加入相關的 maven dependency:

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

只要加上了這個 maven dependency,SpringBoot 在運行時就會自動開啟/actuator/health和/actuator/info這兩個 endpoint,我們就可以透過這兩個 endpoint 查看當前 SpringBoot 運行的情況。

Actuator 其實還提供更多样化的选择 endpoint 讓我們監控 SpringBoot Application,但是因為安全因素,所以需要另外設置才能打開這些 endpoint,详細的設置方式下面解說。

访问
http://localhost:8099/actuator,查看暴露出来的端点:

 

三、Endpoints 介绍

Spring Boot 提供了所谓的 endpoints (下文翻译为端点)给外部来与应用程序进行访问和交互。

打比方来说,/health 端点 提供了关于应用健康情况的一些基础信息。metrics 端点提供了一些有用的应用程序指标(JVM 内存使用、系统CPU使用等)。

这些 Actuator 模块本来就有的端点我们称之为原生端点。根据端点的作用的话,我们大概可以分为三大类:

  • 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
  • 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
  • 操作控制类:提供了对应用的关闭等操作类功能。

需要注意的就是:

  • 每一个端点都可以通过配置来单独禁用或者启动
  • 不同于Actuator 1.x,Actuator 2.x 的大多数端点默认被禁掉。 Actuator 2.x 中的默认端点增加了/actuator前缀。默认暴露的两个端点为/actuator/health和 /actuator/info

Actuator 提供的所有 endpoint:

此处使用的是 SpringBoot 2.2.8 版本,Spring 官方文件

HTTP方法

Endpoint

描述

GET

/actuator

查看有哪些 Actuator endpoint 是開放的

GET

/actuator/auditevent

查看 audit 的事件,例如認證進入、訂單失敗,需要搭配 Spring security 使用,sample code

GET

/actuator/beans

查看運行當下面全部的 bean,以及他們的關係

GET

/actuator/conditions

查看自動配置的結果,記錄哪些自動配置條件通過了,哪些沒通過

GET

/actuator/configprops

查看注入带有 @ConfigurationProperties 類的 properties 值為何(包含默認值)

GET

/actuator/env (常用)

查看全部環境属性,可以看到 SpringBoot 载入了哪些 properties,以及這些 properties 的值(但是會自動*掉帶有 key、password、secret 等關鍵字的 properties 的值,保護安全資訊)

GET

/actuator/flyway

查看 flyway DB 的 migration 資訊

GET

/actuator/health (常用)

查看當前 SpringBoot 運行的健康指標,值由 HealthIndicator 的實現類提供(所以可以自定義一些健康指標資訊,加到這裡面)

GET

/actuator/heapdump

取得 JVM 當下的 heap dump,會下載一個檔案

GET

/actuator/info

查看 properties 中 info 開頭的屬性的值,沒啥用

GET

/actuator/mappings

查看全部的 endpoint(包含 Actuator 的),以及他們和 Controller 的關係

GET

/actuator/metrics(常用)

查看有哪些指標可以看(ex: jvm.memory.max、system.cpu.usage),要再使用/actuator/metrics/{metric.name}分別查看各指標的詳細資訊

GET

/actuator/scheduledtasks

查看定時任務的資訊

POST

/actuator/shutdown

唯一一個需要 POST 請求的 endpoint,關閉這個 SpringBoot 程式

四、端点配置

  • 默认暴露

我们可以通过以下配置,来配置通过JMX 和 HTTP 暴露的端点。

Property

Default

management.endpoints.jmx.exposure.exclude

management.endpoints.jmx.exposure.include

*

management.endpoints.web.exposure.exclude

management.endpoints.web.exposure.include

info, healt

因為安全的因素,所以 Actuator 默認只會開放/actuator/health和/actuator/info這兩個 endpoint,如果要開放其他 endpoint 的話,需要額外在 application.properties 中做設置。

  • 暴露配置
  1. # 可以這樣寫,就會開啟所有endpoints(不包含shutdown)
  2. management.endpoints.web.exposure.include=*
  3. # 也可以這樣寫,就只會開啟指定的endpoint,因此此處只會再額外開啟/actuator/beans和/actuator/mappings
  4. management.endpoints.web.exposure.include=beans,mappings
  5. # exclude可以用來關閉某些endpoints
  6. # exclude通常會跟include一起用,就是先include了全部,然後再exclude /actuator/beans這個endpoint
  7. management.endpoints.web.exposure.exclude=beans
  8. management.endpoints.web.exposure.include=*
  9. # 如果要開啟/actuator/shutdown,要額外再加這一行
  10. management.endpoint.shutdown.enabled=true
  • 路径映射

默认情况下所有端点都暴露在“/actuator”路径下,也可以改變/actuator的路徑,可以自定義成自己想要的:

  1. #這樣寫的話,原本內建的/actuator/xxx路徑,都會變成/manage/xxx,可以用來防止被其他人猜到
  2. management.endpoints.web.base-path=/manage
  3. #同时可以将health修改成healthcheck
  4. management.endpoints.web.path-mapping.health=healthcheck
  • 管理端口调整
  1. #指定端口,默认跟server.port一样,可以防止被其他人猜到
  2. management.server.port=10111
  • 端点响应缓存

对于一些不带参数的端点请求会自动进行缓存,我们可以通过如下方式配置缓存时间,下面配置表示 beans 端点的缓存时间为 100s

management.endpoint.beans.cache.time-to-live=100s

五、端点保护

如果開啟了 Actuator 默認不打開的 endpoints,建議一定要加上 Spring security 之類的做 endpoint 保護,避免重要資訊外洩。因为端点的信息和产生的交互都是非常敏感的,必须防止未经授权的外部访问。

这里我们使用 Spring Security 保护,首先添加相关依赖:

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

添加之后,我们需要定义安全校验规则,来覆盖Spring Security 的默认配置。

这里我给出了两个版本的模板配置:

  1. import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
  2. import org.springframework.boot.actuate.context.ShutdownEndpoint;
  3. import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigu
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/856494
推荐阅读
相关标签
  

闽ICP备14008679号