Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
孙龙
/
ichunt_golang_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
121897e7
authored
Mar 10, 2021
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
优化
parent
8c136707
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
37 deletions
app/dao/dao.go
app/dao/dao.go
View file @
121897e7
...
...
@@ -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"
redis
p
ool
"github.com/ichunt2019/go-redis-pool"
redis
P
ool
"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
]
*
redis
p
ool
.
Pool
db
map
[
string
]
*
xorm
.
Engine
//非主从mysql数据库
dbGroup
map
[
string
]
*
xorm
.
EngineGroup
//mysql主从
redisGroup
map
[
string
]
*
redis
P
ool
.
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
=
redis
pool
.
NewHA
(
&
redisp
ool
.
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
=
redis
Pool
.
NewHA
(
&
redisP
ool
.
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
]
*
redis
pool
.
Pool
,
0
)
Dao
.
db
=
make
(
map
[
string
]
*
xorm
.
Engine
,
0
)
Dao
.
dbGroup
=
make
(
map
[
string
]
*
xorm
.
EngineGroup
,
0
)
Dao
.
redisGroup
=
make
(
map
[
string
]
*
redis
Pool
.
Pool
,
0
)
Dao
=
mysqlSetup
(
Dao
)
Dao
=
redisSetup
(
Dao
)
})
}
\ No newline at end of file
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment