如何使用 Spring Boot Actuator 组件实现系统监控?
wptr33 2024-12-07 17:44 27 浏览
这一讲我们将介绍 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
相关推荐
- redis的八种使用场景
-
前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...
- 基于Redis的3种分布式ID生成策略
-
在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...
- 基于OpenWrt系统路由器的模式切换与网页设计
-
摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...
- 这篇文章教你看明白 nginx-ingress 控制器
-
主机nginx一般nginx做主机反向代理(网关)有以下配置...
- 如何用redis实现注册中心
-
一句话总结使用Redis实现注册中心:服务注册...
- 爱可可老师24小时热门分享(2020.5.10)
-
No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...
- Apportable:拯救程序员,IOS一秒变安卓
-
摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...
- JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透
-
以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...
- 3月26日更新 快速施法自动施法可独立设置
-
2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...
- Redis 是如何提供服务的
-
在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...
- lua _G、_VERSION使用
-
到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
-
BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...
- 移动工作交流工具Lua推出Insights数据分析产品
-
Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...
- Redis 7新武器:用Redis Stack实现向量搜索的极限压测
-
当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...
- Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求
-
重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
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)