健康检查
用于 Fiber 的存活和就绪探针中间件,提供两个端点用于检查 HTTP 应用程序的存活和就绪状态。
概述
-
存活探针: 检查服务器是否正在运行。
- 默认端点:
/livez
- 行为: 默认情况下,当服务器正常运行时立即返回
true
。
- 默认端点:
-
就绪探针: 评估应用程序是否准备好处理请求。
- 默认端点:
/readyz
- 行为: 默认情况下,当服务器正常运行时立即返回
true
。
- 默认端点:
-
HTTP 状态码:
200 OK
: 当检查函数评估为true
时返回。503 Service Unavailable
: 当检查函数评估为false
时返回。
签名
func New(config Config) fiber.Handler
示例
导入作为 Fiber Web 框架一部分的中间件包
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/healthcheck"
)
初始化 Fiber app 后,您可以使用以下方法
// Provide a minimal config
app.Use(healthcheck.New())
// Or extend your config for customization
app.Use(healthcheck.New(healthcheck.Config{
LivenessProbe: func(c *fiber.Ctx) bool {
return true
},
LivenessEndpoint: "/live",
ReadinessProbe: func(c *fiber.Ctx) bool {
return serviceA.Ready() && serviceB.Ready() && ...
},
ReadinessEndpoint: "/ready",
}))
配置
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Function used for checking the liveness of the application. Returns true if the application
// is running and false if it is not. The liveness probe is typically used to indicate if
// the application is in a state where it can handle requests (e.g., the server is up and running).
//
// Optional. Default: func(c *fiber.Ctx) bool { return true }
LivenessProbe HealthChecker
// HTTP endpoint at which the liveness probe will be available.
//
// Optional. Default: "/livez"
LivenessEndpoint string
// Function used for checking the readiness of the application. Returns true if the application
// is ready to process requests and false otherwise. The readiness probe typically checks if all necessary
// services, databases, and other dependencies are available for the application to function correctly.
//
// Optional. Default: func(c *fiber.Ctx) bool { return true }
ReadinessProbe HealthChecker
// HTTP endpoint at which the readiness probe will be available.
// Optional. Default: "/readyz"
ReadinessEndpoint string
}
默认配置
此中间件使用的默认配置定义如下
func defaultLivenessProbe(*fiber.Ctx) bool { return true }
func defaultReadinessProbe(*fiber.Ctx) bool { return true }
var ConfigDefault = Config{
LivenessProbe: defaultLivenessProbe,
ReadinessProbe: defaultReadinessProbe,
LivenessEndpoint: "/livez",
ReadinessEndpoint: "/readyz",
}