package database import ( "fmt" "git.bvbej.com/bvbej/base-golang/pkg/errors" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" "log" "os" "time" ) var _ MysqlRepo = (*mysqlRepo)(nil) type MysqlRepo interface { i() GetRead() *gorm.DB GetWrite() *gorm.DB Close() error } type MySQLConfig struct { Read struct { Addr string `yaml:"addr"` User string `yaml:"user"` Pass string `yaml:"pass"` Name string `yaml:"name"` } `yaml:"read"` Write struct { Addr string `yaml:"addr"` User string `yaml:"user"` Pass string `yaml:"pass"` Name string `yaml:"name"` } `yaml:"write"` Base struct { MaxOpenConn int `yaml:"maxOpenConn"` //最大连接数 MaxIdleConn int `yaml:"maxIdleConn"` //最大空闲连接数 ConnMaxLifeTime time.Duration `yaml:"connMaxLifeTime"` //最大连接超时(分钟) } `yaml:"base"` } type mysqlRepo struct { read *gorm.DB write *gorm.DB } func NewMysql(cfg MySQLConfig) (MysqlRepo, error) { dbr, err := dbConnect(cfg.Read.User, cfg.Read.Pass, cfg.Read.Addr, cfg.Read.Name, cfg.Base.MaxOpenConn, cfg.Base.MaxIdleConn, cfg.Base.ConnMaxLifeTime) if err != nil { return nil, err } dbw, err := dbConnect(cfg.Write.User, cfg.Write.Pass, cfg.Write.Addr, cfg.Write.Name, cfg.Base.MaxOpenConn, cfg.Base.MaxIdleConn, cfg.Base.ConnMaxLifeTime) if err != nil { return nil, err } return &mysqlRepo{ read: dbr, write: dbw, }, nil } func (d *mysqlRepo) i() {} func (d *mysqlRepo) GetRead() *gorm.DB { return d.read } func (d *mysqlRepo) GetWrite() *gorm.DB { return d.write } func (d *mysqlRepo) Close() (err error) { rdb, err1 := d.read.DB() if err1 != nil { err = errors.WithStack(err1) } err2 := rdb.Close() if err2 != nil { err = errors.WithStack(err2) } wdb, err3 := d.write.DB() if err3 != nil { err = errors.WithStack(err3) } err4 := wdb.Close() if err4 != nil { err = errors.WithStack(err4) } return err } func dbConnect(user, pass, addr, dbName string, maxOpenConn, maxIdleConn int, connMaxLifeTime time.Duration) (*gorm.DB, error) { dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s", user, pass, addr, dbName, true, "Local") // 日志配置 newLogger := logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{ SlowThreshold: time.Second, // 慢SQL阈值 Colorful: true, // 彩色打印 IgnoreRecordNotFoundError: true, // 忽略记录未找到错误 LogLevel: logger.Error, // 日志级别 }, ) db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ SingularTable: true, }, Logger: newLogger, }) if err != nil { return nil, errors.Wrap(err, fmt.Sprintf("[db connection failed] Database name: %s", dbName)) } db.Set("gorm:table_options", "CHARSET=utf8mb4") sqlDB, err := db.DB() if err != nil { return nil, err } // 设置连接池 用于设置最大打开的连接数,默认值为0表示不限制.设置最大的连接数,可以避免并发太高导致连接mysql出现too many connections的错误。 sqlDB.SetMaxOpenConns(maxOpenConn) // 设置最大连接数 用于设置闲置的连接数.设置闲置的连接数则当开启的一个连接使用完成后可以放在池里等候下一次使用。 sqlDB.SetMaxIdleConns(maxIdleConn) // 设置最大连接超时 sqlDB.SetConnMaxLifetime(time.Minute * connMaxLifeTime) return db, nil }