From da7d185455fcb01687824980ecef548800cc669a Mon Sep 17 00:00:00 2001 From: "jiaji.H" Date: Tue, 6 Jan 2026 10:14:13 +0800 Subject: [PATCH] =?UTF-8?q?Updata:=E8=A7=A3=E5=86=B3=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/router/router.go | 5 +++ pkg/service/bundle/bundleBalance.go | 68 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/pkg/router/router.go b/pkg/router/router.go index 21d5e8c..6429412 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -7,6 +7,7 @@ import ( "fonchain-fiee/pkg/service/account" "fonchain-fiee/pkg/service/asChat" "fonchain-fiee/pkg/service/auth" + "fonchain-fiee/pkg/service/bundle" "fonchain-fiee/pkg/service/file" "fonchain-fiee/pkg/service/governance" imports "fonchain-fiee/pkg/service/import" @@ -215,6 +216,10 @@ func NewRouter() *gin.Engine { importRoute.GET("generate/photo/test2", imports.Test2) } + { + //健康检测 + v1.GET("health", bundle.HealthCheck) + } //静态文件 r.StaticFS("/api/fiee/static", http.Dir("./runtime")) r.NoRoute(func(c *gin.Context) { diff --git a/pkg/service/bundle/bundleBalance.go b/pkg/service/bundle/bundleBalance.go index d191279..9316ea4 100644 --- a/pkg/service/bundle/bundleBalance.go +++ b/pkg/service/bundle/bundleBalance.go @@ -7,10 +7,12 @@ import ( "fmt" "fonchain-fiee/api/bundle" "fonchain-fiee/api/cast" + "fonchain-fiee/pkg/cache" "fonchain-fiee/pkg/service" "fonchain-fiee/pkg/service/bundle/common" "fonchain-fiee/pkg/utils" "io" + "net/http" "strconv" "strings" "time" @@ -577,3 +579,69 @@ func GetAccountBundleBalance(c *gin.Context) { }) service.Success(c, result) } + +// HealthCheck 健康检测接口 +func HealthCheck(c *gin.Context) { + healthStatus := gin.H{ + "status": "ok", + "timestamp": time.Now().Unix(), + "checks": make(map[string]interface{}), + } + + // 检查 Redis 连接 + redisStatus := "ok" + redisErr := "" + if cache.RedisClient != nil { + _, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + _, err := cache.RedisClient.Ping().Result() + if err != nil { + redisStatus = "failed" + redisErr = err.Error() + } + } else { + redisStatus = "failed" + redisErr = "Redis client is nil" + } + healthStatus["checks"].(map[string]interface{})["redis"] = gin.H{ + "status": redisStatus, + "error": redisErr, + } + + // 检查微服务连接(调用一个简单的服务方法) + serviceStatus := "ok" + serviceErr := "" + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + // 调用 BundleProvider 的简单方法进行健康检测 + _, err := service.BundleProvider.GetBundleBalanceByUserId(ctx, &bundle.GetBundleBalanceByUserIdReq{ + UserId: 0, // 使用一个不存在的用户ID,只检测服务连通性 + }) + if err != nil { + // 检查是否是超时或连接错误 + if ctx.Err() == context.DeadlineExceeded { + serviceStatus = "timeout" + serviceErr = "Service timeout" + } else { + // 其他错误(如业务错误)认为服务是可用的 + serviceStatus = "ok" + serviceErr = "" + } + } + + healthStatus["checks"].(map[string]interface{})["microservice"] = gin.H{ + "status": serviceStatus, + "error": serviceErr, + } + + // 如果所有检查都通过,返回 200,否则返回 503 + httpStatus := http.StatusOK + if redisStatus != "ok" || serviceStatus != "ok" { + httpStatus = http.StatusServiceUnavailable + healthStatus["status"] = "degraded" + } + + c.JSON(httpStatus, healthStatus) + service.Success(c, healthStatus) +}