网关新增走集群内部网络转发
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,12 +6,20 @@ import (
type Config struct { type Config struct {
Gateway struct { Gateway struct {
Routes []struct { Routes []Route `yaml:"routes"`
} `yaml:"gateway"`
}
type Route struct {
Path string `yaml:"path"` Path string `yaml:"path"`
Service string `yaml:"service"` Service string `yaml:"service"`
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
} `yaml:"routes"` Lb Lb `yaml:"lb"`
} `yaml:"gateway"` }
type Lb struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
} }
func ConfigChanged(data string) { func ConfigChanged(data string) {
@ -23,9 +31,9 @@ func ConfigChanged(data string) {
return return
} }
rs := config.Gateway.Routes rs := config.Gateway.Routes
var sma []serviceMap var sma []serviceItem
for _, r := range rs { 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) sma = append(sma, item)
} }
ServiceMap = sma ServiceMap = sma

View File

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

View File

@ -26,7 +26,7 @@ func (n nacosClient) Remote(ctx *gin.Context) {
uri := ctx.Request.RequestURI uri := ctx.Request.RequestURI
// 截取第二个/前面那一段 // 截取第二个/前面那一段
pathAry := strings.Split(uri, "/") pathAry := strings.Split(uri, "/")
var service serviceMap var service serviceItem
apiService := fmt.Sprintf("/%v", pathAry[1]) apiService := fmt.Sprintf("/%v", pathAry[1])
for _, s := range ServiceMap { for _, s := range ServiceMap {
if s.Path != "" && s.Path == apiService { if s.Path != "" && s.Path == apiService {
@ -43,7 +43,11 @@ func (n nacosClient) Remote(ctx *gin.Context) {
ctx.String(http.StatusServiceUnavailable, "系统维护中") ctx.String(http.StatusServiceUnavailable, "系统维护中")
return 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{ instance, err := n.client.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: service.Service, ServiceName: service.Service,
}) })
@ -52,8 +56,11 @@ func (n nacosClient) Remote(ctx *gin.Context) {
ctx.String(http.StatusBadGateway, "服务异常: %v", err.Error()) ctx.String(http.StatusBadGateway, "服务异常: %v", err.Error())
return 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对象 // 创建Request对象
req, err := http.NewRequest(ctx.Request.Method, apiUrl, ctx.Request.Body) req, err := http.NewRequest(ctx.Request.Method, apiUrl, ctx.Request.Body)
if err != nil { if err != nil {

View File

@ -1,16 +1,17 @@
package core package core
type serviceMap struct { type serviceItem struct {
Path string Path string
Service string Service string
Enable bool Enable bool
Lb Lb
} }
func NewServiceMapItem(k, v string, e bool) serviceMap { func NewServiceMapItem(k, v string, e bool, lb Lb) serviceItem {
//var sma []serviceMap //var sma []serviceItem
// 从Nacos读取规则 // 从Nacos读取规则
// gateway: // gateway:
// - path: /user // - path: /user
// service: mb-user-service // 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()) asd := fmt.Sprintf("%v", parentSpan.Context())
c.Set("UberTraceId", asd) c.Set("UberTraceId", asd)
c.Set("ParentSpanContext", parentSpan.Context()) c.Set("ParentSpanContext", parentSpan.Context())
// 设置接口地址
ext.HTTPUrl.Set(parentSpan, c.Request.RequestURI)
// 设置请求方式
ext.HTTPMethod.Set(parentSpan, c.Request.Method)
// 执行后续操作
c.Next() c.Next()
// 设置返回状态码
ext.HTTPStatusCode.Set(parentSpan, uint16(c.Writer.Status())) ext.HTTPStatusCode.Set(parentSpan, uint16(c.Writer.Status()))
// 如果状态码大于299设置错误标签
if c.Writer.Status() > 299 { if c.Writer.Status() > 299 {
ext.Error.Set(parentSpan, true) ext.Error.Set(parentSpan, true)
} }