news 2026/10/14 1:45:40

SpringBoot监控功能

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot监控功能

Spring Boot监控

1、Spring Boot actuator

它是Spring Boot 中提供了一个用于监控和管理自身应用信息的模块,帮助我们及时发现应用程序故障

1.初体验

通过集成 actuator 可以将 Spring Boot 应用本身的一些信息通过 HTTP 或 JMX 的方式暴露出去,可以让第三方的监控工具拉取这些信息,进行信息的管理,查看,监控和告警。

  1. pom.xml添加starter依赖

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.3.12.RELEASE</version></dependency>
  2. yaml添加配置

    management:endpoints:# 默认开启端点enabled-by-default:trueweb:exposure:# 暴露所有端点include:"*"
  3. 访问http://ip:port/项目路径/actuator

内置端点概述

iddescSensitive
auditevents显示当前应用程序的审计事件信息Yes
beans显示应用Spring Beans的完整列表Yes
caches显示可用缓存信息Yes
conditions显示自动装配类的状态及及应用信息Yes
configprops显示所有 @ConfigurationProperties 列表Yes
env显示 ConfigurableEnvironment 中的属性Yes
flyway显示 Flyway 数据库迁移信息Yes
health显示应用的健康信息(未认证只显示status,认证显示全部信息详情)No
info显示任意的应用信息(在资源文件写info.xxx即可)No
liquibase展示Liquibase 数据库迁移Yes
metrics展示当前应用的 metrics 信息Yes
mappings显示所有 @RequestMapping 路径集列表Yes
scheduledtasks显示应用程序中的计划任务Yes
sessions允许从Spring会话支持的会话存储中检索和删除用户会话。Yes
shutdown允许应用以优雅的方式关闭(默认情况下不启用)Yes
threaddump执行一个线程dumpYes
httptrace显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换)Yes
2.端点的配置

从Spring Boot 2.x开始,我们需要手动的去启用和暴露端点。默认情况下,除了/shutdown之外的所有端点都是可用的,同时只有/health和/info端点是对外暴露的。

暴露和排除端点

management:endpoints:# 默认开启端点enabled-by-default:trueweb:exposure:# 暴露端点,*表示所有已经开启的端点 也可以通过逗号分隔多个要暴露的端点include:"*"# 排除端点(虽然开启端点,但没有暴露,无法访问)exclude:beans

开启指定端点

management:endpoints:# 默认不开启端点(只有/health和/info端点)enabled-by-default:falseweb:exposure:# 暴露端点,*表示所有已经开启的端点 也可以通过逗号分隔多个要暴露的端点include:"*"# 排除端点(虽然开启端点,但没有暴露,无法访问)exclude:beansendpoint:health:# 开启health端点 ,注意开启后还需要暴露端点enabled:truebeans:# beans虽然开启了,但是被排除了没有暴露也无法访问enabled:true

注:
1.Actuator中/shutdown端点比较敏感,该端点默认是不可用的,需要我们手动开启。
2./shutdown端点仅接收POST请求

3 自定义端点

除了使用Actuator默认给予的端点外,还可以自定义端点来满足自定义监控的要求。在 Actuator中加入端点只需要加入注解@Endpoint 即可,这个注解会同时提供JMX 监控和Web 监控。如果只想提供JMX监控,可以使用注解@JmxEndpoint; 如果只想提供 Web 监控可以使用注解@WebEndpoint。

  1. 编码

    @Component//id 自定义端点的唯一标识@Endpoint(id="dbcheck")publicclassDatabaseConnectionEndpoint{@Value("${spring.datasource.driver-class-name}")privateStringdriverClassName;@Value("${spring.datasource.url}")privateStringurl;@Value("${spring.datasource.username}")privateStringusername;@Value("${spring.datasource.password}")privateStringpassword;//一个端点只能存在一个@ReadOperation标准的方法// 它表示HTTP的get请求@ReadOperationpublicMap<String,Object>testConnectDatabase(){System.out.println("DatabaseConnectionEndpoint.testConnectDatabase");Map<String,Object>map=newHashMap<>();Connectionconn=null;try{Class.forName(driverClassName);conn=DriverManager.getConnection(url,username,password);map.put("success",true);map.put("message","测试数据库连接成功");}catch(Exceptione){map.put("success",false);map.put("message",e.getMessage());}finally{if(conn!=null){try{conn.close();}catch(SQLExceptionthrowables){throwables.printStackTrace();}}}returnmap;}}
  2. 配置相关参数

    spring:datasource:driver-class-Name:com.mysql.jdbc.Driverurl:jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername:rootpassword:root

注:
1.一个Endpint中只能定义一个@ReadOperation注解
2.除了@ReadOperation外,还可以定义@WriteOperation(对应POST请求)和@DeleteOperation(对应DELETE请求)

4 自定义HealthIndicator

health端点用来显示程序的健康信息。health端点在默认情况下, 仅显示"status": "UP"。 如需显示详细信息, 可以做如下配置:

management:endpoint:health:enabled:true# 显示health端点的详细信息show-details:always

再次访问/health端点可以看到如下结果,可以看到健康指示器下面多出来的两个健康指标,分别为diskSpace磁盘空间和ping检查。

{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":329034756096,"free":45609562112,"threshold":10485760,"exists":true}},"ping":{"status":"UP"}}}

如果内置的健康指标项无法满足需要,我们可以自定义健康指标项。例如,我们现在需要监测服务器是否可以访问万维网 (World wide Web,www),就可以自定义健康指标。

@ComponentpublicclassWwwHealthIndicatorextendsAbstractHealthIndicator{privatefinalstaticStringBAIDU_HOST="www.baidu.com";privatefinalstaticintTIME_OUT=3000;@OverrideprotectedvoiddoHealthCheck(Health.Builderbuilder)throwsException{if(ping()){builder.withDetail("message","当前服务可以访问万维网").up();}else{builder.withDetail("message","当前服务不可以访问万维网").outOfService();}}privatebooleanping(){try{returnInetAddress.getByName(BAIDU_HOST).isReachable(TIME_OUT);}catch(IOExceptione){returnfalse;}}}

2、Spring Boot Admin

目前所有的端点信息不直观,可以通过 Spring Boot Admin 来对 actuator 信息进行管理。
Spring Boot Admin 用于管理和监控 Spring Boot 应用程序的运行状态。在 Spring Boot 项目中可以通过集成 Spring Boot Admin Client 向 Spring Boot Admin Server 进行注册,这样我们就可以在Spring Boot Admin Server 统一管理 Spring Boot 应用。

Spring Boot Admin 主要功能点:

  • 显示应用程序的监控状态
  • 应用程序上下线监控
  • 查看 JVM 和线程信息
  • 可视化的查看日志及下载日志文件
  • 动态切换日志级别
  • HTTP 请求信息跟踪等实用功能
  • 将 actuator 提供的数据进行可视化

开发Admin服务端

  1. 新建一个项目,pom.xml引入spring-boot-admin-starter-server依赖

    <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><!-- 注意版本,要和springboot的版本兼容--><version>2.3.1</version></dependency>
  2. 启动类上添加@EnableAdminServer注解

    @SpringBootApplication@EnableAdminServerpublicclassAdminApplication{publicstaticvoidmain(String[]args){SpringApplication.run(AdminApplication.class,args);}}
  3. 为避免和其它项目冲突,服务端口配置为8081

    server:port:8081
  4. 访问http://localhost:8081

配置Admin客户端(Spring Boot actuator项目)

  1. 项目中增加spring-boot-admin-starter-client依赖

    <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.3.1</version></dependency>
  2. yaml中配置admin服务端的地址

    spring:boot:admin:client:url:http://localhost:8081instance:#使用ip注册prefer-ip:true
  3. 访问http://localhost:8081

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/14 1:45:25

一图带你了解人工智能简史

最原始的关于人工智能的研究&#xff0c;可以追溯到古希腊时期&#xff0c;以哲学家亚里士多德创立形式逻辑这一新学科为代表[1]。 数学家怀特海德&#xff08;Whitehead&#xff09;和罗素&#xff08;Russell&#xff09;在其名著《数学原理》中提出了数理逻辑理论&#xff0…

作者头像 李华
网站建设 2026/10/14 1:45:24

[F2F] Spring相关

Spring 基础 📌 过滤器和拦截器有什么区别? Filter 是 Servlet 规范定义的过滤器层面的东西,Interceptor 是 Spring (SpringMVC) 实现层面的东西。 📌 为什么 Spring Security / Shiro 框架选 Filter,Sa-Token 选拦截器 📌 Spring Security / Shiro 框架选 Filter…

作者头像 李华
网站建设 2026/10/14 1:45:10

MAC常用命令记录

----------------Mac相关-----------------创建目录 mkdir tmp-打开目录 cd ./tmp-使用访达打开目录 open ./tmp-创建文件 touch xxxx.txt-查看目录文件 ls -al 注: -a 显示隐藏&#xff1b;-l 显示文件信息-查看当前进程 ps -a 注: -a 当前用户全部&#xff1b;-A 全部用户…

作者头像 李华
网站建设 2026/10/14 1:44:59

2020年VM Pro16.1.0注册、安装以及使用教程

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/10/14 1:44:57

Microsoft Sql Server2016安装使用

公司一个新的项目要求使用 SqlServer 数据库&#xff0c;SqlServer 以前没使用过&#xff0c;最近安装了开发版的 2016&#xff0c;在此记录一下&#xff1a; 微软 SQL Server 2016 正式版将分为四个版本&#xff0c;分别是企业版&#xff08;Enterprise&#xff09;、标准版&…

作者头像 李华
网站建设 2026/10/14 1:44:10

ATEasy测试平台核心价值:设备无关流程与工程级稳定性解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华