gateway/core/nacos.go

82 lines
2.0 KiB
Go
Raw Normal View History

2021-09-07 13:40:44 +08:00
package core
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"
"io/ioutil"
"net/http"
"strings"
)
type nacosClient struct {
client naming_client.INamingClient
config config_client.IConfigClient
}
func NewNacosClient(c naming_client.INamingClient, n config_client.IConfigClient) nacosClient {
return nacosClient{c, n}
}
// Remote 发起远程调用
func (n nacosClient) Remote(ctx *gin.Context) {
// 取出URI
uri := ctx.Request.RequestURI
// 截取第二个/前面那一段
pathAry := strings.Split(uri, "/")
2021-09-07 17:12:29 +08:00
var service serviceMap
2021-09-07 13:40:44 +08:00
apiService := fmt.Sprintf("/%v", pathAry[1])
for _, s := range ServiceMap {
if s.Path != "" && s.Path == apiService {
2021-09-07 17:12:29 +08:00
service = s
2021-09-07 13:40:44 +08:00
break
}
}
2021-09-07 17:12:29 +08:00
if service.Service == "" {
2021-09-07 13:40:44 +08:00
ctx.String(http.StatusNotFound, "服务不可达")
return
}
2021-09-07 17:12:29 +08:00
if !service.Enable {
ctx.String(http.StatusServiceUnavailable, "系统维护中")
return
}
2021-09-07 13:40:44 +08:00
instance, err := n.client.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
2021-09-07 17:12:29 +08:00
ServiceName: service.Service,
2021-09-07 13:40:44 +08:00
})
if err != nil {
2021-09-07 17:12:29 +08:00
Log.Error("获取健康服务失败: %v\n", err.Error())
2021-09-07 13:40:44 +08:00
return
}
// 组装内部接口
apiUrl := fmt.Sprintf("http://%v:%v%v", instance.Ip, instance.Port, uri)
// 创建Request对象
req, err := http.NewRequest(ctx.Request.Method, apiUrl, ctx.Request.Body)
if err != nil {
2021-09-07 17:12:29 +08:00
Log.Error("请求异常: %v\n", err)
2021-09-07 13:40:44 +08:00
return
}
// 填充Form参数(大概率用不上)
req.Form = ctx.Request.Form
// 填充链路追踪标记
req.Header.Add("uber-trace-id", ctx.GetString("UberTraceId"))
// 请求ID
req.Header.Add("X-Request-Id", ctx.GetString("X-Request-Id"))
// 设置来源
req.Header.Add("From", "internal")
// 发起请求
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
2021-09-07 17:12:29 +08:00
Log.Error("请求异常: %v\n", err)
2021-09-07 13:40:44 +08:00
return
}
// 返回结果到前端
sss, _ := ioutil.ReadAll(response.Body)
2021-09-07 17:12:29 +08:00
Log.Debug(string(sss))
2021-09-07 13:40:44 +08:00
ctx.String(response.StatusCode, string(sss))
}