Forráskód Böngészése

Merge branch 'master' of https://app.yhyue.com/moapp/jyPoints

李哲 4 éve
szülő
commit
092c40c847
3 módosított fájl, 83 hozzáadás és 2 törlés
  1. 3 1
      rpc/etc/integral.yaml
  2. 78 1
      rpc/integral.go
  3. 2 0
      rpc/internal/config/config.go

+ 3 - 1
rpc/etc/integral.yaml

@@ -14,4 +14,6 @@ FileSystemConf:
   Etcd:
     Hosts:
       - 127.0.0.1:2379
-    Key: integral.rpc
+    Key: integral.rpc
+CalleeId: integral.rpc
+Node: 1

+ 78 - 1
rpc/integral.go

@@ -8,6 +8,7 @@ import (
 	"app.yhyue.com/moapp/jyPoints/rpc/internal/svc"
 	"app.yhyue.com/moapp/jyPoints/service"
 	"context"
+	"encoding/json"
 	"flag"
 	"fmt"
 	_ "github.com/go-sql-driver/mysql"
@@ -18,6 +19,7 @@ import (
 	"golang.org/x/time/rate"
 	"google.golang.org/grpc"
 	"log"
+	"strconv"
 	"time"
 )
 
@@ -37,6 +39,7 @@ func main() {
 		integral.RegisterIntegralServer(grpcServer, srv)
 	})
 	s.AddUnaryInterceptors(rateLimitInterceptor)
+	s.AddUnaryInterceptors()
 	defer s.Stop()
 	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
 	b := cron.New()
@@ -60,7 +63,81 @@ func init() {
 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)
+	resp, err = handler(ctx, req)
+	fmt.Println("返回值",resp,err)
+	orm := entity.Engine.NewSession()
+/*	reqData := req.(map[string]interface{})
+	appId:=reqData["AppId"]*/
+	var dat map[string]interface{}
+	jsonStr := Strval(req)
+	if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
+		fmt.Println(dat)
+	}
+	appId:=dat["appId"]
+	eqspStr := Strval(resp)
+	if(len(eqspStr)>300){
+		eqspStr=eqspStr[0:300]
+	}
+	var sqlerr error
+	_,sqlerr = orm.Exec("INSERT INTO `interface_log`(`interName`, `calleeId`, `appId`, `inParameter`, `reTurnInfo`, `node`, `summary`,timestamp) VALUES (?,?,?,?,?,?,?,now())",info.FullMethod,c.CalleeId,appId,jsonStr,eqspStr,c.Node,"")
+	if sqlerr != nil {
+		log.Print("日志存储失败", sqlerr)
+	}
+	return resp,err
+}
+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
 }
 
 func timeDask()  {

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

@@ -10,5 +10,7 @@ type Config struct {
 	FileSystemConf zrpc.RpcClientConf
 	DataSource     string          // 手动代码
 	Cache          cache.CacheConf // 手动代码
+	Node           int             // 节点
+	CalleeId       string          // 服务名字
 	TimeSource	   string			//定时任务
 }