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

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

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

这一讲我们将介绍 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

相关推荐

每天一个AI姬,AMD核显用户有福了,AI绘画打破 NVIDIA 显卡垄断

使用StableDiffusion进行AI绘画,并不一定只能使用NVIDIA英伟达显卡,甚至,也不一定只能使用独立显卡。今天我们使用AMD6800H核显,并安装了StableDif...

NETworkManager:功能强大的网络管理与问题排除工具

关于NETworkManagerNETworkManager是一款功能强大的网络管理与问题排除工具,该工具完全开源,可以帮助广大研究人员轻松管理目标网络系统并排除网络疑难问题。该工具使用远程桌面、Po...

AMD也能深度学习+免费AI绘画:StableDiffusion+ROCm部署教程!

某国政客扇扇嘴皮子,CN玩硬件和深度学习的圈子里就掀起了一场风暴,这就是著名的嘴皮子效应(误)。没了高性能计算的A100H100倒也能理解,但是美利坚这波把RTX4090禁售了就让人无语了,所以不少做...

windows 下编译 python_rtmpstream

最近在研究数字人,看了大咖的项目(https://github.com/lipku/metahuman-stream),尝试编译此项目的依赖项目python_rtmpstream(https://gi...

如何使用 Python 操作 Git 代码?GitPython 入门介绍

花下猫语:今天,我在查阅如何用Python操作Gitlab的时候,看到这篇文章,觉得还不错,特分享给大家。文中还提到了其它几种操作Git的方法,后续有机会的话,再陆续分享之~~作者:匿蟒...

网上看了不少,终于把ZlmediaKit流媒体框架搭建起来啦

你都站在2023年代了,视频通话、视频直播、视频会议、视频监控就是风口浪尖上的猪师兄,只要你学那么一丁点,拿个高薪的工作不过分吧!我也是半瓶子晃荡的,所以路人呀,共学习,同进步!本篇开始,只讲在Lin...

MacDown:一款 macOS 的强大 Markdown 编辑器

大家好,很高兴又见面了,我是"...

ZLMediaKit安装配置和推拉流

一、ZLMediaKit库简介ZLMediaKit是一个基于...

大神赞过的:学习 WebAssembly 汇编语言程序设计

文/阿里淘系F(x)Team-旭伦随着前端页面变得越来越复杂,javascript的性能问题一再被诟病。而Javascript设计时就不是为了性能优化设计的,这使得浏览器上可以运行的本地语言一...

【Docker】部署WVP视频监控平台

回来Docker系列,今天将会跟大家分享一则关于开源WVP视频监控平台的搭建。先说结论吧,一开始按照网上说的一步一步搭建没有搭建成功,不知道是版本太旧还是我这边机器有问题,尝试了好几个不同方式的搭建都...

MongoDB+GridFS存储文件方案

GridFS是MongoDB的一个内置功能,它提供一组文件操作的API以利用MongoDB存储文件,GridFS的基本原理是将文件保存在两个Collection中,一个保存文件索引,一个保存文...

【开源】强大、创新且直观的 EDA套件

今天分享的LibrePCB是...

Ollama如何制作自己的大模型?

背景Llama3发布了,这次用了...

Ollama使用指南【超全版】

一、Ollama快速入门Ollama是一个用于在本地运行大型语言模型的工具,下面将介绍如何在不同操作系统上安装和使用Ollama。官网:https://ollama.comGithub:http...

基于区块链的价值共享互联网即时通讯应用平台源码免费分享

——————关注转发之后私信回复【源码】即可免费获取到本项目所有源码基于区块链的价值共享互联网即时通讯应用平台,是一个去中心化的任何人都可以使用的通讯网络,是一款基于区块链的价值共享互联网即时通讯AP...