SpringBoot集成Actuator健康指示器health
1.说明
本文详细介绍Actuator提供的HealthIndicators,
即健康指示器的配置使用,
利用自动配置的健康指标,
检查正在运行的应用程序的状态,
以及自定义健康指标的方法。
监控软件通过调用健康指示器接口,
在生产系统出现故障时发出提醒。
2.查看健康指示器
访问health端点:
http://localhost:8011/actuator/health
返回结果:
{
"status": "UP"
}
3.显示详细信息
health端点公开的信息可以配置,
通过management.endpoint.health.show-details属性,
可以配置为以下值之一:
| 名称 | 描述 |
|---|---|
| never | 详细信息从不显示。 |
| when-authorized | 详细信息仅显示给授权用户,可以使用management.endpoint.health.roles配置授权角色。 |
| always | 详细信息显示给所有用户。 |
默认值为never,
所以上面返回结果显示的信息很少。
当用户处于端点的一个或多个角色时,
该用户被视为已被授权。
如果端点没有配置授权角色(默认),
则所有经过身份验证的用户都被视为已授权,
可以使用management.endpoint.health.roles配置授权角色。
下面设置show-details属性为always,
显示所有的详细信息:
management:
endpoint:
health:
show-details: always
重启后再次访问health端点:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 2000397791232,
"free": 831475470336,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
可以看到健康指示器下面多出来的两个指标,
分别为diskSpace磁盘空间和ping检查。
4.自动配置HealthIndicators
在应用程序使用到相关功能的情况下,
Spring Boot会自动配置以下HealthIndicators,
这样访问health端点的时候就能看到对应指标:
| 名称 | 描述 |
|---|---|
| CassandraHealthIndicator | 检查Cassandra数据库是否已启动。 |
| CouchbaseHealthIndicator | 检查Couchbase群集是否启动。 |
| DiskSpaceHealthIndicator | 检查磁盘空间是否不足。 |
| DataSourceHealthIndicator | 检查是否可以获取到DataSource的连接。 |
| ElasticsearchHealthIndicator | 检查Elasticsearch群集是否启动。 |
| InfluxDbHealthIndicator | 检查InfluxDB服务器是否启动。 |
| JmsHealthIndicator | 检查JMS代理是否启动。 |
| MailHealthIndicator | 检查邮件服务器是否启动。 |
| MongoHealthIndicator | 检查Mongo数据库是否已启动。 |
| Neo4jHealthIndicator | 检查Neo4j服务器是否启动。 |
| RabbitHealthIndicator | 检查Rabbit服务器是否启动。 |
| RedisHealthIndicator | 检查Redis服务器是否启动。 |
| SolrHealthIndicator | 检查Solr服务器是否启动。 |
5.配置DataSource指标
下面演示配置DataSource指标的方法,
同样的其他指标在引入相关功能后会自动配置。
在引入数据库相关的starter之后,
就会自动配置DataSource指标。
在pom.xml中新增MySQL数据库依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
然后在application.yml中新增数据库配置:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.21.6.30:3306/demodb
username: demo
password: demo123456
重启后再次访问health端点:
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 2000397791232,
"free": 831475470336,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
可以看到多出来了db指标,
连接的是MySQL数据库,
并且数据库连接是正常的。
故意将数据库的密码配置错误,
重启后再次访问health端点:
{
"status": "DOWN",
"components": {
"db": {
"status": "DOWN",
"details": {
"error": "org.springframework.jdbc.CannotGetJdbcConnectionException:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLException:
Access denied for user 'demo'@'10.21.6.30' (using password: YES)"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 2000397791232,
"free": 831475466240,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
发现db指标是DOWN状态了。
6.启用和禁用指标
默认所有指标都是启用的,
可以通过设置management.health.defaults.enabled属性来禁用,
设置为false就是禁用所有指标,
然后可以单独启用某个指标。
下面演示禁用所有指标,
仅启用db指标:
management:
endpoint:
health:
show-details: always
health:
defaults:
enabled: false
db:
enabled: true
重启后再次访问health端点,
就只能看到db指标了:
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
}
}
}
}
如果应用程序使用了数据库相关功能,
但是不想对外开放db指标,
可以单独禁用db指标:
management:
endpoint:
health:
show-details: always
health:
defaults:
enabled: true
db:
enabled: false
7.自定义HealthIndicators方法1
要使用自定义健康信息,
可以注册实现HealthIndicator接口的Spring beans。
提供health()方法的实现并返回Health响应。
Health响应应包括状态,
并可以增加其他详细信息。
以下代码演示了一个HealthIndicator的实现,
名称为MyHealthIndicator,
这样指标名称就是my,
注意实现类的名称格式必须为XXXHealthIndicator:
package com.yuwen.spring.actuator.actuate.health;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {
private static int num = 0;
@Override
public Health health() {
// 进行一些特定的健康检查
int errorCode = check();
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
// 这里模拟检查,设置为一次正常一次异常
private int check() {
num++;
return num % 2;
}
}
重启后访问health端点,
检查结果为异常:
{
"status": "DOWN",
"components": {
"my": {
"status": "DOWN",
"details": {
"Error Code": 1
}
}
}
}
再次访问health端点,
检查结果为正常:
{
"status": "UP",
"components": {
"my": {
"status": "UP"
}
}
}
如果只想显示my指标的结果:
可以修改application.yml,
禁用所有的内置指标:
management:
endpoint:
health:
show-details: always
health:
defaults:
enabled: false
8.修改自定义指标的名称
如果不想修改实现类的名称,
可以通过Component修改指标名称:
@Component("my1")
public class MyHealthIndicator implements HealthIndicator
这样指标名称就从my改为my1了:
{
"status": "DOWN",
"components": {
"my1": {
"status": "DOWN",
"details": {
"Error Code": 1
}
}
}
}
{
"status": "UP",
"components": {
"my1": {
"status": "UP"
}
}
}
9.自定义HealthIndicators方法2
通过继承AbstractHealthIndicator抽象类,
重写doHealthCheck方法,
而不是直接实现HealthIndicator接口,
功能比第一种要强大一点点,
默认的DataSourceHealthIndicator、
RedisHealthIndicator都是这种方法,
代码回调中还做了异常的处理。
以下代码演示了一个AbstractHealthIndicator的继承类,
名称为My2HealthIndicator,
这样指标名称就是my2:
package com.yuwen.spring.actuator.actuate.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.stereotype.Component;
@Component
public class My2HealthIndicator extends AbstractHealthIndicator {
private static int num = 0;
@Override
protected void doHealthCheck(Builder builder) throws Exception {
int errorCode = check();
if (errorCode != 0) {
builder.down().withDetail("code", errorCode).withDetail("num", num).build();
return;
}
builder.up().withDetail("code", errorCode).withDetail("num", num).build();
}
// 这里模拟检查,设置为一次成功一次失败
private int check() {
num++;
return num % 2;
}
}
重启后访问health端点,
检查结果为异常:
{
"status": "DOWN",
"components": {
"my1": {
"status": "DOWN",
"details": {
"Error Code": 1
}
},
"my2": {
"status": "DOWN",
"details": {
"code": 1,
"num": 1
}
}
}
}
再次访问health端点,
检查结果为正常:
{
"status": "UP",
"components": {
"my1": {
"status": "UP"
},
"my2": {
"status": "UP",
"details": {
"code": 0,
"num": 2
}
}
}
}
10.参考文档
Spring Boot Actuator: Production-ready Features
Spring Boot (27) actuator服务监控与管理
SpringBoot集成Actuator健康指示器health的更多相关文章
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成actuator模块的基本使用
© 版权声明:本文为博主原创文章,转载请注明出处 1. 版本 SpringBoot:2.0.0.RELEASE 2. 集成 SpringBoot集成actuator模块非常简单,只需要引入actuat ...
- SpringBoot集成Actuator监控管理
1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...
- SpringBoot集成Actuator端点配置
1.说明 Actuator端点可以监控应用程序并与之交互. Spring Boot包括许多内置的端点, 比如health端点提供基本的应用程序运行状况信息, 并允许添加自定义端点. 可以控制每个单独的 ...
- SpringBoot之actuator
在springBoot中集成actuator可以很方便的管理和监控应用的状态. 暴露的Restful接口有: HTTP方法 路径 描述 鉴权 GET /autoconfig 查看自动配置的使用情况 t ...
- Spring Boot Actuator:健康检查、审计、统计和监控(转)
Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- Spring Boot Actuator:健康检查、审计、统计和监控
Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...
- SpringBoot系列: Actuator监控
Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...
随机推荐
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- 如何在linux 上配置NTP 时间同步?
故障现象: 有些应用场景,对时间同步的要求严格,需要用到NTP同步,如何在linux上配置NTP时间同步? 解决方案: 在linux 上配置NTP 时间同步,具休操作步骤,整理如下: 1.安装软件包( ...
- 使用buffered流结合byte数组,读入文件中的内容,包括中文字符
package com.itcast.demo05.Buffered;import java.io.BufferedInputStream;import java.io.FileInputStream ...
- html上传图片的预览功能实现
表单代码(仅取上传文件部分): <input class="selectImg" style="position:absolute;opacity: 0;width ...
- 通过SSE(Server-Send Event)实现服务器主动向浏览器端推送消息
一.SSE介绍 1.EventSource 对象 SSE 的客户端 API 部署在EventSource对象上.下面的代码可以检测浏览器是否支持 SSE. if ('EventSource' in w ...
- Java高精度基础+开根
在焦作站的acm网络赛中遇到了一个高精度开根的水题--但是那时候WA了 后面学写java补题还T了orz 所以写一篇文章来记录一下java的大整数类型的基础和开根还有一点心得体会吧 首先给那一题的题面 ...
- 代码图形统计工具git_stats web
目录 一.简介 二.安装ruby 三.配置git_stats 四.通过nginx把网页展示出来 一.简介 仓库代码统计工具之一,可以按git提交人.提交次数.修改文件数.代码行数.注释量在时间维度上进 ...
- Python pyecharts绘制柱状图
本文摘抄至https://05x-docs.pyecharts.org/#/zh-cn/charts_base?id=bar%ef%bc%88%e6%9f%b1%e7%8a%b6%e5%9b%be%e ...
- java 图形化小工具Abstract Window Toolit ImageIO缩放图片,添加水印
实现步骤: 读取图像Image src = ImageIO.read 创建目标图像BufferedImage distImage = new BufferedImage(dstWidth, dstHe ...
- 『与善仁』Appium基础 — 27、模拟手势点击坐标
目录 1.模拟手势点击坐标 2.tap()用法 3.练习 4.弊端 1.模拟手势点击坐标 在定位元素的时候,你使出了十八班武艺还是定位不到,怎么办呢?(面试经常会问) 那就拿出绝招:点击元素所在位置的 ...