修改完善nacos熔断机制
This commit is contained in:
@@ -10,8 +10,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
cli naming_client.INamingClient
|
||||
groupName string
|
||||
cli naming_client.INamingClient
|
||||
groupName string
|
||||
ServerUriCache = make(map[string]model.Instance)
|
||||
)
|
||||
|
||||
// NewNacosRegistry 创建一个nacos注册中心
|
||||
@@ -85,16 +86,18 @@ func DiscoverServiceList(serviceName string) ([]model.Instance, error) {
|
||||
|
||||
// DiscoverService 发现一个服务
|
||||
func DiscoverService(serviceName string) (model.Instance, error) {
|
||||
ServiceCache := ServerUriCache[serviceName]
|
||||
client, err := NewNacosRegistry()
|
||||
if err != nil {
|
||||
return model.Instance{}, err
|
||||
return ServiceCache, err
|
||||
}
|
||||
instances, err := (*client).SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
|
||||
ServiceName: serviceName,
|
||||
GroupName: groupName,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Instance{}, err
|
||||
return ServiceCache, err
|
||||
}
|
||||
ServerUriCache[serviceName] = *instances
|
||||
return *instances, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response 通用响应结构
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// 常见的响应代码
|
||||
const (
|
||||
SuccessCode = 200
|
||||
ErrorCode = 500
|
||||
BadRequestCode = 400
|
||||
NotFoundCode = 404
|
||||
UnauthorizedCode = 401
|
||||
)
|
||||
|
||||
// Success 生成成功响应
|
||||
func Success(c *app.RequestContext, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: SuccessCode,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Error 生成错误响应
|
||||
func Error(c *app.RequestContext, code int, message string) {
|
||||
c.JSON(code, Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
// BadRequest 生成400响应
|
||||
func BadRequest(c *app.RequestContext, message string) {
|
||||
Error(c, BadRequestCode, message)
|
||||
}
|
||||
|
||||
// NotFound 生成404响应
|
||||
func NotFound(c *app.RequestContext, message string) {
|
||||
Error(c, NotFoundCode, message)
|
||||
}
|
||||
|
||||
// Unauthorized 生成401响应
|
||||
func Unauthorized(c *app.RequestContext, message string) {
|
||||
Error(c, UnauthorizedCode, message)
|
||||
}
|
||||
|
||||
// FileResponse 生成文件响应
|
||||
func FileResponse(c *app.RequestContext, filePath, fileName string) {
|
||||
// 设置响应头,告诉浏览器这是一个下载文件的请求
|
||||
c.Response.Header.Set("Content-Disposition", "attachment; filename="+fileName)
|
||||
c.Response.Header.Set("Content-Type", "application/octet-stream")
|
||||
|
||||
// 使用 File() 直接发送文件
|
||||
c.File(filePath)
|
||||
}
|
||||
@@ -16,7 +16,10 @@ import (
|
||||
func getApiServer() string {
|
||||
service, err := nacos.DiscoverService("apiServer")
|
||||
if err != nil {
|
||||
loger.Error("getApiServer error", err)
|
||||
loger.Error("获取服务器地址失败", err)
|
||||
if service.Ip != "" {
|
||||
return service.Ip + ":" + strconv.Itoa(int(service.Port))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return service.Ip + ":" + strconv.Itoa(int(service.Port))
|
||||
|
||||
Reference in New Issue
Block a user