浏览代码

优化配置

xyxie 11 月之前
父节点
当前提交
5a8107f08d
共有 7 个文件被更改,包括 172 次插入60 次删除
  1. 0 9
      main.go
  2. 11 29
      models/mgodb/db.go
  3. 6 6
      models/mgodb/edb_data_base.go
  4. 6 6
      models/mgodb/edb_data_calculate.go
  5. 16 10
      utils/config.go
  6. 36 0
      utils/logs.go
  7. 97 0
      utils/mgodb/mgo_config.go

+ 0 - 9
main.go

@@ -1,10 +1,7 @@
 package main
 
 import (
-	"context"
-	"eta/eta_forum_hub/models/mgodb"
 	_ "eta/eta_forum_hub/routers"
-	"fmt"
 	beego "github.com/beego/beego/v2/server/web"
 )
 
@@ -15,10 +12,4 @@ func main() {
 	}
 
 	beego.Run()
-	defer func() {
-		fmt.Println("mongodb disconnect")
-		if err := mgodb.MgoClient.Disconnect(context.TODO()); err != nil {
-			panic(err)
-		}
-	}()
 }

+ 11 - 29
models/mgodb/db.go

@@ -1,39 +1,21 @@
 package mgodb
 
 import (
-	"context"
+	"encoding/json"
 	"eta/eta_forum_hub/utils"
-	"fmt"
-	"go.mongodb.org/mongo-driver/mongo"
-	"go.mongodb.org/mongo-driver/mongo/options"
-	"go.mongodb.org/mongo-driver/mongo/readpref"
-	"time"
+	"eta/eta_forum_hub/utils/mgodb"
 )
 
-var MgoClient *mongo.Client
-
 func init() {
-	var err error
-	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
-	defer cancel()
-	credential := options.Credential{
-		AuthMechanism: utils.MONGODB_CREDENTIAL,
-		Username:      "hzeta",
-		Password:      "hzeta2023",
-	}
-	clientOpts := options.Client().ApplyURI(utils.MONGODB_URL).SetAuth(credential)
-	clientOpts.SetMinPoolSize(0)
-	clientOpts.SetMaxPoolSize(10)
-	clientOpts.SetConnectTimeout(10 * time.Second)
+	if utils.MgoUrlData != `` {
+		var mgoConfig mgodb.MgoConfig
+		if e := json.Unmarshal([]byte(utils.MgoUrlData), &mgoConfig); e != nil {
+			panic("mongodb链接失败,Err:" + e.Error())
+			return
+		}
 
-	MgoClient, err = mongo.Connect(context.TODO(), clientOpts)
-	if err != nil {
-		panic(err)
+		mgoCli := mgodb.NewMgoClient(mgoConfig)
+		utils.MgoDataCli = mgoCli
+		utils.MgoDataDbName = mgoConfig.Database
 	}
-
-	if err = MgoClient.Ping(ctx, readpref.Primary()); err != nil {
-		panic(err)
-	}
-
-	fmt.Println("Connected to MongoDB!")
 }

+ 6 - 6
models/mgodb/edb_data_base.go

@@ -29,7 +29,7 @@ type EdbInfoSearchData struct {
 
 func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
 	findOptions := options.Find()
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	filter := bson.D{{"edb_info_id", edbInfoId}}
 	ctx := context.TODO()
 	cur, err := db.Find(filter, findOptions)
@@ -57,7 +57,7 @@ func GetEdbDataBaseByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error)
 
 func GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
 	findOptions := options.Find()
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	filter := bson.D{{"edb_code", edbCode}}
 	ctx := context.TODO()
 	cur, err := db.Find(filter, findOptions)
@@ -84,7 +84,7 @@ func GetEdbDataBaseByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
 }
 
 func InsertEdbDataBatch(items []interface{}) (err error) {
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	_, err = db.InsertMany(items)
 	if err != nil {
 		return
@@ -95,7 +95,7 @@ func InsertEdbDataBatch(items []interface{}) (err error) {
 func ModifyEdbDataEdbInfoId(edbInfoId int64, edbCode string) (err error) {
 	filter := bson.D{{"edb_code", edbCode}}
 	update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now()}}}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	_, err = db.UpdateMany(filter, update)
 	if err != nil {
 		return
@@ -106,7 +106,7 @@ func ModifyEdbDataEdbInfoId(edbInfoId int64, edbCode string) (err error) {
 // 删除
 func DeleteEdbInfoDataByEdbInfoId(edbInfoId int) (err error) {
 	filter := bson.D{{"edb_info_id", edbInfoId}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	_, err = db.DeleteMany(filter)
 	return
 }
@@ -115,7 +115,7 @@ func DeleteEdbInfoDataByEdbInfoId(edbInfoId int) (err error) {
 func ModifyValueEdbDataValue(edbDataId primitive.ObjectID, value float64) (err error) {
 	filter := bson.D{{"_id", edbDataId}}
 	update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now()}}}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_base", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
 	_, err = db.UpdateOne(filter, update)
 	return
 }

+ 6 - 6
models/mgodb/edb_data_calculate.go

@@ -11,7 +11,7 @@ import (
 
 func GetEdbDataCalculateByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err error) {
 	findOptions := options.Find()
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	filter := bson.D{{"edb_info_id", edbInfoId}}
 	ctx := context.TODO()
 	cur, err := db.Find(filter, findOptions)
@@ -39,7 +39,7 @@ func GetEdbDataCalculateByEdbInfoId(edbInfoId int) (items []*EdbDataBase, err er
 
 func GetEdbDataCalculateByEdbCode(edbCode string) (items []*EdbDataBase, err error) {
 	findOptions := options.Find()
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	filter := bson.D{{"edb_code", edbCode}}
 	ctx := context.TODO()
 	cur, err := db.Find(filter, findOptions)
@@ -66,7 +66,7 @@ func GetEdbDataCalculateByEdbCode(edbCode string) (items []*EdbDataBase, err err
 }
 
 func InsertEdbCalculateDataBatch(items []interface{}) (err error) {
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	_, err = db.InsertMany(items)
 	if err != nil {
 		return
@@ -77,7 +77,7 @@ func InsertEdbCalculateDataBatch(items []interface{}) (err error) {
 func ModifyEdbCalculateDataEdbInfoId(edbInfoId int64, edbCode string) (err error) {
 	filter := bson.D{{"edb_code", edbCode}}
 	update := bson.D{{"$set", bson.D{{"edb_info_id", edbInfoId}, {"modify_time", time.Now()}}}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	_, err = db.UpdateMany(filter, update)
 	if err != nil {
 		return
@@ -88,7 +88,7 @@ func ModifyEdbCalculateDataEdbInfoId(edbInfoId int64, edbCode string) (err error
 // 删除
 func DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId int) (err error) {
 	filter := bson.D{{"edb_info_id", edbInfoId}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	_, err = db.DeleteMany(filter)
 	return
 }
@@ -97,7 +97,7 @@ func DeleteEdbInfoCalculateDataByEdbInfoId(edbInfoId int) (err error) {
 func ModifyValueEdbCalculateDataValue(edbDataId primitive.ObjectID, value float64) (err error) {
 	filter := bson.D{{"_id", edbDataId}}
 	update := bson.D{{"$set", bson.D{{"value", value}, {"modify_time", time.Now()}}}}
-	db := NewMgo(utils.MONGODB_COMMUNITY, "edb_data_calculate", MgoClient)
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
 	_, err = db.UpdateOne(filter, update)
 	return
 }

+ 16 - 10
utils/config.go

@@ -4,18 +4,23 @@ import (
 	"fmt"
 	beeLogger "github.com/beego/bee/v2/logger"
 	"github.com/beego/beego/v2/server/web"
+	"go.mongodb.org/mongo-driver/mongo"
 	"strconv"
 )
 
 var (
-	RunMode            string //运行模式
-	APP_NAME_CN        string
-	MYSQL_URL          string //数据库连接
-	MONGODB_URL        string //mongodb连接
+	RunMode     string //运行模式
+	APP_NAME_CN string
+	MYSQL_URL   string //数据库连接
+	/*MONGODB_URL        string //mongodb连接
 	MONGODB_COMMUNITY  string //mongodb库名
-	MONGODB_CREDENTIAL string
-	Re                 error       //redis错误
-	Rc                 RedisClient //redis缓存
+	MONGODB_CREDENTIAL string*/
+	Re error       //redis错误
+	Rc RedisClient //redis缓存
+
+	MgoUrlData    string        // mongodb数据库连接配置
+	MgoDataCli    *mongo.Client // mongodb客户端连接
+	MgoDataDbName string        // mongodb指标数据的库名
 )
 
 // ES配置
@@ -91,9 +96,10 @@ func init() {
 	}
 	APP_NAME_CN = appNameCn
 	MYSQL_URL = config["mysql_url"]
-	MONGODB_URL = config["mongodb_url"]
-	MONGODB_COMMUNITY = config["mongodb_community"]
-	MONGODB_CREDENTIAL = config["mongodb_credential"]
+
+	// mongodb数据库连接配置
+	MgoUrlData = config["mgo_url_data"]
+
 	// 公共api内部服务调用
 	{
 		// 公共指标库相关

+ 36 - 0
utils/logs.go

@@ -18,6 +18,7 @@ var FileLog *logs.BeeLogger
 var ApiLog *logs.BeeLogger
 var FileLogData *logs.BeeLogger
 var Binlog *logs.BeeLogger
+var MongoLog *logs.BeeLogger
 
 func init() {
 	if LogMaxDays == 0 {
@@ -46,6 +47,7 @@ func init() {
 	initBinlog()
 	initApiLog()
 	initFileLogData()
+	initMgoLog()
 }
 
 type logConfig struct {
@@ -128,6 +130,40 @@ func initFileLogData() {
 	FileLogData.EnableFuncCallDepth(true)
 }
 
+// initMgoLog
+// @Description: mongo日志
+// @author: Roc
+// @datetime 2024-05-06 16:42:47
+func initMgoLog() {
+	//mgo日志
+
+	var logPath string
+	{
+		binlogPath := BinLogPath
+		if binlogPath == "" {
+			binlogPath = DefaultBinlogPath
+		}
+		logPath = path.Dir(binlogPath)
+	}
+	if logPath == `` {
+		logPath = "./etalogs"
+	}
+
+	mongoLogPath := logPath + "/mongolog"
+	mongoLogFile := "mongolog.log"
+
+	os.MkdirAll(mongoLogPath, os.ModePerm)
+	logFileName := path.Join(mongoLogPath, mongoLogFile)
+	MongoLog = logs.NewLogger(1000000)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	//logConf.MaxLines = 10000000
+	//logConf.Rotate = true
+	b, _ := json.Marshal(logConf)
+	MongoLog.SetLogger(logs.AdapterFile, string(b))
+	MongoLog.EnableFuncCallDepth(true)
+}
 func getDefaultLogConfig() logConfig {
 	return logConfig{
 		FileName: "",

+ 97 - 0
utils/mgodb/mgo_config.go

@@ -0,0 +1,97 @@
+package mgodb
+
+import (
+	"context"
+	"eta/eta_forum_hub/utils"
+	"fmt"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/event"
+	"go.mongodb.org/mongo-driver/mongo"
+	"go.mongodb.org/mongo-driver/mongo/options"
+	"go.mongodb.org/mongo-driver/mongo/readpref"
+	"sync"
+	"time"
+)
+
+var (
+	MaxPoolSize = uint64(50)
+	MinPoolSize = uint64(0)
+)
+
+//func init() {
+//	fmt.Println("start MgoNewClient")
+//	MgoNewClient()
+//	fmt.Println("end MgoNewClient")
+//}
+
+type MgoConfig struct {
+	Url           string `json:"url"`
+	Username      string `json:"username"`
+	Password      string `json:"password"`
+	AuthMechanism string `json:"auth_mechanism"`
+	Database      string `json:"database"`
+}
+
+func NewMgoClient(mgoConfig MgoConfig) *mongo.Client {
+	var err error
+	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+	defer cancel()
+	credential := options.Credential{
+		AuthMechanism: mgoConfig.AuthMechanism,
+		Username:      mgoConfig.Username,
+		Password:      mgoConfig.Password,
+	}
+	clientOpts := options.Client().ApplyURI(mgoConfig.Url).SetAuth(credential)
+	startedCommands := sync.Map{} // map[int64]bson.Raw
+
+	cmdMonitor := &event.CommandMonitor{
+		Started: func(_ context.Context, evt *event.CommandStartedEvent) {
+			startedCommands.Store(evt.RequestID, evt.Command)
+			//startedCommands[evt.RequestID] = evt.Command
+		},
+		Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
+			//log.Printf("Command: %v Reply: %v\n",
+			//	startedCommands[evt.RequestID],
+			//	evt.Reply,
+			//)
+			var commands bson.Raw
+			v, ok := startedCommands.Load(evt.RequestID)
+			if ok {
+				commands = v.(bson.Raw)
+			}
+			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] %v \n", float64(evt.Duration)/1e6, commands, evt.Reply)
+		},
+		Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
+			//log.Printf("Command: %v Failure: %v\n",
+			//	startedCommands[evt.RequestID],
+			//	evt.Failure,
+			//)
+			var commands bson.Raw
+			v, ok := startedCommands.Load(evt.RequestID)
+			if ok {
+				commands = v.(bson.Raw)
+			}
+			utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
+		},
+	}
+
+	// 创建options
+
+	clientOpts.SetMonitor(cmdMonitor)
+
+	clientOpts.SetMinPoolSize(MinPoolSize)
+	clientOpts.SetMaxPoolSize(MaxPoolSize)
+	clientOpts.SetConnectTimeout(10 * time.Second)
+
+	mgoClient, err := mongo.Connect(context.TODO(), clientOpts)
+	if err != nil {
+		panic(err)
+	}
+
+	if err = mgoClient.Ping(ctx, readpref.Primary()); err != nil {
+		panic(err)
+	}
+
+	fmt.Println("Connected to MongoDB!")
+	return mgoClient
+}