「这是我参与11月更文挑战的第15天,活动详情查看:2021最后一次更文挑战」
通过前面的学习,我们知道如何使用 Spring Boot 开发我们 Web 应用。这一讲我们将介绍 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 这两个基础端点。
近期评论