Spring Boot Actuator 使用介绍

article/2025/11/9 16:43:49

Spring Boot Actuator 使用介绍

  • 初识 Actuator
  • 原生端点
    • 应用配置类
    • 度量指标类
  • 操作控制类

近期在看《Spring Cloud 微服务实战》,由于时间过去几年,对于Actuator监控端点的介绍过时,故作此文更新一下。

Spring Boot 版本:2.5.3

初识 Actuator

在现有的Spring Boot应用中引入该模块非常简单,只需要在pom.xmldependencies节点中,新增spring-boot-starter-actuator的依赖即可,具体如下:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

通过增加该依赖之后,重新启动应用。此时,我们可以在控制台中看到如下图所示的输出:
在这里插入图片描述
上图显示在路径/actuator暴露了一个端点,端点并非我们自己在程序中创建,而是由spring-boot-starter-actuator模块根据应用依赖和配置自动创建出来的监控和管理端点。通过这些端点,我们可以实时的获取应用的各项监控指标,比如:访问/actuator/health端点,我们可以获得如下返回的应用健康信息:

{status: "UP"
}

原文中列出的大量的端点哪去了呢?经查找是因为在web情景下为了安全没有暴露出来,因此我们需要在application.properties文件中设置:

management.endpoints.web.exposure.include=*

再次启动应用,可有如下输出:
在这里插入图片描述
通过访问 http://localhost:8080/actuator,可能看到所有端点:

{_links: {self: {href: "http://localhost:8080/actuator",templated: false},beans: {href: "http://localhost:8080/actuator/beans",templated: false},caches-cache: {href: "http://localhost:8080/actuator/caches/{cache}",templated: true},caches: {href: "http://localhost:8080/actuator/caches",templated: false},health: {href: "http://localhost:8080/actuator/health",templated: false},health-path: {href: "http://localhost:8080/actuator/health/{*path}",templated: true},info: {href: "http://localhost:8080/actuator/info",templated: false},conditions: {href: "http://localhost:8080/actuator/conditions",templated: false},configprops: {href: "http://localhost:8080/actuator/configprops",templated: false},configprops-prefix: {href: "http://localhost:8080/actuator/configprops/{prefix}",templated: true},env: {href: "http://localhost:8080/actuator/env",templated: false},env-toMatch: {href: "http://localhost:8080/actuator/env/{toMatch}",templated: true},loggers-name: {href: "http://localhost:8080/actuator/loggers/{name}",templated: true},loggers: {href: "http://localhost:8080/actuator/loggers",templated: false},heapdump: {href: "http://localhost:8080/actuator/heapdump",templated: false},threaddump: {href: "http://localhost:8080/actuator/threaddump",templated: false},metrics-requiredMetricName: {href: "http://localhost:8080/actuator/metrics/{requiredMetricName}",templated: true},metrics: {href: "http://localhost:8080/actuator/metrics",templated: false},scheduledtasks: {href: "http://localhost:8080/actuator/scheduledtasks",templated: false},mappings: {href: "http://localhost:8080/actuator/mappings",templated: false}}
}

通过点击具体的链接,获取更详细的描述。

原生端点

通过在前一节中添加 spring-boot-starter-actuator模块,我们已经对它有了一个初步的认识。接下来,我们详细介绍一下spring-boot-starter-actuator模块中已经实现的一些原生端点。如果根据端点的作用来说,我们可以原生端点分为三大类:

  • 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
  • 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
  • 操作控制类:提供了对应用的关闭等操作类功能。
    下面我们来详细了解一下这三类端点都分别可以为我们提供怎么样的有用信息和强大功能,以及我们如何去扩展和配置它们。

应用配置类

由于Spring Boot为了改善传统Spring应用繁杂的配置内容,采用了包扫描和自动化配置的机制来加载原本集中于xml文件中的各项内容。虽然这样的做法,让我们的代码变得非常简洁,但是整个应用的实例创建和依赖关系等信息都被离散到了各个配置类的注解上,这使得我们分析整个应用中资源和实例的各种关系变得非常的困难。而这类端点就可以帮助我们轻松的获取一系列关于Spring 应用配置内容的详细报告,比如:自动化配置的报告、Bean创建的报告、环境属性的报告等。

  • /conditions(原/autoconfig):该端点用来获取应用的自动化配置报告,其中包括所有自动化配置的候选项。同时还列出了每个候选项自动化配置的各个先决条件是否满足。所以,该端点可以帮助我们方便的找到一些自动化配置为什么没有生效的具体原因。该报告内容将自动化配置内容分为两部分:
    • positiveMatches 中返回的是条件匹配成功的自动化配置
    • negativeMatches 中返回的是条件匹配不成功的自动化配置
    • unconditionalClasse 未设置条件的自动化配置
{contexts: {application: {positiveMatches: {AuditEventsEndpointAutoConfiguration: [{condition: "OnAvailableEndpointCondition",message: "@ConditionalOnAvailableEndpoint no property management.endpoint.auditevents.enabled found so using endpoint default; @ConditionalOnAvailableEndpoint marked as exposed by a 'management.endpoints.web.exposure' property"},...],...},negativeMatches: {RabbitHealthContributorAutoConfiguration: {notMatched: [{condition: "OnClassCondition",message: "@ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate'"}],...},unconditionalClasses: ["org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration","org.springframework.boot.actuate.autoconfigure.availability.AvailabilityHealthContributorAutoConfiguration","org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration","org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration","org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration","org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration","org.springframework.boot.actuate.autoconfigure.metrics.integration.IntegrationMetricsAutoConfiguration","org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration","org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration","org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration","org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration"]],

从如上示例中我们可以看到,每个自动化配置候选项中都有一系列的条件,比如上面没有成功匹配的RabbitHealthContributorAutoConfiguration配置,它的先决条件就是需要在工程中包含org.springframework.amqp.rabbit.core.RabbitTemplate类,由于我们没有引入相关的依赖,它就不会执行自动化配置内容。所以,当我们发现有一些期望的配置没有生效时,就可以通过该端点来查看没有生效的具体原因。

  • /beans:该端点用来获取应用上下文中创建的所有Bean。
{contexts: {application: {beans: {endpointCachingOperationInvokerAdvisor: {aliases: [ ],scope: "singleton",type: "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",resource: "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",dependencies: ["org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration","environment"]},...},parentId: null}}
}

如上示例中,我们可以看到在每个bean都以键值对出现,key为Bean的名称,value是个集合,都包含了下面这几个信息:

- scope:Bean的作用域
- type:Bean的Java类型
- reource:class文件的具体路径
- dependencies:依赖的Bean名称
  • /configprops:该端点用来获取应用中配置的属性信息报告。从下面该端点返回示例的片段中,我们看到返回了关于该短信的配置信息,prefix属性代表了属性的配置前缀,properties代表了各个属性的名称和值。所以,我们可以通过该报告来看到各个属性的配置路径,比如我们要关闭该端点,就可以通过使用endpoints.configprops.enabled=false来完成设置。
  • /env:该端点与/configprops不同,它用来获取应用所有可用的环境属性报告。包括:环境变量、JVM属性、应用的配置配置、命令行中的参数。从下面该端点返回的示例片段中,我们可以看到它不仅返回了应用的配置属性,还返回了系统属性、环境变量等丰富的配置信息,其中也包括了应用还没有没有使用的配置。所以它可以帮助我们方便地看到当前应用可以加载的配置信息,并配合@ConfigurationProperties注解将它们引入到我们的应用程序中来进行使用。另外,为了配置属性的安全,对于一些类似密码等敏感信息,该端点都会进行隐私保护,但是我们需要让属性名中包含:password、secret、key这些关键词,这样该端点在返回它们的时候会使用*来替代实际的属性值。
  • /mappings:该端点用来返回所有Spring MVC的控制器映射关系报告。从下面的示例片段中,我们可以看该报告的信息与我们在启用Spring MVC的Web应用时输出的日志信息类似,其中bean属性标识了该映射关系的请求处理器,method属性标识了该映射关系的具体处理类和处理函数。
  • /info:该端点用来返回一些应用自定义的信息。默认情况下,该端点只会返回一个空的json内容。我们可以在application.properties配置文件中通过info前缀来设置一些属性

度量指标类

  • /metrics:该端点用来返回当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息等。
{names: ["http.server.requests","jvm.buffer.count","jvm.buffer.memory.used","jvm.buffer.total.capacity","jvm.classes.loaded","jvm.classes.unloaded","jvm.gc.live.data.size","jvm.gc.max.data.size","jvm.gc.memory.allocated","jvm.gc.memory.promoted","jvm.gc.pause","jvm.memory.committed","jvm.memory.max","jvm.memory.used","jvm.threads.daemon","jvm.threads.live","jvm.threads.peak","jvm.threads.states","logback.events","process.cpu.usage","process.files.max","process.files.open","process.start.time","process.uptime","system.cpu.count","system.cpu.usage","system.load.average.1m","tomcat.sessions.active.current","tomcat.sessions.active.max","tomcat.sessions.alive.max","tomcat.sessions.created","tomcat.sessions.expired","tomcat.sessions.rejected"]
}

以上列出了key,通过将key加在链接后面,即可查看详情数据,例如想了解jvm内存使用,访问http://localhost:8080/actuator/metrics/jvm.memory.used,结果如下:

{name: "jvm.memory.used",description: "The amount of used memory",baseUnit: "bytes",measurements: [{statistic: "VALUE",value: 108388088}],availableTags: [{tag: "area",values: ["heap","nonheap"]},{tag: "id",values: ["Compressed Class Space","PS Survivor Space","PS Old Gen","Metaspace","PS Eden Space","Code Cache"]}]
}

在自定义标量值时,Springboot1.5.x支持的org.springframework.boot.actuate.metrics.CounterServiceorg.springframework.boot.actuate.metrics.GaugeService已不再支持,改用注解。即:
@Endpoint@WebEndpoint@EndpointWebExtension 上的操作将使用 Jersey、Spring MVC 或 Spring WebFlux 通过 HTTP 自动暴露。
写代码如下:

import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;@Configuration
@WebEndpoint(id = "testEndpoint")
public class HelloController {@ReadOperationpublic HashMap<String, Object> index() {HashMap<String, Object> hashMap = new HashMap<>();hashMap.put("name", "sky");hashMap.put("age", 24);return hashMap;}
}

就可以访问http://localhost:8080/actuator/testEndpoint,得出结果如下:

{name: "sky",age: 24
}
  • /health:该端点在一开始的示例中我们已经使用过了,它用来获取应用的各类健康指标信息。在spring-boot-starter-actuator模块中自带实现了一些常用资源的健康指标检测器。

操作控制类

在原生端点中,只提供了一个用来关闭应用的端点:/shutdown。我们可以通过如下配置开启它:

management.endpoint.shutdown.enabled=true

在配置了上述属性之后,只需要访问该应用的/shutdown端点就能实现关闭该应用的远程操作。由于开放关闭应用的操作本身是一件非常危险的事,所以真正在线上使用的时候,我们需要对其加入一定的保护机制。


http://chatgpt.dhexx.cn/article/XnaWME5g.shtml

相关文章

Activiti集成Activiti Modeler

Activiti6.0.0及以上版本与activiti-modeler的maven引用有冲突&#xff0c;解决方法参考Activiti6.0.0及以上版本集成Activiti Modeler 1.下载源文件 activiti-5.22.0官方Demo activiti5.22.0源码 2.copy源文件 &#xff08;一&#xff09;复制前端文件 解压activiti-5.22.…

【activiti】activiti入门

activiti入门 在本章内容中&#xff0c;我们来创建一个Activiti工作流&#xff0c;并启动这个流程。 创建Activiti工作流主要包含以下几步&#xff1a; 1、定义流程&#xff0c;按照BPMN的规范&#xff0c;使用流程定义工具&#xff0c;用流程符号把整个流程描述出来 2、部署…

Activiti 介绍

一、工作流 1.工作流 工作流(Workflow)&#xff0c;就是“业务过程的部分或整体在计算机应用环境下的自动化”&#xff0c;它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档、信息或任务的过程自动进行&#xff0c;从而实现某个预期的业务目标&#xff0c;或…

Vuetify组件中常见的v-slot:activator=“{ on, attrs }“是什么意思?

在使用Vuetify组件时&#xff0c;常看到v-slot:activator"{ on, attrs }"以及插槽中的v-bind"attrs" v-on"on" 例如&#xff1a; 由于之前写代码时少有这种写法而且是第一次遇见&#xff0c;用久了难免想知道是什么意思。因为国内没有相关的问…

Tedddby Activator V5.1,免费绕过iOS 14.7Beta,支持iCloud登录

Tedddby Activator 是一款Windows下绕激活的工具&#xff0c;目前来说也是最好用的一款软件&#xff01; Tedddby Activator官网&#xff1a;https://tedddby.com 支持的功能 GSM两网绕过可以打电话/4G/短信/iCloud登陆/完美重启/消息推送 MEID三网游戏机绕过可以登陆iClou…

Spring Boot Actuator详解与深入应用(一):Actuator 1.x

《Spring Boot Actuator详解与深入应用》预计包括三篇&#xff0c;第一篇重点讲Spring Boot Actuator 1.x的应用与定制端点&#xff1b;第二篇将会对比Spring Boot Actuator 2.x 与1.x的区别&#xff0c;以及应用和定制2.x的端点&#xff1b;第三篇将会介绍Actuator metric指标…

springboot 集成 actuator

简介 spring-actuator做度量统计收集&#xff0c;使用Prometheus&#xff08;普罗米修斯&#xff09;进行数据收集&#xff0c;Grafana&#xff08;增强ui&#xff09;进行数据展示&#xff0c;用于监控生成环境机器的性能指标和业务数据指标。一般&#xff0c;我们叫这样的操作…

Activiti应用

1.介绍 Activiti是一个工作流引擎&#xff0c; activiti可以将业务系统中复杂的业务流程抽取出来&#xff0c;使用专门的建模语言 BPMN2.0进行定义&#xff0c;业务流程按照预先定义的流程进行执行&#xff0c;实现了系统的流程由activiti进行管理&#xff0c;减 少业务系统由…

springboot之Actuator

1、Actuator 介绍 Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块&#xff0c;借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看、统计等。 Actuator 的核心是端点 Endpoint&#xff0c;它用来监视应用程序及交互&#xff0c;spring-boo…

ActivitiListener

ActivitiListener 目录概述需求&#xff1a; 设计思路实现思路分析1.ActivitiListener2.Activity3.Gateway5.FieldExtension IOSpecification 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;sk…

SolidWorks2016软件,SW2010-2016.Activator.GUI.SSQ激活闪退解决办法:

SolidWorks2016软件&#xff0c;SW2010-2016.Activator.GUI.SSQ激活闪退解决办法&#xff1a; 解决方案&#xff1a; 原贴&#xff1a; https://xcshare.cn/other/1033.html

Actuator

1&#xff0c;简介 Actuator’ ktʃʊˌeɪtə是 Spring Boot 提供的对应用系统的自省和监控的 集成功能&#xff0c;可以对应用系统进行配置查看、相关功能统计等。在 Spring Cloud 中主要是完成 微服务的监控&#xff0c;完成监控治理。可以查看微服务间的数据处理和调用&…

Spring boot——Actuator 详解

一、什么是 Actuator Spring Boot Actuator 模块提供了生产级别的功能&#xff0c;比如健康检查&#xff0c;审计&#xff0c;指标收集&#xff0c;HTTP 跟踪等&#xff0c;帮助我们监控和管理Spring Boot 应用。 这个模块是一个采集应用内部信息暴露给外部的模块&#xff0c…

Spring Boot Actuator详解

Actuator简介 什么是Spring Boot Actuator&#xff1f; Spring Boot Actuator 模块提供了生产级别的功能&#xff0c;比如健康检查&#xff0c;审计&#xff0c;指标收集&#xff0c;HTTP跟踪等&#xff0c;帮助我们监控和管理Spring Boot应用。这个模块是一个采集应用内部信…

C# 反射之Activator用法举例

概述 程序运行时&#xff0c;通过反射可以得到其它程序集或者自己程序集代码的各种信息&#xff0c;包括类、函数、变量等来实例化它们&#xff0c;执行它们&#xff0c;操作它们&#xff0c;实际上就是获取程序在内存中的映像&#xff0c;然后基于这个映像进行各种操作。 Acti…

将IPA放到服务器提供下载

2015年12月15日 09:45:16 LC_畅 阅读数&#xff1a;3696 &#xff0a; 上传到服务器我们需要两个文件&#xff0c;一个ipa和一个 plist文件 &#xff0a; 注意plist文件和ipa包的名字必须要相同&#xff08;最好取名英文&#xff09; 第一步&#xff1a;把ipa文件放到你们服务…

ipa文件包获取服务器地址,iOS获取App ipa包以及资源文件

要获得线上APP的ipa文件&#xff0c;现在有以下几种方案 1.通过PP助手下载安装到手机的应用 2.通过iTools助手下载安装到手机的应用 3.通过Apple Configurator 2(Mac商店)获取 前两种方案网上的教程很多&#xff0c;这里只介绍第三种方案 首先 去Mac上的App Store下载Apple Con…

AppleStore 原始ipa文件提取

//TODO Apple Configurator 2提取ipa文件_饿到饱的博客-CSDN博客安装Apple Configurator 2从Mac AppStore安装Apple Configurator 2下载ipa打开后连接设备&#xff0c;选中设备点击添加&#xff0c;如果没登录就登录Apple ID&#xff0c;登录后会把你在AppStore下过的应用都列…

获取ipa文件下载链接(appstore下载链接)

获取apptore下载链接 所需工具: 一台越狱的iphone 抓包工具(fiddler或burp等) 步骤 1、ios10以下的版本,越狱之后下载插件ssl kill就可对appstore进行抓包 2、ios11以后的版本,越狱后要想访问appstore还需appstore++插件 3、打开appstore,找到要下载的软件,点击下…

Mac 电脑下载 AppStore 中的 ipa 软件包详细流程

附&#xff1a;iPhone 移除描述文件详细步骤&#xff08;Apple Configurator 2&#xff09; 1、Mac 电脑中安装 Apple Configurator 2 软件。 2、电脑连接手机&#xff0c;并信任&#xff0c;在所有设备中&#xff0c;选中设备&#xff0c;然后点击顶部的加号&#xff0c;选…