网关新增走集群内部网络转发
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
李寻欢 2021-09-09 11:46:04 +08:00
parent 4fe735ce0d
commit feb62f12b7
5 changed files with 45 additions and 25 deletions

View File

@ -6,14 +6,22 @@ import (
type Config struct {
Gateway struct {
Routes []struct {
Path string `yaml:"path"`
Service string `yaml:"service"`
Enable bool `yaml:"enable"`
} `yaml:"routes"`
Routes []Route `yaml:"routes"`
} `yaml:"gateway"`
}
type Route struct {
Path string `yaml:"path"`
Service string `yaml:"service"`
Enable bool `yaml:"enable"`
Lb Lb `yaml:"lb"`
}
type Lb struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
func ConfigChanged(data string) {
// 从配置文件解析为对象
config := Config{}
@ -23,9 +31,9 @@ func ConfigChanged(data string) {
return
}
rs := config.Gateway.Routes
var sma []serviceMap
var sma []serviceItem
for _, r := range rs {
item := NewServiceMapItem(r.Path, r.Service, r.Enable)
item := NewServiceMapItem(r.Path, r.Service, r.Enable, r.Lb)
sma = append(sma, item)
}
ServiceMap = sma

View File

@ -2,6 +2,6 @@ package core
var (
NacosClient nacosClient
ServiceMap []serviceMap
ServiceMap []serviceItem
Log Logger
)

View File

@ -26,7 +26,7 @@ func (n nacosClient) Remote(ctx *gin.Context) {
uri := ctx.Request.RequestURI
// 截取第二个/前面那一段
pathAry := strings.Split(uri, "/")
var service serviceMap
var service serviceItem
apiService := fmt.Sprintf("/%v", pathAry[1])
for _, s := range ServiceMap {
if s.Path != "" && s.Path == apiService {
@ -43,17 +43,24 @@ func (n nacosClient) Remote(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, "系统维护中")
return
}
instance, err := n.client.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: service.Service,
})
if err != nil {
Log.Error("获取健康服务失败: %v\n", err.Error())
ctx.String(http.StatusBadGateway, "服务异常: %v", err.Error())
return
var apiHost string
// 判断是否是走集群内部负载
if service.Lb.Host != "" {
apiHost = fmt.Sprintf("%s:%v", service.Lb.Host, service.Lb.Port)
} else {
instance, err := n.client.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: service.Service,
})
if err != nil {
Log.Error("获取健康服务失败: %v\n", err.Error())
ctx.String(http.StatusBadGateway, "服务异常: %v", err.Error())
return
}
apiHost = fmt.Sprintf("%s:%v", instance.Ip, instance.Port)
}
// 组装内部接口
apiUrl := fmt.Sprintf("http://%v:%v%v", instance.Ip, instance.Port, uri)
apiUrl := fmt.Sprintf("http://%v%v", apiHost, uri)
// 创建Request对象
req, err := http.NewRequest(ctx.Request.Method, apiUrl, ctx.Request.Body)
if err != nil {

View File

@ -1,16 +1,17 @@
package core
type serviceMap struct {
type serviceItem struct {
Path string
Service string
Enable bool
Lb Lb
}
func NewServiceMapItem(k, v string, e bool) serviceMap {
//var sma []serviceMap
func NewServiceMapItem(k, v string, e bool, lb Lb) serviceItem {
//var sma []serviceItem
// 从Nacos读取规则
// gateway:
// - path: /user
// service: mb-user-service
return serviceMap{k, v, e}
return serviceItem{k, v, e, lb}
}

View File

@ -38,11 +38,15 @@ func OpenTracing() gin.HandlerFunc {
asd := fmt.Sprintf("%v", parentSpan.Context())
c.Set("UberTraceId", asd)
c.Set("ParentSpanContext", parentSpan.Context())
// 设置接口地址
ext.HTTPUrl.Set(parentSpan, c.Request.RequestURI)
// 设置请求方式
ext.HTTPMethod.Set(parentSpan, c.Request.Method)
// 执行后续操作
c.Next()
// 设置返回状态码
ext.HTTPStatusCode.Set(parentSpan, uint16(c.Writer.Status()))
// 如果状态码大于299设置错误标签
if c.Writer.Status() > 299 {
ext.Error.Set(parentSpan, true)
}