当前位置:网站首页>API健康状态自检
API健康状态自检
2022-07-25 09:15:00 【笑 瘾】
一、服务器有哪些健康指标
1、 磁盘空间
2、 CPU状态
3、 MEM状态
4、 服务状态等
二、定义路由分组用于服务器健康检查
1、后期我们会实现很多路由对应的处理函数,如果量大的话,router文件会变得非常大,因此,我们也可以将处理函数放到handler目录中
2、”apiserver/handler/sd“ 此目录将用于保存服务器检查相关处理函数
3、编写路由函数脚本apiserver/router/router.go
// 加载模块-处理函数模块化
"apiserver/handler/sd"
// 在Load函数中添加
// -modify here- 添加健康检查的handler
svcd := g.Group("/sd")
{
svcd.GET("/health", sd.HealthCheck)
svcd.GET("/disk", sd.DiskCheck)
svcd.GET("/cpu", sd.CPUCheck)
svcd.GET("/ram", sd.RAMCheck)
}
4、分析上面的函数脚本
1、该代码块定义了一个叫 sd 的路由分组,在该分组下注册了 `/health`、`/disk`、`/cpu`、`/ram` HTTP 路径,分别路由到 `sd.HealthCheck`、`sd.DiskCheck`、`sd.CPUCheck`、`sd.RAMCheck` 函数。
2、sd 分组主要用来检查 API Server 的状态:健康状况、服务器硬盘、CPU 和内存使用量。
3、`main()` 函数通过调用 `router.Load` 函数来加载路由,路由映射到具体的处理函数
三、服务器健康检查实现
1、编写检查脚本函数apiserver/handler/sd/check.go
package sd
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
)
// 定义常量
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
// HealthCheck shows `OK` as the ping-pong result.
func HealthCheck(c *gin.Context) {
message := "OK"
// http.StatusOK => 所有HTTP状态码都对应到一个名字 (源码)
c.String(http.StatusOK, "\n"+message)
}
// DiskCheck checks the disk usage.
func DiskCheck(c *gin.Context) {
// 可查看disk.Usage的源代码,此处有2个返回值,*UsageStat, erro
u, _ := disk.Usage("/")
usedMB := int(u.Used) / MB
usedGB := int(u.Used) / GB
totalMB := int(u.Total) / MB
totalGB := int(u.Total) / GB
usedPercent := int(u.UsedPercent)
status := http.StatusOK
text := "OK"
if usedPercent >= 95 {
status = http.StatusInternalServerError
text = "CRITICAL"
} else if usedPercent >= 90 {
status = http.StatusTooManyRequests
text = "WARNING"
}
message := fmt.Sprintf("%s - Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%", text, usedMB, usedGB, totalMB, totalGB, usedPercent)
c.String(status, "\n"+message)
}
// CPUCheck checks the cpu usage.
func CPUCheck(c *gin.Context) {
cores, _ := cpu.Counts(false)
a, _ := load.Avg()
l1 := a.Load1
l5 := a.Load5
l15 := a.Load15
status := http.StatusOK
text := "OK"
if l5 >= float64(cores-1) {
status = http.StatusInternalServerError
text = "CRITICAL"
} else if l5 >= float64(cores-2) {
status = http.StatusTooManyRequests
text = "WARNING"
}
message := fmt.Sprintf("%s - Load average: %.2f, %.2f, %.2f | Cores: %d", text, l1, l5, l15, cores)
c.String(status, "\n"+message)
}
// RAMCheck checks the disk usage.
func RAMCheck(c *gin.Context) {
u, _ := mem.VirtualMemory()
usedMB := int(u.Used) / MB
usedGB := int(u.Used) / GB
totalMB := int(u.Total) / MB
totalGB := int(u.Total) / GB
usedPercent := int(u.UsedPercent)
status := http.StatusOK
text := "OK"
if usedPercent >= 95 {
status = http.StatusInternalServerError
text = "CRITICAL"
} else if usedPercent >= 90 {
status = http.StatusTooManyRequests
text = "WARNING"
}
message := fmt.Sprintf("%s - Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%", text, usedMB, usedGB, totalMB, totalGB, usedPercent)
c.String(status, "\n"+message)
}
四、解决依赖并测试
go mod tidy
go get github.com/shirou/gopsutil/cpu
go get github.com/shirou/gopsutil/disk
go get github.com/shirou/gopsutil/load
go get github.com/shirou/gopsutil/mem
五、测试

六、启动apiserver时自检
1、定时任务/监控系统:编写监控脚本,有问题时提醒(邮件/短信/电话/微信/钉钉…)
2、启动服务时:主动检查,有问题直接停掉服务,提醒管理员
3、在apiserver/main.go文件的最后定义pingServer()用于检查/sd/health是否正常访问
// pingServer pings the http server to make sure the router is working.
func pingServer() error {
for i := 0; i < 10; i++ {
// 请求/sd/health => Get返回值有两个
resp, err := http.Get("http://127.0.0.1:8000" + "/sd/health")
log.Print("Waiting for the router, retry in 1 second.")
// 如果返回200,则表示启动成功,直接返回nil
if err == nil && resp.StatusCode == 200 {
return nil
}
// 否则1秒后重试
log.Print("Waiting for the router, retry in 1 second.")
time.Sleep(time.Second)
}
// 尝试10次,均失败则返回一个错误
return errors.New("Cannot connect to the router.")
}
#函数解析
# 在 `pingServer()` 函数中,`http.Get` 向 `http://0.0.0.0:8000/sd/health` 发送 HTTP GET 请求
# 如果函数正确执行并且返回的 HTTP StatusCode 为 200,则说明 API 服务器可用。
# 如果超过指定次数,服务还是不能访问,`pingServer`会 返回errors,表示API服务器不可用。
4.在apiserver/main.go文件的主函数main里面调用pingServer()函数检查服务是否正常
func main() {
...
// 调用协程函数,检查服务健康状态
go func() {
if err := pingServer(); err != nil {
log.Fatal("The router has no response, or it might took too long to start up.", err)
}
log.Print("The router has been deployed successfully.")
}()
// 让应用运行在本地服务器上,默认监听端口是 8080
g.Run("0.0.0.0:8000") // listen and serve on 0.0.0.0:8000
}
#代码解析
# 在启动 HTTP 端口前 go 一个 `pingServer` 协程(后台并行执行的一个任务)
# 启动 HTTP 端口后,该协程不断地 ping `/sd/health` 路径
# 如果成功,则输出部署成功提示
# 如果失败次数超过一定次数,则终止 HTTP 服务器进程
七、测试

边栏推荐
- Learn about spark project on Nebula graph
- Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)
- BGP border gateway protocol basic knowledge points
- Dark horse programmer JDBC
- Robot jumping problem
- Leetcode · 83 biweekly race · 6129. Number of all 0 subarrays · mathematics
- 28.插槽
- Why use MQ message oriented middleware? These questions must be taken down!
- 图解LeetCode——919. 完全二叉树插入器(难度:中等)
- Django4.0 + Web + MySQL5.7 实现简单登录操作
猜你喜欢

Redis/Mysql知识概述

Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?

JS small game source code magic tower breakthrough Download

Django4.0 + web + MySQL 5.7 realize simple login operation

sticksy.js页面滚动div固定位置插件

C语言实现二叉平衡树
![[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box](/img/5c/52ccc0583451eba15fec18adb1499e.png)
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box
![[deep learning] overview | the latest progress of deep learning](/img/b9/6117862397dcda4d555c819e913c9b.png)
[deep learning] overview | the latest progress of deep learning

How to realize the drop-down option box of wechat applet

Silicon Valley classroom lesson 15 - Tencent cloud deployment
随机推荐
Oracle10g单实例数据库升级到哪个版本好,求建议
Software examination system architecture designer concise tutorial | software life cycle
51单片机外设篇:蜂鸣器
为什么说DAO是未来的公司形式
51单片机内部外设:串口通信
The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference
【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
Additional: in the lower division / county (data sheet)
Algorithm --- flip digit (kotlin)
Asp. Net core CMD common instructions
centos更改mysql数据库目录
The development of art NFT
registration status: 204
Do you know these methods of MySQL database optimization?
JDBC快速入门
JDBC的API解析
51 MCU peripherals: buzzer
How does Youxuan database encrypt data?
PL/SQL工具导出sql文件所使用的命令是什么?
Dependency conflict resolution under idea