自定义 Hea1thIndicator |学习笔记

简介: 快速学习自定义 Hea1thIndicator

开发者学堂课程【SpringBoot 快速掌握 - 高级应用:自定义 Hea1thIndicator】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址:https://developer.aliyun.com/learning/course/613/detail/9325


自定义Hea1thIndicator

内容介绍

一、定制端点信息

二、健康状态指示器


一、定制端点信息

1.health

在访问端点中,存在一个端点:health

(1)halth 的作用

进行应用中的组件健康检查

(2)health 的访问

//将端点更改为8181端口

localhost:8181//manage/health

//其为操作者返回当前应用每个组件的健康状态,但此处仅默认存在 diskSpace-即磁盘空间的默认状态。

{

status:"UP",

- diskSpace: {

status: "UP",

total: 126682750976,

free: 42200621056,

threshold: 10485760

}

}

2.spring-boot-actuator

另外,也可自行定制或使用 spring-boot 中现成完成配置的健康状态。

(1)包含内容

所有的端点配置信息都存在于 spring-boot-actuator 包之下,存在众多的健康状态组件,如:MongoHealthIndicator、RabbitHealthIndicator、RedisHealthIndicator

(2)生效

编写相应 start 后进行生效

(3)测试

//引入 Redis 的相关 start

//配置 Redis 连接信息

spring.redis.host=localhost  //本机未安装 redis,此时为错误配置

//启动引用

//检查健康信息,此时 redis 存在错误

{

status:"UP",

-diskSpace: {

status: "UP",

total: 126682750976,

free: 4220062 1056,

threshold: 10485760

}

}

- redis: {

status: "DOWN",  

error:" org. springf ramework.data redis. RedisConnect ionFailureException:Cannot get Jedis connecticn; nested exception is redis.clients.jedis.exceptions.JedisCormnectionException: Could not get aresource from the pool"

}

}

//将 redis 连接信息书写正确,即可使其正常运行

//将连接信息书写为已安装 redis 的远程主机地址

spring.redis.host=118.24.44.169

//进行重新启动

//若 redis 一切工作正常,代表着其健康状态同样正常

//进行刷新,此时 redis 为 UP 状态

{

status:"UP",

- diskSpace: {

status: "UP",

total: 126682750976,

free: 42164674560

threshold: 10485760

}

}

-redis: {

status: "UP",  

version “4.0.9”

}

}

//监控中存在 RedisHealthIndicator,检查获取 redis 是否能够连接


二、健康状态指示器

1.目的

在后续开发中存在众多服务组件,欲拥有对应的实时健康状态检查、访问health时能够反馈组件健康信息,则应进行自定义健康状态检查指示器

2.操作步骤

(1)大致方向

编写一个实现  HealthIndactor 接口的指示器

指示器名字:xxxHealthIndicator

例如:位于 RedisHealthIndicator 中,则其指示器名字应为 RedisHealthIndicator

加入容器中

(2)实际操作

编写指示器:healthMyAPPHealthIndactor

@Component

public calss healthMyAPPHealthIndactor implements Health {

@Override

public Health health(){

//自定义检查方法

return null;

}

}

//若检查结果为健康

return Health.up().build()

//若检查不健康

return Health.down().withDetail(“msg”,“服务异常”).build();

}

}

//进行服务的启动

//检查健康状况信息

//刷新

{

status:"DOWN",

-MyApp: {

status: "DOWN",

total:126682750976,

free:42080505856

threshold:10485760

}

}

-redis: {

status: "UP",  

version “4.0.9”

}

}

//此时以除  HealthIndactor 以前的部分作为指示器名字

相关文章
|
运维 Kubernetes 数据库
《云服务器运维之Windows篇》电子版地址下载
本书从技术原理到线上真实案例,从基础篇、进阶篇到高手篇,用11个章节,详解阿里云上Windows服务器运维要点,助你轻松掌握阿里云上Windows服务器运维的相关知识和技能!经出版社授权线上上架30%内容,更多详情可自行购买学习
265 0
《云服务器运维之Windows篇》电子版地址下载
|
Scala 开发者
嵌套分支|学习笔记
快速学习嵌套分支。
510 0
嵌套分支|学习笔记
|
算法 数据挖掘 Python
论文阅读之 A Convex Optimization Framework for Active Learning
A Convex Optimization Framework for Active Learning Active learning is the problem of progressively selecting and annotating the most informative...
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8090 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10553 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全