Selaa lähdekoodia

wip:接口对接修改

wangkaiyue 2 vuotta sitten
vanhempi
commit
fd52e39080

+ 1 - 1
core/proxy/broker/broker.go

@@ -55,7 +55,7 @@ func (r *broker) GetServerAddr(code, ip string) (*url.URL, error) {
 	defer r.RUnlock()
 	module, ok := r.addresses[code]
 	if !ok {
-		return nil, NewErrorWithCode(GATEWAY_MODULE_UNDEFINED, fmt.Sprintf("获取服务异常 module %s", code))
+		return nil, NewErrorWithCode(GATEWAY_MODULE_UNDEFINED, fmt.Sprintf("获取服务地址异常 module %s", code))
 	}
 	return module.Get(ip)
 }

+ 45 - 28
core/proxy/middleware/filterFuncs.go

@@ -4,7 +4,8 @@ import (
 	. "bp.jydev.jianyu360.cn/BaseService/gateway/common/gatecode"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/proxy/rpc"
 	"bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
-	"bp.jydev.jianyu360.cn/BaseService/gateway/core/util"
+	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/util/gconv"
 	"strings"
@@ -63,52 +64,68 @@ func filterBefore(r *ghttp.Request) error {
 // 同步处理,若扣减异常,接口返回错误信息。
 func filterAfter(r *ghttp.Request) (err error) {
 	ctx := router.GetGContext(r.GetCtx())
+	if r.GetError() != nil {
+		return
+	}
 	rule := ctx.RouterRule
 	// 响应完成,判断是否扣除
 	if rule.Deduct != 0 {
-		respJson, err := GetResponseJsonAndClear(r, "resourceNum", "resourceIds")
-		if err != nil {
-			//获取资源扣减异常
-			return err
-		}
-
-		if code, ok := respJson["error_code"]; !ok || gconv.Int64(code) != 0 {
-			//扣减数据结构异常
-			return nil
-		}
-
 		var ids []string
 		var deductNum int64 = 1
 		if rule.Deduct == 2 || rule.Deduct == -2 { //后端应用
-			deductNum = gconv.Int64(respJson["resourceNum"])
-			ids = strings.Split(gconv.String(respJson["resourceIds"]), ",")
+			var respErr error
+			deductNum, ids, respErr = GetResponseJsonAndClear(r)
+			if respErr != nil { //扣减接口异常 不修改接口内容
+				g.Log().Error(r.Context(), respErr)
+			}
 		} else if rule.Deduct != 1 {
 			deductNum = gconv.Int64(rule.Deduct)
 		}
 
-		if rule.Deduct > 0 { //扣减
-			if err = rpc.ResourcePowerDeduct(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
-				return err
-			}
-		} else { //充值
-			if err = rpc.ResourcePowerRecharge(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
-				return err
+		if deductNum > 0 { //资源接口异常
+			if rule.Deduct > 0 { //扣减
+				fmt.Println("扣减", deductNum, ids)
+				if err = rpc.ResourcePowerDeduct(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
+					return err
+				}
+			} else { //充值
+				fmt.Println("充值", deductNum, ids)
+				if err = rpc.ResourcePowerRecharge(ctx.Sess.EntId, ctx.Sess.NewUid, rule.FuncCode, rule.AppId, deductNum, ids); err != nil {
+					return err
+				}
 			}
 		}
+
 	}
 	return nil
 }
 
 // GetResponseJsonAndClear 获取接口返回参数,并删除扣减预留字段
-func GetResponseJsonAndClear(r *ghttp.Request, deleteFiled ...string) (respJson map[string]interface{}, err error) {
-	err = util.ChangeResponse(r, func(b []byte) error {
-		respJson = gconv.Map(b)
-		for _, field := range deleteFiled {
+func GetResponseJsonAndClear(r *ghttp.Request) (deductNum int64, ids []string, err error) {
+	respJson := gconv.Map(r.Response.Buffer())
+	defer func() {
+		r.Response.ClearBuffer()
+		r.Response.WriteJson(respJson)
+		r.Response.Header().Set("Content-Length", gconv.String(len(r.Response.Buffer())))
+		g.Log().Debugf(r.Context(), "填充 %s 相应头长度%s", gconv.String(respJson), r.Response.Header().Get("Content-Length"))
+	}()
+	g.Log().Debugf(r.Context(), "获取 %s 相应头长度%s", gconv.String(respJson), r.Response.Header().Get("Content-Length"))
+
+	//仅接口返回成功后 进行资源操作
+	if code, exists := respJson["error_code"]; !(exists && code == 0) {
+		err = fmt.Errorf("扣减接口返回异常 %v", gconv.String(respJson))
+	}
+
+	deductNum = gconv.Int64(respJson["resourceNum"])
+	if idStr := gconv.String(respJson["resourceIds"]); idStr != "" {
+		ids = strings.Split(idStr, ",")
+	}
+
+	for _, field := range []string{"resourceNum", "resourceIds"} {
+		if _, ok := respJson[field]; ok {
 			delete(respJson, field)
 		}
-		b = gconv.Bytes(respJson)
-		return nil
-	})
+	}
 	return
 }
 

+ 1 - 0
core/proxy/middleware/filterHandler.go

@@ -14,6 +14,7 @@ func FilterHandler(r *ghttp.Request) {
 		r.SetError(beforeErr)
 		return
 	}
+
 	// 业务逻辑
 	r.Middleware.Next()
 

+ 28 - 18
etc/config.yaml

@@ -22,21 +22,20 @@ etcd:
     userCenter:
       key: usercenter.rpc
       address:
-        - 192.168.3.206:2379
+        - 192.168.3.240:2379
     # 资源中台配置
     resourceCenter:
       key: resource.rpc
       address:
-        - 192.168.3.206:2379
+        - 192.168.3.240:2379
 
 #数据库配置
 databases:
   # redis配置
-  redis: session=192.168.3.11:1713    # 用户获取剑鱼程序用户session
+  redis: session=192.168.3.206:1712    # 用户获取剑鱼程序用户session
   polyredis:
-    address: 192.168.3.11:1713
+    address: 192.168.3.206:1712
     poolsize: 30
-
   # mysql配置
   mysql:
     dbName: base_service
@@ -45,6 +44,7 @@ databases:
     passWord: =PDT49#80Z!RVv52_z
     maxOpenConns: 5
     maxIdleConns: 5
+
   # jianyu mysql配置
   mysqlJy:
     dbName: jianyu
@@ -61,13 +61,21 @@ databases:
     dbName: qfw
     replSet:
 
+  # mongo 日志配置
+  mogLog:
+    address: 192.168.3.206:27090
+    size: 5
+    dbName: qfw
+    replSet:
+    userName: admin
+    password: 123456
 
 # 系统配置
 system:
-  routerTable: front_proxy
+  routerTable: front_proxy_dev
   # 监听服务注册etcd配置
   etcdListen:
-    - 192.168.3.206:2379
+    - 192.168.3.240:2379
 
   response:
     head-clear: # 响应头删除
@@ -86,15 +94,17 @@ proxy:
 
 #日志配置
 logger:
-  level: "PROD"                # ALL, DEV, PROD
+  level: "ALL"                # ALL, DEV, PROD
   default:
+    level: "ALL"
     file: "{Y-m-d}.log"
     rotateExpire: 10
     rotateBackupLimit: 10
     stdout: true
+    stStatus: 0
     path: "./logs/system"
   reqLog:
-    level: "PROD"
+    level: "ALL"
     saveToDb: false
     path: "./logs/request"
     file: "{Y-m-d}.log"        # 日志文件格式
@@ -103,12 +113,12 @@ logger:
     rotateBackupExpire: 60s    # 切分文件的过期时间为7d,即七天后会自动删除该切分文件
     rotateBackupCompress: 0   # 滚动切分文件的压缩比(0-9)。默认为0,表示不压缩
 
-# 系统告警
-alarm:
-  isOpen: true                        # 异常通知开关。默���关闭
-  address: 192.168.3.207:4150         # nsq消息通知地址。默认关闭
-  toppic: jyalert                     # 消息管道
-  isJsonEncode: false                 # 是否加密
-  id: pre_alert                       # id
-  title: 你有新的告警消息处理            # 消息标题
-  text: 前置代理告警请查看               # 消息正文
+  # 系统告警
+  alarm:
+    isOpen: false                        # 异常通知开关。默认关闭
+    address: 192.168.3.207:4150         # nsq消息通知地址。默认关闭
+    toppic: jyalert                     # 消息管道
+    isJsonEncode: false                 # 是否加密
+    id: pre_alert                       # id
+    title: 你有新的告警消息处理            # 消息标题
+    text: 前置代理告警请查看               # 消息正文