123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package util
- import (
- "app.yhyue.com/moapp/jybase/es"
- 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"
- "google.golang.org/grpc"
- "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 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
- esV7 es.Es
- )
- 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) {
- grpcOpts := []zrpc.ClientOption{
- zrpc.WithDialOption(grpc.WithWriteBufferSize(40 * 1024 * 1024)),
- zrpc.WithDialOption(grpc.WithReadBufferSize(40 * 1024 * 1024)),
- zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(40 * 1024 * 1024))), // 设置接收消息的最大字节数,这里也是40MB
- zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(40 * 1024 * 1024))), // 设置发送消息的最大字节数,这里是40MB
- }
- conf := zrpc.RpcClientConf{
- Etcd: etcd,
- }
- client := zrpc.MustNewClient(conf, grpcOpts...)
- FileSystem = filesystemclient.NewFileSystem(client)
- }
- func InitRedis(rc []string) {
- //初始化 redis
- if len(rc) > 0 {
- log.Printf("----------->【jy_docs】 redis :[%s] init<--------------", strings.Join(rc, ","))
- redis.InitRedisBySize(strings.Join(rc, ","), 100, 30, 300)
- }
- }
- func InitMongo(mgo MongoConfig) {
- if mgo.Main.Address != "" {
- log.Printf("----------->【jy_docs】 mongodb main:[%s] init<--------------", mgo.Main.Address)
- 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.Printf("----------->【jy_docs】 mongodb log:[%s] init<--------------", mgo.Logger.Address)
- 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()
- }
- }
- // esv7
- func InitESV7(addr string, poolSize int, userName, password string) {
- if addr != "" {
- log.Printf("----------->【jy_docs】 elasticSearch :[%s] init<--------------", addr)
- esV7 = &es.EsV7{
- Address: addr,
- UserName: userName,
- Password: password,
- Size: poolSize,
- }
- esV7.Init()
- }
- }
- func GetJyMgoLoggerDB() *mongodb.MongodbSim {
- return mgoLog
- }
- func GetJyMgoMainDB() *mongodb.MongodbSim {
- return mgoMain
- }
- func GetJyDocsDB() *gorm.DB {
- return jyDocsDB
- }
- func GetESV7DB() es.Es {
- return esV7
- }
|