百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

如何使用 Spring Boot Actuator 组件实现系统监控?

wptr33 2024-12-07 17:44 21 浏览

这一讲我们将介绍 Spring Boot 中一个非常有特色的主题——系统监控。

系统监控是 Spring Boot 中引入的一项全新功能,它能够有效的对应用程序运行状态进行管理。它为我们的应用提供了强大的监控能力。随着互联网发展,现在的应用程序越来越复杂了,线上常常需要借助一些监控工具去帮助我们快速的定位问题。

Spring Boot Actuator 组件主要通过一系列 HTTP 端点提供的系统监控功能来实现系统监控。因此,接下来我们将引入 Spring Boot Actuator 组件,介绍如何使用它进行系统监控,以及如何对 Actuator 端点进行扩展。

引入 Actuator 组件

如何整合 Actuator,第一件事需要引用依赖。首先我们需要引入 Spring Boot Actuator 组件,具体操作为在 pom 中添加如下所示的 Maven 依赖:

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

请注意,引入 Spring Boot Actuator 组件后,并不是所有的端点都对外暴露。这样的应用就整合好了, 我们把应用启动起来。我们就可以在启动日志中发现如下所示内容:

Exposing 1 endpoint(s) beneath base path '/actuator'

通过浏览器访问 http://localhost:8080/actuator 端点后,会得到如下所示结果。

{
	"_links": {
		"self": {
			"href": "http://localhost:8080/actuator",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8080/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8080/actuator/health/{*path}",
			"templated": true
		}
	}
}

这种结果就是 HATEOAS 风格的 HTTP 响应。

health 端点

Actuator 端点是干嘛用的呢?这是 Spring Boot Actuator 的一个导航端点。它可以展示出 Spring Boot Actuator 所有端点。

通过浏览器访问 http://localhost:8080/actuator/health 端点后,会得到如下所示结果。

{"status":"UP"}

health 端点是一个非常重要的端点。在后面的课程中我们会不断的用到。

它的作用是什么呢?是健康检查。什么是健康检查呢?检查的又是什么呢?检查的是应用的资源。什么是应用的资源呢?要解释清楚这一点,我们需要加一个配置。

management.endpoint.health.show-details=always

这个配置是做什么的呢?上述配置项指定了针对 health 端点需要显示它的详细信息。这时,如果我们重启 Spring Boot 应用程序,并重新访问 http://localhost:8082/actuator/health 端点,就可以获取如下所示的详细信息。

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

Spring Boot Actuator 支持检查哪些资源呢?这个就非常多了。如下图所示:

status 取值有四种取值。第一种 up 表示正常。第二种 DOWN 表示遇到了问题,不正常。第三种 OUT_OF_SERVICE 这表达的是什么意思呢,资源未在使用或者不该使用。第四种 UNKNOWN 不知道,Actuator 不知道状态是什么。

info 端点

Info 端点用于暴露 Spring Boot 应用的自身信息。在 Spring Boot 内部,它把这部分工作委托给了一系列 InfoContributor 对象,而 Info 端点会暴露所有 InfoContributor 对象所收集的各种信息。

访问 http://localhost:8080/actuator/info 端点后,返回了一个空。它是做什么用的呢?info 端点是 Actuator 里面的一个例外,它并不是一个监控端点,而是一个描述性的端点。它是用来描述应用的使用方式非常的简单。它的格式 key-value 形式,主要遵守这个格式,随便怎么写。

info.app.encoding=UTF-8
info.app.java.source=1.8.0_291
info.app.java.target=1.8.0_291

现在访问 http://localhost:8080/actuator/info 端点,我们就能得到如下的 Environment 信息。

{
	"app": {
		"encoding": "UTF-8",
		"java": {
			"source": "1.8.0_291",
			"target": "1.8.0_291"
		}
	}
}

同时我们还可以这么配置 info 端点,而不是硬编码这些值。我们就可以按照如下所示的配置,重写前面的示例,重启我们应用,最后得到同样的效果。

info.app.encoding=@project.build.sourceEncoding@
info.app.java.source=@java.version@
info.app.java.target=@java.version@

info 一般的使用是建议描述应用。什么意思呢?比如我可以写上应用的名称。项目的开发者是谁,开发者的邮箱,一旦我们的应用出现问题,我们就可以知道是谁开发的,找到相应的人解决问题。

暴露端点

前面我们说过 Actuator 有点提供了强大的监控能力。但是我们现在这个应用才有两个端点,那么如何暴露所有的端点?如果我们想看到默认情况下看不到的所有端点,则需要在配置文件中添加如下所示的配置信息。

management.endpoints.web.exposure.include=*

重启应用后,我们就能获取到 Spring Boot 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": {
			"href": "http://localhost:8080/actuator/loggers",
			"templated": false
		},
		"loggers-name": {
			"href": "http://localhost:8080/actuator/loggers/{name}",
			"templated": true
		},
		"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 官网阅读文档,这里不做详细介绍。

Actuator 端点那么多,有时候有的端点并不想激活,那怎么办呢?比如只向激活 metrics。

只要配置 metrics 即可。

{
	"_links": {
		"self": {
			"href": "http://localhost:8080/actuator",
			"templated": false
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
			"templated": true
		},
		"metrics": {
			"href": "http://localhost:8080/actuator/metrics",
			"templated": false
		}
	}
}

那如果我想激活多个端点,比如暴露 health 端点和 metrics 端点,端点与端点使用逗号分隔。具体配置如下:

management.endpoints.web.exposure.include=metrics,health

我们把应用重启下,访问 http://localhost:8080/actuator/ 端点,可以看到现在应用只会暴露 metrics 端点以及 health 端点,其他的都被隐藏了。

{
	"_links": {
		"self": {
			"href": "http://localhost:8080/actuator",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8080/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8080/actuator/health/{*path}",
			"templated": true
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
			"templated": true
		},
		"metrics": {
			"href": "http://localhost:8080/actuator/metrics",
			"templated": false
		}
	}
}

但是 Actuator 其实有很多的配置。全部都是以 management 开头。这说明了 Actuator 提供了非常灵活的配置,帮助你自定义 Actuator 的各种行为。

到目前为止,我们用 Spring Boot Actuator 端点监控应用的时候,都是以 json 文本的形式在使用。而实际项目中,我们是不是往往需要一个可视化的监控工具。有没有这样的工具呢?肯定是有的。

总结

Spring Boot 内置的 Actuator 组件使得开发人员在管理应用程序运行的状态有了更加直接且高效的手段。我们引入了 Actuator 组件并介绍了该组件提供的一系列核心端点,同时重点分析了 Info 和 Health 这两个基础端点。

作者:程序猿的果壳
链接:https://juejin.cn/post/7031837721219825694

相关推荐

F103C8T6移植FATFS文件系统 版本R0.15

STM32F103C8T6芯片在W25Q64上移植FATFS(版本R0.15)实现过程:1、首先完成USART初始化和调试,用于传输信息到串口调试软件。2、完成SPI相关参数配置及调试,用于单片机和存...

stm32使用MPU6050或ADXL345控制的车辆减速灯

本实验例程采用MPU6050六轴运动处理组件...

STM32F103串口输出prtinf覆盖(stm32printf函数的串口输出)

采用正点原子的板子,有如下坑,记录如下:(1)main中应用头文件#include"stdio.h"(2)采用hal进行fputc和fgetc覆盖,如下intfputc(intc...

STM32 学习8 USART串口通讯与printf重定向

一、串口通信介绍STM32F103ZET6包含多个UART、USART串口。...

教你如何使用SEGGER RTT优雅的实现日志系统

今天开始了BMS系统的软件代码部分的搭建,计划是分成三层:硬件驱动,AFE层和系统应用层。第一步肯定是先把底层的IIC通信调通,CG861xx的IIC通信和TI的BQ769X0...

终极调试利器,各种Link通吃(link4a调制方式)

今天继续更新一期KEIL调试方法。事实上,关于调试方法,鱼鹰写了一个系列,汇总文为《佛祖保佑,永无BUG,永不修改|KEIL调试系列总结篇》,对于KEIL方法感兴趣的可以看看。这个调试...

在 STM32 中使用 printf() 函数,别漏掉这几行代码!

问:在STM32上轻松使用printf函数除了点亮LED外,向串行控制台发送打印信息可能是调试嵌入式项目时最简单、最直接且最常用的技术。虽然大多数平台都拥有可以在UART总线上传输数据的API,但它们...

高性能异步io机制:io_uring(异步io select)

io_uring是linux内核5.10引入的异步io接口。相比起用户态的DPDK、SPDK,io_uring作为内核的一部分,通过mmap的方式实现用户和内核共享内存,并基于m...

精品博文ARM中打印函数print 的几种实现方法

1利用C库函数printf步骤:1)首先需要包含头文件stdio.h。2)然后定义文件句柄。实际上就是一个int型变量封装在结构体中。struct__FILE{inthandle;};3)定...

C语言char的详解(c语言(char))

在C语言中,char是一种基础数据类型,用于表示字符或小整数值。对char的理解和处理非常重要,尤其是在字符串操作、文件读写或其他需要直接控制内存的应用场景中。下面从基本定义、存储方式、常见用法...

C语言之文件操作(c语言文件操作实验总结)

文件操作是C语言中非常重要的功能,用于读取和写入文件中的数据。C语言提供了一组标准库函数(如fopen、fclose、fread、fwrite等)来实现文件操作。以下是针对C语言初学者的详细讲解。...

STM32-ADC如何把采集的数据转换为小数

编辑一、代码原理解析这段代码围绕“STM32中ADC数据采集、整数与小数计算及串口输出”展开,核心是数据类型的使用(unsignedint/signedint/float)、ADC数...

循环队列原理及在单片机串口通讯中的应用(二)

前言书接上回,前文主要介绍了环形队列的实现原理以及C语言实现及测试过程,本文将回归到嵌入式平台的应用中,话不多说,淦,上干货!...

STM32编程中printf函数重定向背后的原理

  在C语言中,printf是一个非常好用的函数,尤其是在程序调试阶段,我们可以通printf打印变量的值来帮助查错。在学习C语言的时候我们的开发环境和运行环境都是PC机,printf函数打印到PC机...

MySQL 避坑指南之隐式数据类型转换

...