package util import ( elastic "app.yhyue.com/moapp/jybase/esv7" "app.yhyue.com/moapp/jybase/mongodb" "app.yhyue.com/moapp/jybase/mysql" "app.yhyue.com/moapp/jybase/redis" "app.yhyue.com/moapp/jyfs/rpc/filesystemclient" "github.com/zeromicro/go-zero/core/discov" "github.com/zeromicro/go-zero/zrpc" "gorm.io/gorm" "log" "strings" ) type MysqlDBConfig struct { DriverName string DataSourceName string MaxOpenConn int MaxIdleConn int MaxConnLifeTime int } type EsConfig struct { Addr string Pool int UserName string Password string } type RedisConfig struct { Addr []string } type MongoConfig struct { Main Mongo Logger Mongo } type Mongo struct { Address string `json:"address"` Size int `json:"size"` DbName string `json:"dbName"` UserName string `json:"userName,optional"` Password string `json:"password,optional"` } var ( jyDocsDB *gorm.DB FileSystem filesystemclient.FileSystem mgoLog *mongodb.MongodbSim mgoMain *mongodb.MongodbSim ) func InitDB(url, driverName string, maxOpenConn, maxIdle int) { jyDocsDB = mysql.GormMysql(url, driverName, maxOpenConn, maxIdle, nil) if jyDocsDB != nil { log.Printf("----------->【jy_docs】 DB :[%s] 初始化成功!<--------------", url) } else { log.Fatalf("----------->【jy_docs】 DB初始化失败<--------------") } } func InitEs(addr string, poolSize int, userName, password string) { log.Printf("----------->【jy_docs】 elastic :[%s] init<--------------", addr) elastic.InitElasticSizeByAuth(addr, poolSize, userName, password) } func InitOss(etcd discov.EtcdConf) { client := zrpc.MustNewClient(zrpc.RpcClientConf{ Etcd: etcd, }) FileSystem = filesystemclient.NewFileSystem(client) } func InitRedis(rc RedisConfig) { //初始化 redis if len(rc.Addr) > 0 { log.Println("--初始化 redis--") redis.InitRedisBySize(strings.Join(rc.Addr, ","), 100, 30, 300) } } func InitMongo(mgo MongoConfig) { if mgo.Main.Address != "" { log.Println("--初始化 mongodb main --") mgoMain = &mongodb.MongodbSim{ MongodbAddr: mgo.Main.Address, Size: mgo.Main.Size, DbName: mgo.Main.DbName, UserName: mgo.Main.UserName, Password: mgo.Main.Password, } mgoMain.InitPool() } if mgo.Logger.Address != "" { log.Println("--初始化 mongodb log--") mgoLog = &mongodb.MongodbSim{ MongodbAddr: mgo.Logger.Address, Size: mgo.Logger.Size, DbName: mgo.Logger.DbName, UserName: mgo.Logger.UserName, Password: mgo.Logger.Password, } mgoLog.InitPool() } } func GetJyMgoLoggerDB() *mongodb.MongodbSim { return mgoLog } func GetJyMgoMainDB() *mongodb.MongodbSim { return mgoMain } func GetJyDocsDB() *gorm.DB { return jyDocsDB }