From feb62f12b7dcab87b75b28ba27a07355805d006d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 9 Sep 2021 11:46:04 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BD=91=E5=85=B3=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=B5=B0=E9=9B=86=E7=BE=A4=E5=86=85=E9=83=A8=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E8=BD=AC=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config.go | 22 +++++++++++++++------- core/core.go | 2 +- core/nacos.go | 27 +++++++++++++++++---------- core/service.go | 9 +++++---- middleware/jaeger.go | 10 +++++++--- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/core/config.go b/core/config.go index 3d9aab2..e178ff5 100644 --- a/core/config.go +++ b/core/config.go @@ -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 diff --git a/core/core.go b/core/core.go index aa20304..2f52c9a 100644 --- a/core/core.go +++ b/core/core.go @@ -2,6 +2,6 @@ package core var ( NacosClient nacosClient - ServiceMap []serviceMap + ServiceMap []serviceItem Log Logger ) diff --git a/core/nacos.go b/core/nacos.go index 9412e1d..8719902 100644 --- a/core/nacos.go +++ b/core/nacos.go @@ -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 { diff --git a/core/service.go b/core/service.go index b27fb9e..ac9e91e 100644 --- a/core/service.go +++ b/core/service.go @@ -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} } diff --git a/middleware/jaeger.go b/middleware/jaeger.go index b699f7f..acea6ec 100644 --- a/middleware/jaeger.go +++ b/middleware/jaeger.go @@ -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) }