Updata:解决冲突
This commit is contained in:
parent
83e9d8266d
commit
da7d185455
@ -7,6 +7,7 @@ import (
|
|||||||
"fonchain-fiee/pkg/service/account"
|
"fonchain-fiee/pkg/service/account"
|
||||||
"fonchain-fiee/pkg/service/asChat"
|
"fonchain-fiee/pkg/service/asChat"
|
||||||
"fonchain-fiee/pkg/service/auth"
|
"fonchain-fiee/pkg/service/auth"
|
||||||
|
"fonchain-fiee/pkg/service/bundle"
|
||||||
"fonchain-fiee/pkg/service/file"
|
"fonchain-fiee/pkg/service/file"
|
||||||
"fonchain-fiee/pkg/service/governance"
|
"fonchain-fiee/pkg/service/governance"
|
||||||
imports "fonchain-fiee/pkg/service/import"
|
imports "fonchain-fiee/pkg/service/import"
|
||||||
@ -215,6 +216,10 @@ func NewRouter() *gin.Engine {
|
|||||||
importRoute.GET("generate/photo/test2", imports.Test2)
|
importRoute.GET("generate/photo/test2", imports.Test2)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
//健康检测
|
||||||
|
v1.GET("health", bundle.HealthCheck)
|
||||||
|
}
|
||||||
//静态文件
|
//静态文件
|
||||||
r.StaticFS("/api/fiee/static", http.Dir("./runtime"))
|
r.StaticFS("/api/fiee/static", http.Dir("./runtime"))
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
|
|||||||
@ -7,10 +7,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"fonchain-fiee/api/bundle"
|
"fonchain-fiee/api/bundle"
|
||||||
"fonchain-fiee/api/cast"
|
"fonchain-fiee/api/cast"
|
||||||
|
"fonchain-fiee/pkg/cache"
|
||||||
"fonchain-fiee/pkg/service"
|
"fonchain-fiee/pkg/service"
|
||||||
"fonchain-fiee/pkg/service/bundle/common"
|
"fonchain-fiee/pkg/service/bundle/common"
|
||||||
"fonchain-fiee/pkg/utils"
|
"fonchain-fiee/pkg/utils"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -577,3 +579,69 @@ func GetAccountBundleBalance(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
service.Success(c, result)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user