فهرست منبع

change log table to casscadra

unknown 7 سال پیش
والد
کامیت
06b97a2f64
3فایلهای تغییر یافته به همراه58 افزوده شده و 27 حذف شده
  1. 49 26
      src/apiservice/tools/log.go
  2. 5 1
      src/config.json
  3. 4 0
      src/main.go

+ 49 - 26
src/apiservice/tools/log.go

@@ -1,12 +1,15 @@
 package tools
 
 import (
+	"encoding/json"
 	"log"
 	"net"
 	"net/http"
 	"strings"
 	"sync"
 	"time"
+	uc "ucbsutil"
+	ca "ucbsutil/cassandra"
 )
 
 //对map的同步
@@ -28,13 +31,23 @@ func init() {
 //定时保存日志
 func SaveLogTask() {
 	lock.Lock()
-	if len(arr) >= 1 {
+	/*if len(arr) >= 1 {
 		tmp := arr
 		arr = make([]map[string]interface{}, 0)
 		go func() {
 			log.Println("timer..save..log", len(tmp))
 			MQFW.SaveBulk("log", tmp...)
 		}()
+	}*/
+	if len(arr) >= 1 {
+		tmp := arr
+		arr = make([]map[string]interface{}, 0)
+		go func() {
+			success := ca.SaveBatch("jy_openapi", tmp)
+			if success {
+				log.Println("timer..visit..log", len(tmp))
+			}
+		}()
 	}
 	lock.Unlock()
 	time.AfterFunc(1*time.Minute, SaveLogTask)
@@ -47,40 +60,50 @@ func AddLog(req *http.Request, username, msg string, code, apicount int) {
 	ref := req.Referer()
 	s_url := req.RequestURI
 	date := timeNow.Unix()
+	md, _ := json.Marshal(req.Form)
+	str := string(md)
 	logs := map[string]interface{}{
-		"l_date":      date,
-		"s_ip":        req.Proto,
-		"s_refer":     ref,
-		"i_year":      timeNow.Year(),
-		"i_month":     timeNow.Month(),
-		"i_day":       timeNow.Day(),
-		"i_hour":      timeNow.Hour(),
-		"i_minutes":   timeNow.Minute(),
-		"s_describe":  req.Form,
-		"s_client":    agent,
-		"s_os":        GetOS(agent),
-		"s_browse":    GetBrowse(agent),
-		"s_method":    req.Method,
-		"s_url":       s_url,
-		"s_username":  username,
-		"s_keyword":   req.FormValue("keyword"),
-		"s_appid":     req.FormValue("appid"),
-		"s_rMsg":      msg,
-		"s_signature": req.FormValue("signature"),
-		"s_action":    req.FormValue("action"),
-		"s_pagenum":   req.FormValue("pagenum"),
-		"s_timestamp": req.FormValue("timestamp"),
-		"i_rCode":     code,
-		"i_apicount":  apicount,
+		"id":        uc.DayShortTime(),
+		"date":      date,
+		"ip":        req.Proto,
+		"refer":     ref,
+		"year":      timeNow.Year(),
+		"month":     timeNow.Month(),
+		"day":       timeNow.Day(),
+		"hour":      timeNow.Hour(),
+		"minutes":   timeNow.Minute(),
+		"mdescribe": str,
+		"client":    agent,
+		"os":        GetOS(agent),
+		"browse":    GetBrowse(agent),
+		"method":    req.Method,
+		"url":       s_url,
+		"uname":     username,
+		"keyword":   req.FormValue("keyword"),
+		"appid":     req.FormValue("appid"),
+		"rmsg":      msg,
+		"signature": req.FormValue("signature"),
+		"action":    req.FormValue("action"),
+		"pagenum":   req.FormValue("pagenum"),
+		"timestamp": req.FormValue("timestamp"),
+		"rcode":     code,
+		"apicount":  apicount,
 	}
 	lock.Lock()
 	arr = append(arr, logs)
 	if len(arr) >= nc {
 		tmp := arr
 		arr = make([]map[string]interface{}, 0)
-		go func() {
+		/*go func() {
 			log.Println("save..article..log", len(tmp))
 			MQFW.SaveBulk("log", tmp...)
+		}()*/
+
+		go func() {
+			success := ca.SaveBatch("jy_openapi", tmp)
+			if success {
+				log.Println("save..article..log", len(tmp))
+			}
 		}()
 	}
 	lock.Unlock()

+ 5 - 1
src/config.json

@@ -7,5 +7,9 @@
     "redisaddrs": "jyOpenAPI=192.168.3.18:3380",
 	"redispool":50,
     "webport": "8081",
-	"jyaddress":"https://www.jianyu360.com"
+	"jyaddress":"https://www.jianyu360.com",
+	"cassandra": [
+        "192.168.3.18"
+    ],
+    "cassandrasize":5
 }

+ 4 - 0
src/main.go

@@ -8,6 +8,8 @@ import (
 	"qfw/util/elastic"
 	"qfw/util/redis"
 
+	ca "ucbsutil/cassandra"
+
 	"github.com/go-xweb/xweb"
 )
 
@@ -15,6 +17,8 @@ func init() {
 	initXweb()
 	redis.InitRedisBySize(Sysconfig["redisaddrs"].(string), util.IntAllDef(Sysconfig["redispool"], 10), 20, 240)
 	elastic.InitElasticSize(Sysconfig["elasticsearch"].(string), util.IntAllDef(Sysconfig["elasticPoolSize"], 30))
+	//初始化cassandra
+	ca.InitCassandra("jianyu", (int)(Sysconfig["cassandrasize"].(float64)), util.ObjArrToStringArr(Sysconfig["cassandra"].([]interface{})))
 }
 
 func main() {