-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
192 lines (161 loc) · 5.19 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mysqlx
import (
"fmt"
"github.com/chenxinqun/ginWarpPkg/errno"
"github.com/chenxinqun/ginWarpPkg/sysx/environment"
"math/rand"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"
)
type Info struct {
Read []struct {
Addr string `toml:"Addr" json:"Addr"`
User string `toml:"User" json:"User"`
Pass string `toml:"Pass" json:"Pass"`
Name string `toml:"Name" json:"Name"`
} `toml:"Read" json:"Read"`
Write []struct {
Addr string `toml:"Addr" json:"Addr"`
User string `toml:"User" json:"User"`
Pass string `toml:"Pass" json:"Pass"`
Name string `toml:"Name" json:"Name"`
} `toml:"Write" json:"Write"`
Base struct {
MaxOpenConn int `toml:"MaxOpenConn" json:"MaxOpenConn"`
MaxIDleConn int `toml:"MaxIDleConn" json:"MaxIDleConn"`
ConnMaxLifeTime time.Duration `toml:"ConnMaxLifeTime" json:"ConnMaxLifeTime"` // 最大连接超时时间单位分钟
} `toml:"Base"`
}
var _ Repo = (*DB)(nil)
type Repo interface {
GetDb() *gorm.DB
Close() error
}
type DB struct {
Db *gorm.DB
}
var defaultRepo Repo
func Default() Repo {
return defaultRepo
}
func New(cfg Info) (Repo, error) {
var repo Repo
base := cfg.Base
write := cfg.Write[0]
db, err := dbConnect(write.User, write.Pass, write.Addr, write.Name, base.MaxOpenConn, base.MaxIDleConn, base.ConnMaxLifeTime)
if err != nil {
return nil, err
}
// 根据MySQL热Read配置判断是否要开启读写分离
if len(cfg.Read) > 0 {
// 合成read库的连接地址
readList := make([]string, 0)
for _, r := range cfg.Read {
readList = append(readList, Dsn(r.User, r.Pass, r.Addr, r.Name))
}
// 合成write库的连接地址
writeList := make([]string, 0)
for _, w := range cfg.Write {
writeList = append(writeList, Dsn(w.User, w.Pass, w.Addr, w.Name))
}
// 获取写分分离插件
resolver := readWriteResolver(readList, writeList, base.MaxOpenConn, base.MaxIDleConn, base.ConnMaxLifeTime)
// 注册读写分离插件
_ = db.Use(resolver)
}
// 注册链路追踪插件
_ = db.Use(new(TracePlugin))
if environment.Active() != nil {
// 如果不是Pro和Pre环境, 开启db.Debug()模式
if !environment.Active().IsPro() && !environment.Active().IsPre() {
db.Logger = db.Logger.LogMode(logger.Info)
}
}
repo = &DB{
Db: db,
}
// 将第一次数据库连接设为默认值
if defaultRepo == nil {
defaultRepo = repo
}
return repo, nil
}
func (d *DB) GetDb() *gorm.DB {
return d.Db
}
func (d *DB) Close() error {
sqlDB, err := d.Db.DB()
if err != nil {
return err
}
return sqlDB.Close()
}
func Dsn(user, pass, addr, dbName string) string {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s",
user,
pass,
addr,
dbName,
true,
"Local")
return dsn
}
type randomPolicy struct{}
func (randomPolicy) Resolve(connPools []gorm.ConnPool) gorm.ConnPool {
rand.Seed(time.Now().UnixNano())
return connPools[rand.Intn(len(connPools))]
}
func readWriteResolver(readList []string, writeList []string, maxOpenConn, maxIDleConn int, connMaxLifeTime time.Duration) *dbresolver.DBResolver {
// 处理读的连接
replicas := make([]gorm.Dialector, 0)
for _, readDsn := range readList {
replicas = append(replicas, mysql.Open(readDsn))
}
// 处理写的连接
sources := make([]gorm.Dialector, 0)
for _, writeDsn := range writeList {
sources = append(sources, mysql.Open(writeDsn))
}
// 负载均衡器
policy := new(randomPolicy)
resolver := dbresolver.Register(dbresolver.Config{
Sources: sources,
Replicas: replicas,
Policy: policy,
}).
// 设置连接池 用于设置最大打开的连接数,默认值为0表示不限制.设置最大的连接数,可以避免并发太高导致连接mysql出现too many connections的错误。
SetMaxOpenConns(maxOpenConn).
// 设置最大连接数 用于设置闲置的连接数.设置闲置的连接数则当开启的一个连接使用完成后可以放在池里等候下一次使用。
SetMaxIdleConns(maxIDleConn).
// 设置最大连接超时
SetConnMaxLifetime(time.Minute * connMaxLifeTime)
return resolver
}
func dbConnect(user, pass, addr, dbName string, maxOpenConn, maxIDleConn int, connMaxLifeTime time.Duration) (*gorm.DB, error) {
dsn := Dsn(user, pass, addr, dbName)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 使用单数表名(不使用复数形式单词)
},
})
if err != nil {
return nil, errno.Wrap(err, fmt.Sprintf("[mysql connection failed] Database DSN: %s", dsn))
}
// 获取原始SQL连接
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)
db.Set("gorm:table_options", "CHARSET=utf8mb4")
return db, nil
}