mysql add master and slave mode

This commit is contained in:
hoteas 2021-05-24 06:14:58 +08:00
parent c8e926446e
commit 2632517fab
7 changed files with 81 additions and 86 deletions

View File

@ -1,7 +1,6 @@
package hotime package hotime
import ( import (
"./tools/db"
"bytes" "bytes"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
@ -23,7 +22,7 @@ type Application struct {
Port string //端口号 Port string //端口号
TLSPort string //ssl访问端口号 TLSPort string //ssl访问端口号
connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用false表示有监听器处理 connectListener []func(this *Context) bool //所有的访问监听,true按原计划继续使用false表示有监听器处理
connectDbFunc func(err ...*Error) *sql.DB connectDbFunc func(err ...*Error) (master, slave *sql.DB)
configPath string configPath string
Config Map Config Map
Db HoTimeDB Db HoTimeDB
@ -157,7 +156,7 @@ func (this *Application) Run(router Router) {
} }
//启动实例 //启动实例
func (this *Application) SetConnectDB(connect func(err ...*Error) *sql.DB) { func (this *Application) SetConnectDB(connect func(err ...*Error) (master, slave *sql.DB)) {
this.connectDbFunc = connect this.connectDbFunc = connect
this.Db.SetConnect(this.connectDbFunc) this.Db.SetConnect(this.connectDbFunc)
@ -172,7 +171,7 @@ func (this *Application) SetSession(short CacheIns, Long CacheIns) {
} }
//默认配置缓存和session实现 //默认配置缓存和session实现
func (this *Application) SetDefault(connect func(err ...*Error) *sql.DB) { func (this *Application) SetDefault(connect func(err ...*Error) (*sql.DB, *sql.DB)) {
this.SetConfig() this.SetConfig()
if connect != nil { if connect != nil {
@ -451,7 +450,7 @@ func Init(config string) Application {
appIns := Application{} appIns := Application{}
//手动模式, //手动模式,
appIns.SetConfig(config) appIns.SetConfig(config)
db.SetDB(&appIns) SetDB(&appIns)
//appIns.SetCache() //appIns.SetCache()
return appIns return appIns
} }

25
db.go
View File

@ -17,15 +17,15 @@ type HoTimeDB struct {
DBCached bool DBCached bool
LastQuery string LastQuery string
LastData []interface{} LastData []interface{}
ConnectFunc func(err ...*Error) *sql.DB ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
LastErr Error LastErr Error
limit Slice limit Slice
Tx *sql.Tx //事务对象 Tx *sql.Tx //事务对象
SlaveDB *sql.DB
} }
//设置数据库配置连接 //设置数据库配置连接
func (this *HoTimeDB) SetConnect(connect func(err ...*Error) *sql.DB, err ...*Error) { func (this *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error) {
this.ConnectFunc = connect this.ConnectFunc = connect
this.InitDb() this.InitDb()
} }
@ -55,15 +55,19 @@ func (this *HoTimeDB) InitDb(err ...*Error) Error {
if len(err) != 0 { if len(err) != 0 {
this.LastErr = *(err[0]) this.LastErr = *(err[0])
} }
this.DB = this.ConnectFunc(&this.LastErr) this.DB, this.SlaveDB = this.ConnectFunc(&this.LastErr)
if this.DB == nil { if this.DB == nil {
return this.LastErr return this.LastErr
} }
e := this.DB.Ping() e := this.DB.Ping()
this.LastErr.SetError(e) this.LastErr.SetError(e)
if this.SlaveDB != nil {
e := this.SlaveDB.Ping()
this.LastErr.SetError(e)
}
return this.LastErr return this.LastErr
} }
@ -284,8 +288,13 @@ func (this *HoTimeDB) Query(query string, args ...interface{}) []Map {
this.LastQuery = query this.LastQuery = query
this.LastData = args this.LastData = args
//主从数据库切换只有select语句有从数据库
db := this.DB
if this.SlaveDB != nil {
db = this.SlaveDB
}
if this.DB == nil { if db == nil {
err = errors.New("没有初始化数据库") err = errors.New("没有初始化数据库")
this.LastErr.SetError(err) this.LastErr.SetError(err)
return nil return nil
@ -294,12 +303,12 @@ func (this *HoTimeDB) Query(query string, args ...interface{}) []Map {
if this.Tx != nil { if this.Tx != nil {
resl, err = this.Tx.Query(query, args...) resl, err = this.Tx.Query(query, args...)
} else { } else {
resl, err = this.DB.Query(query, args...) resl, err = db.Query(query, args...)
} }
this.LastErr.SetError(err) this.LastErr.SetError(err)
if err != nil { if err != nil {
if err = this.DB.Ping(); err != nil { if err = db.Ping(); err != nil {
this.LastErr.SetError(err) this.LastErr.SetError(err)
this.InitDb() this.InitDb()
if this.LastErr.GetError() != nil { if this.LastErr.GetError() != nil {

58
db_assist.go Normal file
View File

@ -0,0 +1,58 @@
package hotime
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)
func SetDB(appIns *Application) {
db := appIns.Config.GetMap("db")
dbSqlite := db.GetMap("sqlite")
dbMysql := db.GetMap("mysql")
if db != nil && dbSqlite != nil {
SetSqliteDB(appIns, dbSqlite)
}
if db != nil && dbMysql != nil {
SetMysqlDB(appIns, dbMysql)
}
}
func SetMysqlDB(appIns *Application, config Map) {
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
//master数据库配置
query := config.GetString("user") + ":" + config.GetString("password") +
"@tcp(" + config.GetString("host") + ":" + config.GetString("port") + ")/" + config.GetString("name") + "?charset=utf8"
DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
master = DB
//slave数据库配置
configSlave := config.GetMap("slave")
if configSlave != nil {
query := configSlave.GetString("user") + ":" + configSlave.GetString("password") +
"@tcp(" + config.GetString("host") + ":" + configSlave.GetString("port") + ")/" + configSlave.GetString("name") + "?charset=utf8"
DB1, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
slave = DB1
}
return master, slave
//return DB
})
appIns.Db.Type = "mysql"
}
func SetSqliteDB(appIns *Application, config Map) {
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
db, e := sql.Open("sqlite3", config.GetString("path"))
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
master = db
return master, slave
})
appIns.Db.Type = "sqlite"
}

View File

@ -2,7 +2,6 @@ package main
import ( import (
"../../hotime" "../../hotime"
"../tools/db"
"database/sql" "database/sql"
"fmt" "fmt"
//"go.hoteas.com/hotime/cache" //"go.hoteas.com/hotime/cache"
@ -34,32 +33,19 @@ func main() {
//mysql //mysql
//mysql.SetDB(&appIns) //mysql.SetDB(&appIns)
//自动选择数据库 //自动选择数据库
db.SetDB(&appIns)
//appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
// query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
// "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
// DB, e := sql.Open("mysql", query)
// if e != nil && len(err) != 0 {
// err[0].SetError(e)
// }
// return DB
//})
//内存缓存数据库数据,错误则删除
// appIns.Db.CacheIns=hotime.CacheIns(&hotime.CacheMemory{})
appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")})) appIns.SetSession(hotime.CacheIns(&hotime.CacheMemory{}), hotime.CacheIns(&hotime.CacheDb{Db: &appIns.Db, Time: appIns.Config.GetInt64("cacheTime")}))
appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{})) appIns.SetCache(hotime.CacheIns(&hotime.CacheMemory{}))
//快捷模式 //快捷模式
appIns.SetDefault(func(err ...*hotime.Error) *sql.DB { appIns.SetDefault(func(err ...*hotime.Error) (*sql.DB, *sql.DB) {
query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") + query := appIns.Config.GetString("dbUser") + ":" + appIns.Config.GetString("dbPwd") +
"@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8" "@tcp(" + appIns.Config.GetString("dbHost") + ":" + appIns.Config.GetString("dbPort") + ")/" + appIns.Config.GetString("dbName") + "?charset=utf8"
DB, e := sql.Open("mysql", query) DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 { if e != nil && len(err) != 0 {
err[0].SetError(e) err[0].SetError(e)
} }
return DB return DB, nil
}) })
appIns.Run(hotime.Router{ appIns.Run(hotime.Router{

View File

@ -1,19 +0,0 @@
package db
import (
"../../../hotime"
"../mysql"
"../sqlite"
)
func SetDB(appIns *hotime.Application) {
db := appIns.Config.GetMap("db")
dbSqlite := db.GetMap("sqlite")
dbMysql := db.GetMap("mysql")
if db != nil && dbSqlite != nil {
sqlite.SetDB(appIns, dbSqlite)
}
if db != nil && dbMysql != nil {
mysql.SetDB(appIns, dbMysql)
}
}

View File

@ -1,20 +0,0 @@
package mysql
import (
"../../../hotime"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func SetDB(appIns *hotime.Application, config hotime.Map) {
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
query := config.GetString("user") + ":" + config.GetString("password") +
"@tcp(" + config.GetString("host") + ":" + config.GetString("port") + ")/" + config.GetString("name") + "?charset=utf8"
DB, e := sql.Open("mysql", query)
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return DB
})
appIns.Db.Type = "mysql"
}

View File

@ -1,18 +0,0 @@
package sqlite
import (
"../../../hotime"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func SetDB(appIns *hotime.Application, config hotime.Map) {
appIns.SetConnectDB(func(err ...*hotime.Error) *sql.DB {
db, e := sql.Open("sqlite3", config.GetString("path"))
if e != nil && len(err) != 0 {
err[0].SetError(e)
}
return db
})
appIns.Db.Type = "sqlite"
}