// Package mysql ... // // Description : mysql客户端 // // Author : go_developer@163.com<白茶清欢> // // Date : 2021-03-01 9:20 下午 package mysql import ( "fmt" "git.zhangdeman.cn/zhangdeman/gopkg/logger/wrapper" "github.com/gin-gonic/gin" "go.uber.org/zap" "git.zhangdeman.cn/zhangdeman/gopkg/logger" gormLogger "gorm.io/gorm/logger" "gorm.io/driver/mysql" "gorm.io/gorm" ) // NewDBClient ... // // Author : go_developer@163.com<白茶清欢> // // Date : 3:09 PM 2021/12/24 func NewDBClient(masterConf *DBConfig, slaveConf *DBConfig, logConf *LogConfig, loggerInstance *zap.Logger, extraRequestFieldList []string) (*DBClient, error) { client := &DBClient{ extraFieldList: extraRequestFieldList, } var err error // 日志初始化失败 if client.loggerInstance, err = getLogInstance(logConf, loggerInstance); nil != err { return nil, err } if client.master, err = GetDatabaseClient(masterConf, nil); nil != err { return nil, err } if client.slave, err = GetDatabaseClient(slaveConf, nil); nil != err { return nil, err } return client, nil } // DBClient 包装日志实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 3:09 PM 2021/12/24 type DBClient struct { loggerInstance *zap.Logger master *gorm.DB slave *gorm.DB extraFieldList []string } // GetMaster 获取主库连接 // // Author : go_developer@163.com<白茶清欢> // // Date : 3:28 PM 2021/12/24 func (dc *DBClient) GetMaster(ctx *gin.Context) *gorm.DB { session := dc.master.Session(&gorm.Session{}) session.Logger = dc.getLogger(ctx, session, "slave") return session } // GetSlave 获取从库链接 // // Author : go_developer@163.com<白茶清欢> // // Date : 3:29 PM 2021/12/24 func (dc *DBClient) GetSlave(ctx *gin.Context) *gorm.DB { session := dc.slave.Session(&gorm.Session{}) session.Logger = dc.getLogger(ctx, session, "slave") return session } // getLogger 获取日志实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 3:45 PM 2021/12/24 func (dc *DBClient) getLogger(ctx *gin.Context, dbClient *gorm.DB, node string) gormLogger.Interface { return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, node, dc.extraFieldList) } // getLogInstance 获取日志实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 3:20 PM 2021/12/24 func getLogInstance(logConf *LogConfig, loggerInstance *zap.Logger) (*zap.Logger, error) { if nil != loggerInstance { return loggerInstance, nil } logConfList := []logger.SetLoggerOptionFunc{logger.WithEncoder(logConf.Encoder), logger.WithCallerSkip(logConf.Skip), logger.WithCaller()} if logConf.ConsoleOutput { logConfList = append(logConfList, logger.WithConsoleOutput()) } var ( err error ) if loggerInstance, err = logger.NewLogger(logConf.Level, logConf.SplitConfig, logConfList...); nil != err { return nil, err } return loggerInstance, nil } // GetDatabaseClient 获取日志实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 10:49 下午 2021/3/1 func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) { var ( instance *gorm.DB err error loggerInstance *zap.Logger ) if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err { return nil, err } if nil != logConf { if loggerInstance, err = getLogInstance(logConf, nil); nil != err { return nil, err } instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, loggerInstance, "", nil) } return instance, nil } // buildConnectionDSN 构建建立连接的DSN // // Author : go_developer@163.com<白茶清欢> // // Date : 9:34 下午 2021/3/1 func buildConnectionDSN(conf *DBConfig) string { return fmt.Sprintf( "%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", conf.Username, conf.Password, conf.Host, conf.Port, conf.Database, conf.Charset, ) }