Commit 121897e7 by mushishixian

优化

parent 8c136707
Showing with 37 additions and 37 deletions
......@@ -4,47 +4,49 @@ import (
"fmt"
"github.com/go-redis/redis/v7"
_ "github.com/go-sql-driver/mysql"
"xorm.io/xorm"
"github.com/ichunt2019/cfg/lib"
"ichunt_golang/app/common/config"
"time"
"xorm.io/xorm"
redispool "github.com/ichunt2019/go-redis-pool"
redisPool "github.com/ichunt2019/go-redis-pool"
"sync"
)
var (
once sync.Once
Dao *dao
Dao *dao
)
type dao struct {
db map[string]*xorm.Engine //非主从mysql数据库
dbGroup map[string]*xorm.EngineGroup //mysql主从
redisGroup map[string]*redispool.Pool
db map[string]*xorm.Engine //非主从mysql数据库
dbGroup map[string]*xorm.EngineGroup //mysql主从
redisGroup map[string]*redisPool.Pool
}
//获取db实例
func(self *dao) GetDb(databases string) *xorm.Engine{
return self.db[databases]
func (self *dao) GetDb(databases string) *xorm.Engine {
return self.db[databases]
}
//获取主从db实例
func(self *dao) GetDbGroup(databases string) *xorm.EngineGroup{
return self.dbGroup[databases]
func (self *dao) GetDbGroup(databases string) *xorm.EngineGroup {
return self.dbGroup[databases]
}
//获取主从db实例
func(self *dao) GetRedisDbGroup(databases string) *redispool.Pool{
return self.redisGroup[databases]
func (self *dao) GetRedisDbGroup(databases string) *redisPool.Pool {
return self.redisGroup[databases]
}
func mysqlSetup(d *dao) *dao{
func mysqlSetup(d *dao) *dao {
var (
err error
)
DatabaseList := config.BuildDatabaseList()
GroupDatabaseList := config.BuildGroupDatabaseList()
for conName, db := range DatabaseList {
d.db[conName],err = xorm.NewEngine("mysql", db.DataSourceName)
d.db[conName], err = xorm.NewEngine("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
......@@ -58,7 +60,7 @@ func mysqlSetup(d *dao) *dao{
d.db[conName].SetMaxOpenConns(db.MaxOpenCons)
}
for conName, db := range GroupDatabaseList {
d.dbGroup[conName],err = xorm.NewEngineGroup("mysql", db.DataSourceName)
d.dbGroup[conName], err = xorm.NewEngineGroup("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
......@@ -72,30 +74,29 @@ func mysqlSetup(d *dao) *dao{
d.dbGroup[conName].SetMaxOpenConns(db.MaxOpenCons)
}
return d
}
func redisSetup(d *dao) *dao{
func redisSetup(d *dao) *dao {
var err error
redisGroupList := config.BuildRedisGroupConfigs()
fmt.Println(redisGroupList)
for redisServerName,redisInfo:= range redisGroupList{
d.redisGroup[redisServerName], err = redispool.NewHA(&redispool.HAConfig{
Master: redisInfo.MasterHost,
Slaves: redisInfo.SlaveHost,
Password:redisInfo.Password,
ReadonlyPassword:redisInfo.ReadonlyPassword,
Options:&redis.Options{
DialTimeout:redisInfo.DialTimeout,//连接超时
MinIdleConns:redisInfo.MinIdleConns,//空闲链接数
ReadTimeout:redisInfo.ReadTimeout,
for redisServerName, redisInfo := range redisGroupList {
d.redisGroup[redisServerName], err = redisPool.NewHA(&redisPool.HAConfig{
Master: redisInfo.MasterHost,
Slaves: redisInfo.SlaveHost,
Password: redisInfo.Password,
ReadonlyPassword: redisInfo.ReadonlyPassword,
Options: &redis.Options{
DialTimeout: redisInfo.DialTimeout, //连接超时
MinIdleConns: redisInfo.MinIdleConns, //空闲链接数
ReadTimeout: redisInfo.ReadTimeout,
WriteTimeout: redisInfo.WriteTimeout,
},
AutoEjectHost: true,//是否弹出故障主机
ServerFailureLimit: 3,//达到失败次数时弹出
ServerRetryTimeout: 5 * time.Second,//在“ServerRetryTimeout”之后重试弹出的主机`
MinServerNum: 1,//保留min服务器 针对从服务器
AutoEjectHost: true, //是否弹出故障主机
ServerFailureLimit: 3, //达到失败次数时弹出
ServerRetryTimeout: 5 * time.Second, //在“ServerRetryTimeout”之后重试弹出的主机`
MinServerNum: 1, //保留min服务器 针对从服务器
})
if err != nil {
......@@ -109,10 +110,10 @@ func redisSetup(d *dao) *dao{
func Init() {
Dao = &dao{}
once.Do(func() {
Dao.db = make(map[string]*xorm.Engine,0)
Dao.dbGroup = make(map[string]*xorm.EngineGroup,0)
Dao.redisGroup = make(map[string]*redispool.Pool,0)
Dao.db = make(map[string]*xorm.Engine, 0)
Dao.dbGroup = make(map[string]*xorm.EngineGroup, 0)
Dao.redisGroup = make(map[string]*redisPool.Pool, 0)
Dao = mysqlSetup(Dao)
Dao = redisSetup(Dao)
})
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment