健康检查是Spring Boot Actuator中重要端点之一,可以非常容易查看应用运行至状态。本文在前文的基础上介绍如何自定义健康检查。
本节我们简单说明下依赖及启用配置,展示缺省健康信息。首先需要引入依赖:
1
compile("org.springframework.boot:spring-boot-starter-actuator")
现在通过http://localhost:8080/actuator/health端点进行验证:
缺省该端点返回应用中很多组件的汇总健康信息,但可以修改属性配置展示详细内容:
1
2
3
4
management:
endpoint:
health:
show-details: always
现在再次访问返回结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 214748360704,
"free": 112483500032,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
查看DiskSpaceHealthIndicatorProperties文件的源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@ConfigurationProperties(prefix = "management.health.diskspace")
public class DiskSpaceHealthIndicatorProperties {
/**
* Path used to compute the available disk space.
*/
private File path = new File(".");
/**
* Minimum disk space that should be available.
*/
private DataSize threshold = DataSize.ofMegabytes(10);
public File getPath() {
return this.path;
}
public void setPath(File path) {
this.path = path;
}
public DataSize getThreshold() {
return this.threshold;
}
public void setThreshold(DataSize threshold) {
Assert.isTrue(!threshold.isNegative(), "threshold must be greater than or equal to 0");
this.threshold = threshold;
}
}
上面结果显示当前项目启动的路径 . ,报警值 为10M ,这些属性都可以通过配置进行修改。
上面Json响应显示“ping”和“diskSpace”检查。这些检查也称为健康指标,如果应用引用了数据源,Spring会增加db健康指标;同时“diskSpace”是缺省配置。
Spring Boot包括很多预定义的健康指标,下面列出其中一部分:
DataSourceHealthIndicator MongoHealthIndicator Neo4jHealthIndicator CassandraHealthIndicator RedisHealthIndicator CassandraHealthIndicator RabbitHealthIndicator CouchbaseHealthIndicator DiskSpaceHealthIndicator (见上面示例) ElasticsearchHealthIndicator InfluxDbHealthIndicator JmsHealthIndicator MailHealthIndicator SolrHealthIndicator如果在Spring Boot应用中使用Mongo或Solr等,则Spring Boot会自动增加相应健康指标。
Spring Boot提供了一捆预定义健康指标,但并没有阻止你增加自己的健康指标。一般有两种自定义类型检查:
单个健康指标组件和组合健康指标组件。
自定义需要实现HealthIndicator接口并重新health()方法,同时增加@Component注解。假设示例应用程序与服务A(启动)和服务B(关闭)通信。如果任一服务宕机,应用程序将被视为宕机。因此,我们将写入两个运行状况指标。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
public class ServiceAHealthIndicator implements HealthIndicator {
private final String message_key = "Service A";
@Override
public Health health() {
if (!isRunningServiceA()) {
return Health.down().withDetail(message_key, "Not Available").build();
}
return Health.up().withDetail(message_key, "Available").build();
}
private Boolean isRunningServiceA() {
Boolean isRunning = true;
return isRunning;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
public class ServiceBHealthIndicator implements HealthIndicator {
private final String message_key = "Service B";
@Override
public Health health() {
if (!isRunningServiceB()) {
return Health.down().withDetail(message_key, "Not Available").build();
}
return Health.up().withDetail(message_key, "Available").build();
}
private Boolean isRunningServiceB() {
Boolean isRunning = false;
return isRunning;
}
}
现在,我们看到健康监控响应中增加的指标。ServerA状态是UP,ServiceB是DOWN,因此整个监控检测状态为DOWN.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"status": "DOWN",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 214748360704,
"free": 112483229696,
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
},
"serviceA": {
"status": "UP",
"details": {
"Service A": "Available"
}
},
"serviceB": {
"status": "DOWN",
"details": {
"Service B": "Not Available"
}
}
}
}
前面示例很容易查看各个指标各自的状态。但有时需要基于几个指标查看资源的状态,则需要使用 HealthContributor ,该接口没有定义方法,仅用于标记。如果一个服务有另外两个动作组合进行实现,只有两者同时工作该服务状态才算正常。最后使用 CompositeHealthContributors组合多个指标:
1
2
3
4
public class ServiceAHealthIndicator
implements HealthIndicator, HealthContributor {
...
}
下面定义组合健康检查指标:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Component("UserServiceAPI")
public class UserServiceAPIHealthContributor
implements CompositeHealthContributor {
private Map<String, HealthContributor>
contributors = new LinkedHashMap<>();
@Autowired
public UserServiceAPIHealthContributor(
ServiceAHealthIndicator serviceAHealthIndicator, ServiceBHealthIndicator serviceBHealthIndicator) {
contributors.put("serverA", serviceAHealthIndicator);
contributors.put("serverB", serviceBHealthIndicator);
}
/**
* return list of health contributors
*/
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
return contributors.entrySet().stream()
.map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
}
@Override
public HealthContributor getContributor(String name) {
return contributors.get(name);
}
}
现在我们使用serverA和serverB组合新的检查UserServiceAPI。
本文我们学习了Spring Boot健康指标及相关配置、以及预定义的健康指标,同时介绍了如何自定义健康指标。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
关于Spring Cloud健康检查的陷阱springboot 如何禁用某项健康检查SpringBoot Admin健康检查功能的实现详解SpringBoot健康检查的实现原理Spring Cloud Admin健康检查 邮件、钉钉群通知的实现SpringBoot实现项目健康检查与监控SpringBoot actuator 健康检查不通过的解决方案原文链接:https://blog.csdn.net/neweastsun/article/details/108933365
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关知识
spring boot 应用在 k8s 中的健康检查(一)
spring 健康检查地址
如何为SpringBoot应用设置健康检查
如何关闭nacos健康检查
Eureka自我保护机制、健康检查的作用、actuator模块监控
SpringBoot之旅
揭秘微服务健康检查:如何保障系统稳定运行的关键一步
详解 Spring Boot 2.7.18 与 MyBatis PageHelper 的整合步骤
Nacos如何查看集群节点健康状态?
健康检查 检测java假死
网址: Spring Boot Actuator自定义健康检查教程 https://m.trfsz.com/newsview1365992.html