huangrf 5 лет назад
Родитель
Сommit
c8fa260d51
2 измененных файлов с 14 добавлено и 5 удалено
  1. 2 1
      middleware/logger_cassandra.cql
  2. 12 4
      middleware/logger_cassandra.go

+ 2 - 1
middleware/logger_cassandra.cql

@@ -2,6 +2,7 @@
  * 创建轨迹表
  */
 CREATE TABLE log_info (
+    app_name text,
     req_date text,
     req_time text,
     req_method text,
@@ -16,7 +17,7 @@ CREATE TABLE log_info (
     resp_msg text,
 	resp_data text,
 	cost_time double,
-    primary key(req_date, req_time)
+    primary key((app_name, req_date), req_time)
 ) WITH CLUSTERING ORDER BY (req_time DESC)
        AND COMPACTION = {'class': 'TimeWindowCompactionStrategy',
                      'compaction_window_unit': 'DAYS',

+ 12 - 4
middleware/logger_cassandra.go

@@ -4,14 +4,18 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
+	"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
 	"github.com/gin-gonic/gin"
 	"github.com/gocql/gocql"
+	"strings"
 	"time"
 )
 
 var __logInfoChan chan *logInfo
 // cassandra 配置
 var _clusterCfg *gocql.ClusterConfig = nil
+// app名称
+var __appName string = ""
 
 func init(){
 	defer func() {
@@ -20,10 +24,14 @@ func init(){
 		}
 	}()
 
+	// 设置app名称
+	__appName = config.AppConfig.GetKey("app_name")
+
 	__logInfoChan = make(chan *logInfo, 1000)
 
 	// 设置cassandar 配置
-	_clusterCfg = gocql.NewCluster("218.14.81.38")
+	cassandra := config.AppConfig.GetKey("cassandra")
+	_clusterCfg = gocql.NewCluster(strings.Split(cassandra, ",")...)
 	_clusterCfg.Keyspace = "i2_log"
 	_clusterCfg.Consistency = gocql.Quorum
 	//设置连接池的数量,默认是2个(针对每一个host,都建立起NumConns个连接)
@@ -152,10 +160,10 @@ func insertLogInfo(logInfo *logInfo){
 	// 保存轨迹
 	fmt.Println("保存轨迹")
 	cqlformat := `insert into 
-		log_info(req_date, req_time, req_method, req_url, req_proto, req_ua, req_referer, req_post_data, resp_time, resp_body, resp_code, resp_msg, resp_data, cost_time) 
-		values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
+		log_info(app_name, req_date, req_time, req_method, req_url, req_proto, req_ua, req_referer, req_post_data, resp_time, resp_body, resp_code, resp_msg, resp_data, cost_time) 
+		values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
 	reqDate := logInfo.ReqTime.Format("20060102")
-	q := session.Query(cqlformat, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
+	q := session.Query(cqlformat, __appName, reqDate, logInfo.ReqTime.Format("2006-01-02 15:04:05"), logInfo.ReqMethod, logInfo.ReqUrl, logInfo.ReqProto, logInfo.ReqUa, logInfo.ReqReferer, logInfo.ReqPostData, logInfo.RespTime.Format("2006-01-02 15:04:05"), logInfo.RespBody, logInfo.RespCode, logInfo.RespMsg, logInfo.RespData, logInfo.CostTime)
 	err = q.Exec()		// 应该可以使用批量插入
 	if err != nil{
 		fmt.Println("插入日志错误:", err)