如何使用 Spring Boot Actuator 组件实现系统监控?
wptr33 2024-12-07 17:44 12 浏览
这一讲我们将介绍 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
相关推荐
- Git 的设计:版本管理的天才之作
-
Git的设计之所以被称为“天才”,是因为它通过一系列**革命性的理念**和**精妙的工程实现**,完美解决了版本控制中的核心痛点。以下是其设计精髓的深度解析:---###一、**颠覆传统的核心设计...
- IDEA上如何重新设置git账号密码
-
修改git账号密码,使用IDEA拉取项目的文件,出现如下错误信息。fatal:unabletoaccess'http://192.168.105.101:8901/XX/XX-XX-p...
- 在IDEA中利用ignore插件忽略Git非必要提交的文件
-
迎关注我的头条号:Wooola,10年Java软件开发及架构设计经验,专注于Java、Go语言、微服务架构,致力于每天分享原创文章、快乐编码和开源技术。前言在IEDA中,使用Maven建多工程编写代码...
- Git入门系列之分支切换完全指南
-
在Git中切换分支时,若存在未提交的修改,需谨慎处理以避免数据丢失。主要注意事项包括:1)通过gitstatus检查当前修改;2)优先使用gitstash暂存修改再切换;3)可提交修...
- 解决Git推送提交时'src refspec master does not match any'错误
-
技术背景在使用Git进行代码管理时,我们经常会遇到各种问题。其中,“srcrefspecmasterdoesnotmatchany”错误是一个比较常见的问题。当我们尝试使用...
- 修改git提交的用户名
-
在我们项目中,新手同学们往往由于不当操作导致提交的用户名填写错误,当我们用户名填写错误的时候可以通过命令修改。获取当前用户名:gitconfiguser.name...
- 在项目管理中,自动关联Git代码提交,集成Gitlab/码云/Git等
-
不再让开发提交的代码成为“黑洞”在软件开发过程中,会有一个问题和担心,就是不知道开发人员提交的代码质量如何,规范怎样。更为让人痛苦的莫过于当时某个功能需求改了什么代码,根本无人知道。今天,分享一个工具...
- 常用的十五个Git命令汇总?
-
Git是一个分布式的版本管理系统,在开发中被广泛应用于代码版本的管理,下面就是在日常开发中常用的一些Git命令以及其示例操作,如下所示,我们就来一起看看吧!gitinit用于初始化一个新的Git仓库...
- git修改已提交记录的用户信息
-
背景介绍因为使用的是个人电脑,配置的git全局config的用户信息是和github的账户一致的。新下载的工作git,由于没有单独设置局部的用户信息,导致提交记录使用的是github用户,在push代...
- 刚来公司,大佬让我Git下 我该怎么办?
-
我们在开发过程中,都会用到版本控制工具。常用的工具有SVNGIt等。但现在越来越多的人喜欢用Git。本文为你介绍新手如何快速上手Git。创建自己的远程仓库远程仓库,顾名思义。就是将自己的代码放到远程服...
- Git+Maven+Sonar实现提交代码前进行代码的质量检查
-
Git+Maven+Sonar实现提交代码前进行代码的质量检查一、前言为了规范代码质量,使开发人员写出更高质量的代码,实践了一下git-hooks中的pre-commit钩子,可以在提交代码时强制校验...
- Git Rebase
-
本文档将深入讨论gitrebase命令。Rebase命令在设置仓库和重写历史页面中也有涉及。本页将更详细地介绍gitrebase的配置和执行。这里将涵盖常见的Rebase使用场景和注...
- Git提交规范
-
一、分区存储...
- git合并分支时禁止合并特定文件
-
问题:1.在日常开发过程中经常会遇到多环境,但是环境文件不同的情况,导致每次切换git环境时候非常麻烦,可能会提交上来不需要提交的文件,一个文件来回提交修改。场景:...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)