From 901cdfeb5c277e65b95f14d2b673ad705576d578 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Thu, 6 Nov 2025 16:31:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=81=A5=E5=BA=B7=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design/health-check-routing/index.md | 11 +++++ internal/app/api/api.go | 4 ++ internal/app/api/router.go | 7 ++- .../controller/health/health_controller.go | 47 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 design/health-check-routing/index.md create mode 100644 internal/app/controller/health/health_controller.go diff --git a/design/health-check-routing/index.md b/design/health-check-routing/index.md new file mode 100644 index 0000000..e149e02 --- /dev/null +++ b/design/health-check-routing/index.md @@ -0,0 +1,11 @@ +# 问题 + +增加健康检查路由, 供docker/k8s健康探测服务使用 + +## issue + +http://git.huangwc.com/pig/pig-farm-controller/issues/48 + +# 方案 + +增加三个对应路由 \ No newline at end of file diff --git a/internal/app/api/api.go b/internal/app/api/api.go index c2f7165..a858f7c 100644 --- a/internal/app/api/api.go +++ b/internal/app/api/api.go @@ -20,6 +20,7 @@ import ( _ "git.huangwc.com/pig/pig-farm-controller/docs" // 引入 swag 生成的 docs "git.huangwc.com/pig/pig-farm-controller/internal/app/controller/device" + "git.huangwc.com/pig/pig-farm-controller/internal/app/controller/health" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller/management" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller/monitor" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller/plan" @@ -51,6 +52,7 @@ type API struct { pigFarmController *management.PigFarmController // 猪场管理控制器实例 pigBatchController *management.PigBatchController // 猪群控制器实例 monitorController *monitor.Controller // 数据监控控制器实例 + healthController *health.Controller // 健康检查控制器实例 listenHandler webhook.ListenHandler // 设备上行事件监听器 analysisTaskManager *domain_plan.AnalysisPlanTaskManager // 计划触发器管理器实例 } @@ -102,6 +104,8 @@ func NewAPI(cfg config.ServerConfig, pigBatchController: management.NewPigBatchController(logs.AddCompName(baseCtx, "PigBatchController"), pigBatchService), // 在 NewAPI 中初始化数据监控控制器 monitorController: monitor.NewController(logs.AddCompName(baseCtx, "MonitorController"), monitorService), + // 在 NewAPI 中初始化健康检查控制器 + healthController: health.NewController(logs.AddCompName(baseCtx, "HealthController")), } api.setupRoutes() // 设置所有路由 diff --git a/internal/app/api/router.go b/internal/app/api/router.go index bf94dbd..cc09cb4 100644 --- a/internal/app/api/router.go +++ b/internal/app/api/router.go @@ -20,6 +20,11 @@ func (a *API) setupRoutes() { // --- Public Routes --- // 这些路由不需要身份验证 + // 健康检查路由 + a.echo.GET("/healthz", a.healthController.Healthz) + a.echo.GET("/readyz", a.healthController.Readyz) + logger.Debug("公开接口注册成功:健康检查") + // 用户注册和登录 a.echo.POST("/api/v1/users", a.userController.CreateUser) // 注册新用户 a.echo.POST("/api/v1/users/login", a.userController.Login) // 用户登录 @@ -169,7 +174,7 @@ func (a *API) setupRoutes() { monitorGroup.GET("/pending-collections", a.monitorController.ListPendingCollections) monitorGroup.GET("/user-action-logs", a.monitorController.ListUserActionLogs) monitorGroup.GET("/raw-material-purchases", a.monitorController.ListRawMaterialPurchases) - monitorGroup.GET("/raw-material-stock-logs", a.monitorController.ListRawMaterialStockLogs) + monitorGroup.GET("raw-material-stock-logs", a.monitorController.ListRawMaterialStockLogs) monitorGroup.GET("/feed-usage-records", a.monitorController.ListFeedUsageRecords) monitorGroup.GET("/medication-logs", a.monitorController.ListMedicationLogs) monitorGroup.GET("/pig-batch-logs", a.monitorController.ListPigBatchLogs) diff --git a/internal/app/controller/health/health_controller.go b/internal/app/controller/health/health_controller.go new file mode 100644 index 0000000..398301e --- /dev/null +++ b/internal/app/controller/health/health_controller.go @@ -0,0 +1,47 @@ +package health + +import ( + "context" + + "git.huangwc.com/pig/pig-farm-controller/internal/app/controller" + + "github.com/labstack/echo/v4" +) + +// Controller 结构体定义了健康检查控制器及其依赖。 +type Controller struct { + ctx context.Context +} + +// NewController 创建并返回一个新的健康检查控制器实例。 +func NewController(ctx context.Context) *Controller { + return &Controller{ + ctx: ctx, + } +} + +// Healthz 是一个简单的健康检查端点,用于存活探针 (Liveness Probe)。 +// 它也适用于 Docker 的 HEALTHCHECK 指令。 +// @Summary 服务存活检查 +// @Description 检查服务进程是否运行正常,只要服务能响应就返回 200 OK。 +// @Tags Health +// @Produce json +// @Success 200 {object} controller.Response "服务存活" +// @Router /healthz [get] +func (c *Controller) Healthz(ctx echo.Context) error { + // 使用项目统一的响应函数,不记录日志 + return controller.SendResponse(ctx, controller.CodeSuccess, "服务存活", nil) +} + +// Readyz 是一个简单的就绪检查端点,用于就绪探针 (Readiness Probe)。 +// @Summary 服务就绪检查 +// @Description 检查服务是否已准备好接收流量。当前实现为只要服务能响应即代表就绪。 +// @Tags Health +// @Produce json +// @Success 200 {object} controller.Response "服务已就绪" +// @Router /readyz [get] +func (c *Controller) Readyz(ctx echo.Context) error { + // TODO: 在未来,这里应该检查所有关键依赖(如数据库、外部服务)的可用性。 + // 使用项目统一的响应函数,不记录日志 + return controller.SendResponse(ctx, controller.CodeSuccess, "服务已就绪", nil) +}