From e3df65f354ecfd82d1292c117bbd83060f4b0049 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Tue, 7 Dec 2021 18:46:44 +0800 Subject: [PATCH] feat: issues #809 --- server/config.yaml | 20 ++++++++++++++++++++ server/config/config.go | 5 +++-- server/config/db_list.go | 21 +++++++++++++++++++++ server/global/global.go | 22 ++++++++++++++++++++++ server/initialize/db_list.go | 24 ++++++++++++++++++++++++ server/initialize/gorm_mysql.go | 21 +++++++++++++++++++++ server/initialize/gorm_pgsql.go | 20 ++++++++++++++++++++ server/main.go | 1 + 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 server/config/db_list.go create mode 100644 server/initialize/db_list.go diff --git a/server/config.yaml b/server/config.yaml index b94bdd06..1946fdb8 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -83,6 +83,26 @@ pgsql: log-mode: "" log-zap: false +db-list: [ + { + disabled: true, # 是否启用 + type: "", # 数据库的类型,目前支持mysql、pgsql + alias-name: "", # 数据库的名称,注意: alias-name 需要在db-list中唯一 + path: '', + port: '', + config: '', + db-name: '', + username: '', + password: '', + max-idle-conns: 10, + max-open-conns: 100, + log-mode: "", + log-zap: false, + } +] + + + # local configuration local: path: 'uploads/file' diff --git a/server/config/config.go b/server/config/config.go index cbf017cc..5668edc8 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -11,8 +11,9 @@ type Server struct { // auto AutoCode Autocode `mapstructure:"autoCode" json:"autoCode" yaml:"autoCode"` // gorm - Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` - Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` + Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` + Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` + DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` // oss Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` diff --git a/server/config/db_list.go b/server/config/db_list.go new file mode 100644 index 00000000..2933e8e4 --- /dev/null +++ b/server/config/db_list.go @@ -0,0 +1,21 @@ +package config + +type DB struct { + Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"` + Type string `mapstructure:"type" json:"type" yaml:"type"` + AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"` + Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 + Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 + Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 + Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名 + Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名 + Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码 + MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数 + MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 + LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志 + LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` +} + +func (m *DB) Dsn() string { + return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config +} diff --git a/server/global/global.go b/server/global/global.go index 9093c505..e0f40c42 100644 --- a/server/global/global.go +++ b/server/global/global.go @@ -1,6 +1,8 @@ package global import ( + "sync" + "github.com/flipped-aurora/gin-vue-admin/server/utils/timer" "github.com/songzhibin97/gkit/cache/local_cache" @@ -17,6 +19,7 @@ import ( var ( GVA_DB *gorm.DB + GVA_DBList map[string]*gorm.DB GVA_REDIS *redis.Client GVA_CONFIG config.Server GVA_VP *viper.Viper @@ -26,4 +29,23 @@ var ( GVA_Concurrency_Control = &singleflight.Group{} BlackCache local_cache.Cache + lock sync.RWMutex ) + +// GetGlobalDBByDBName 通过名称获取db list中的db +func GetGlobalDBByDBName(dbname string) *gorm.DB { + lock.RLock() + defer lock.RUnlock() + return GVA_DBList[dbname] +} + +// MustGetGlobalDBByDBName 通过名称获取db 如果不存在则panic +func MustGetGlobalDBByDBName(dbname string) *gorm.DB { + lock.RLock() + defer lock.RUnlock() + db, ok := GVA_DBList[dbname] + if !ok || db == nil { + panic("db no init") + } + return db +} diff --git a/server/initialize/db_list.go b/server/initialize/db_list.go new file mode 100644 index 00000000..395d2834 --- /dev/null +++ b/server/initialize/db_list.go @@ -0,0 +1,24 @@ +package initialize + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "gorm.io/gorm" +) + +func DBList() { + dbMap := make(map[string]*gorm.DB) + for _, info := range global.GVA_CONFIG.DBList { + if info.Disable { + continue + } + switch info.Type { + case "mysql": + dbMap[info.Dbname] = GormMysqlByConfig(info) + case "pgsql": + dbMap[info.Dbname] = GormPgSqlByConfig(info) + default: + continue + } + } + global.GVA_DBList = dbMap +} diff --git a/server/initialize/gorm_mysql.go b/server/initialize/gorm_mysql.go index 851fa8de..4c8b6e6f 100644 --- a/server/initialize/gorm_mysql.go +++ b/server/initialize/gorm_mysql.go @@ -1,6 +1,7 @@ package initialize import ( + "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/mysql" @@ -29,3 +30,23 @@ func GormMysql() *gorm.DB { return db } } + +// GormMysqlByConfig 初始化Mysql数据库用过传入配置 +func GormMysqlByConfig(m config.DB) *gorm.DB { + if m.Dbname == "" { + return nil + } + mysqlConfig := mysql.Config{ + DSN: m.Dsn(), // DSN data source name + DefaultStringSize: 191, // string 类型字段的默认长度 + SkipInitializeWithVersion: false, // 根据版本自动配置 + } + if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { + panic(err) + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(m.MaxIdleConns) + sqlDB.SetMaxOpenConns(m.MaxOpenConns) + return db + } +} diff --git a/server/initialize/gorm_pgsql.go b/server/initialize/gorm_pgsql.go index e178a277..e474b981 100644 --- a/server/initialize/gorm_pgsql.go +++ b/server/initialize/gorm_pgsql.go @@ -1,6 +1,7 @@ package initialize import ( + "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal" "gorm.io/driver/postgres" @@ -28,3 +29,22 @@ func GormPgSql() *gorm.DB { return db } } + +// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数 +func GormPgSqlByConfig(p config.DB) *gorm.DB { + if p.Dbname == "" { + return nil + } + pgsqlConfig := postgres.Config{ + DSN: p.Dsn(), // DSN data source name + PreferSimpleProtocol: false, + } + if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config()); err != nil { + panic(err) + } else { + sqlDB, _ := db.DB() + sqlDB.SetMaxIdleConns(p.MaxIdleConns) + sqlDB.SetMaxOpenConns(p.MaxOpenConns) + return db + } +} diff --git a/server/main.go b/server/main.go index e607749a..cdcab9ed 100644 --- a/server/main.go +++ b/server/main.go @@ -23,6 +23,7 @@ func main() { global.GVA_LOG = core.Zap() // 初始化zap日志库 global.GVA_DB = initialize.Gorm() // gorm连接数据库 initialize.Timer() + initialize.DBList() if global.GVA_DB != nil { initialize.RegisterTables(global.GVA_DB) // 初始化表 // 程序结束前关闭数据库链接