李哲 4 жил өмнө
parent
commit
3fc6bf7678

+ 3 - 1
rpc/userlib/etc/userlib.yaml

@@ -7,7 +7,7 @@ Etcd:
 FileSystemConf:
   Etcd:
     Hosts:
-      - 127.0.0.1:2380
+      - 127.0.0.1:2379
     Key: integral.rpc
 JyDocsMysqlDB:
   DriverName: "mysql"
@@ -15,3 +15,5 @@ JyDocsMysqlDB:
   MaxOpenConn: 20
   MaxIdleConn: 10
   MaxConnLifeTime: 100
+Callee: "文库"
+Node: "1"

+ 2 - 0
rpc/userlib/internal/config/config.go

@@ -9,6 +9,8 @@ type Config struct {
 	zrpc.RpcServerConf
 	JyDocsMysqlDB  jyDocRpcUtil.MysqlDBConfig
 	FileSystemConf zrpc.RpcClientConf
+	Callee			string
+	Node			string
 }
 
 var   Configs Config

+ 1 - 1
rpc/userlib/test/userLib_test.go

@@ -19,7 +19,7 @@ func Test_UserDocCollect(t *testing.T) {
 	})
 	userLib := userlibclient.NewUserLib(client)
 	resp, err := userLib.DocCollect(context.Background(), &userlibclient.UserCollectRequest{
-		DocId:  "SUgJfBt1M6nop8kvwcu5",
+		DocId:  "010ef774-888c-11eb-8699-0050568f51e7",
 		UserId: "5d6378301c298a5aac7b5402",
 		AppId:  "10000",
 	})

+ 97 - 4
rpc/userlib/userlib.go

@@ -1,11 +1,16 @@
 package main
 
 import (
+	"app.yhyue.com/moapp/jy_docs/services/model"
+	userLibService "app.yhyue.com/moapp/jy_docs/services/userlib"
 	jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
 	"context"
+	"encoding/json"
 	"flag"
 	"fmt"
 	"log"
+	"strconv"
+	"time"
 
 	"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/config"
 	"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/server"
@@ -16,7 +21,6 @@ import (
 	"github.com/tal-tech/go-zero/zrpc"
 	"google.golang.org/grpc"
 )
-
 var configFile = flag.String("f", "etc/userlib.yaml", "the config file")
 func main() {
 	flag.Parse()
@@ -36,8 +40,97 @@ func main() {
 		log.Fatal("【jy_docs】 read config error!")
 	}
 }
+
 func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
-	fmt.Println("拦截器")
-	log.Println("方法名:",info.FullMethod,"  参数:",req)
-	return handler(ctx, req)
+	jsonStr := Strval(req)
+	var dat map[string]interface{}
+	if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
+		fmt.Println("==============json str 转map=======================")
+	}
+	resp, err = handler(ctx, req)
+	/*fmt.Println("接口名称:",info.FullMethod)
+	fmt.Println("被调用者身份:",config.Configs.Callee)
+	fmt.Println("调用者身份:",dat["appId"])
+	fmt.Println("入参:",req)
+	fmt.Println("返回信息:",resp)
+	fmt.Println("节点:",config.Configs.Node)
+	fmt.Println("摘要:",resp)
+	fmt.Println("时间戳:",time.Now().Unix())*/
+	res := fmt.Sprint(resp)
+	nameRune := []rune(res)
+	if len(res) > 255 {
+		res = string(nameRune[:255])
+	}
+	data := &model.InterfaceLog{}
+	data.InterName = info.FullMethod
+	data.CalleeId = config.Configs.Callee
+	data.AppId = fmt.Sprint(dat["appId"])
+	data.InParameter = fmt.Sprint(req)
+	data.ReturnInfo = res
+	data.Node = config.Configs.Node
+	data.Summary = res
+	data.Timestamp = time.Now()
+	flag := userLibService.InterfaceLog(data)
+	if flag {
+		log.Println("接口调用日志记录成功")
+	}
+	return resp,err
+}
+
+// Strval 获取变量的字符串值
+// 浮点型 3.0将会转换成字符串3, "3"
+// 非数值或字符类型的变量将会被转换成JSON格式字符串
+func Strval(value interface{}) string {
+	var key string
+	if value == nil {
+		return key
+	}
+
+	switch value.(type) {
+	case float64:
+		ft := value.(float64)
+		key = strconv.FormatFloat(ft, 'f', -1, 64)
+	case float32:
+		ft := value.(float32)
+		key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
+	case int:
+		it := value.(int)
+		key = strconv.Itoa(it)
+	case uint:
+		it := value.(uint)
+		key = strconv.Itoa(int(it))
+	case int8:
+		it := value.(int8)
+		key = strconv.Itoa(int(it))
+	case uint8:
+		it := value.(uint8)
+		key = strconv.Itoa(int(it))
+	case int16:
+		it := value.(int16)
+		key = strconv.Itoa(int(it))
+	case uint16:
+		it := value.(uint16)
+		key = strconv.Itoa(int(it))
+	case int32:
+		it := value.(int32)
+		key = strconv.Itoa(int(it))
+	case uint32:
+		it := value.(uint32)
+		key = strconv.Itoa(int(it))
+	case int64:
+		it := value.(int64)
+		key = strconv.FormatInt(it, 10)
+	case uint64:
+		it := value.(uint64)
+		key = strconv.FormatUint(it, 10)
+	case string:
+		key = value.(string)
+	case []byte:
+		key = string(value.([]byte))
+	default:
+		newValue, _ := json.Marshal(value)
+		key = string(newValue)
+	}
+
+	return key
 }

+ 15 - 0
services/model/stdlib.go

@@ -107,3 +107,18 @@ type DocStatistics struct {
 func (ud *DocStatistics) TableName() string {
 	return "doc_statistics"
 }
+
+type InterfaceLog struct {
+	InterName   string    `json:"interName"  gorm:"column:interName"`
+	CalleeId    string    `json:"calleeId" gorm:"column:calleeId"`
+	AppId       string    `json:"appId" gorm:"column:appId"`
+	InParameter string    `json:"inParameter" gorm:"column:inParameter"`
+	ReturnInfo  string    `json:"returnInfo" gorm:"column:returnInfo"`
+	Node        string    `json:"node" gorm:"column:node"`
+	Summary     string    `json:"summary" gorm:"column:summary"`
+	Timestamp   time.Time `json:"timestamp" gorm:"column:timestamp"`
+}
+
+func (ud *InterfaceLog) TableName() string {
+	return "interface_log"
+}

+ 18 - 1
services/userlib/userDocService.go

@@ -377,5 +377,22 @@ func UserDocsList(in *userlib.UserDocsRequest) ([]*model.UserDoc, int64, bool, s
 		return data, count, false, msg
 	}
 	return data, count, true, msg
-
 }
+
+/*接口日志添加*/
+func InterfaceLog(in *model.InterfaceLog) bool {
+	orm := docRpcUtil.GetJyDocsDB()
+	err := orm.Transaction(func(tx *gorm.DB) error {
+		err := orm.Create(in)
+		if err.Error != nil {
+			log.Println("接口日志添加失败:", err)
+			tx.Rollback()
+			return err.Error
+		}
+		return nil
+	})
+	if err != nil {
+		return false
+	}
+	return true
+}