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
5b340abf
authored
Apr 21, 2021
by
孙龙
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
up
parent
48e8e430
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
37 additions
and
23 deletions
README.md
app/common/config/mysql.go
app/controller/demo_controller.go
app/dao/dao.go
go.mod
go.sum
util/regist_etcd/regist.go
README.md
View file @
5b340abf
...
...
@@ -61,4 +61,10 @@ go run ./cmd/main.go -config=./config/dev/ -logdir = ./logs/
├── go.mod
参考文献:
xorm:https://www.kancloud.cn/xormplus/xorm/413729
go-redis-pool https://github.com/ichunt2019/go-redis-pool
app/common/config/mysql.go
View file @
5b340abf
...
...
@@ -18,7 +18,7 @@ type GroupDatabase struct {
}
//单
数据库配置
//单
、主数据 master
func
BuildDatabaseList
()
(
DatabaseList
map
[
string
]
BaseDatabase
)
{
return
map
[
string
]
BaseDatabase
{
"micro"
:
{
...
...
@@ -32,14 +32,14 @@ func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) {
}
//主从
mysql数据库配置
//主从
数据 master slave
func
BuildGroupDatabaseList
()
(
DatabaseList
map
[
string
]
GroupDatabase
)
{
return
map
[
string
]
GroupDatabase
{
"liexin"
:
{
DataSourceName
:
lib
.
Instance
(
"db"
)
.
GetStringSlice
(
"liexin.data_source_name"
),
Prefix
:
lib
.
Instance
(
"db"
)
.
GetString
(
"
micro
.table_prefix"
),
MaxIdleCons
:
lib
.
Instance
(
"db"
)
.
GetInt
(
"
micro
.max_idle_conn"
),
MaxOpenCons
:
lib
.
Instance
(
"db"
)
.
GetInt
(
"
micro
.max_open_conn"
),
Prefix
:
lib
.
Instance
(
"db"
)
.
GetString
(
"
liexin
.table_prefix"
),
MaxIdleCons
:
lib
.
Instance
(
"db"
)
.
GetInt
(
"
liexin
.max_idle_conn"
),
MaxOpenCons
:
lib
.
Instance
(
"db"
)
.
GetInt
(
"
liexin
.max_open_conn"
),
},
}
...
...
app/controller/demo_controller.go
View file @
5b340abf
...
...
@@ -20,8 +20,7 @@ func Hbsdata(ctx *gin.Context) {
xlog
.
Instance
(
"spu"
)
.
Info
(
"Hbsdata"
)
fmt
.
Println
(
lib
.
Instance
(
"db"
)
.
GetString
(
"liexin.max_open_conn"
))
fmt
.
Println
(
lib
.
Instance
(
"db"
)
.
GetString
(
"liexin.master.data_source_name"
))
fmt
.
Println
(
lib
.
Instance
(
"db"
)
.
GetStringSlice
(
"liexin.slave.data_source_name"
))
fmt
.
Println
(
lib
.
Instance
(
"db"
)
.
GetStringSlice
(
"liexin.data_source_name"
))
//b := lib.Instance("db").GetStringSlice("liexin.slave.data_source_name")
//fmt.Println(b)
...
...
app/dao/dao.go
View file @
5b340abf
...
...
@@ -2,15 +2,15 @@ package dao
import
(
"fmt"
"github.com/go-redis/redis/v7"
_
"github.com/go-sql-driver/mysql"
"github.com/ichunt2019/cfg/lib"
"ichunt_golang/app/common/config"
"time"
"sync"
"xorm.io/xorm"
redisPool
"github.com/ichunt2019/go-redis-pool"
"sync"
"github.com/go-redis/redis/v7"
_
"github.com/go-sql-driver/mysql"
"github.com/ichunt2019/cfg/lib"
"ichunt_golang/app/common/config"
)
var
(
...
...
@@ -19,9 +19,9 @@ var (
)
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
//redis 主从 连接池
}
//获取db实例
...
...
@@ -45,6 +45,7 @@ func mysqlSetup(d *dao) *dao {
)
DatabaseList
:=
config
.
BuildDatabaseList
()
GroupDatabaseList
:=
config
.
BuildGroupDatabaseList
()
if
len
(
GroupDatabaseList
)
>
0
{
for
conName
,
db
:=
range
DatabaseList
{
d
.
db
[
conName
],
err
=
xorm
.
NewEngine
(
"mysql"
,
db
.
DataSourceName
)
if
err
!=
nil
{
...
...
@@ -59,6 +60,8 @@ func mysqlSetup(d *dao) *dao {
//设置最大打开连接数
d
.
db
[
conName
]
.
SetMaxOpenConns
(
db
.
MaxOpenCons
)
}
}
if
len
(
GroupDatabaseList
)
>
0
{
for
conName
,
db
:=
range
GroupDatabaseList
{
d
.
dbGroup
[
conName
],
err
=
xorm
.
NewEngineGroup
(
"mysql"
,
db
.
DataSourceName
)
if
err
!=
nil
{
...
...
@@ -73,6 +76,7 @@ func mysqlSetup(d *dao) *dao {
//设置最大打开连接数
d
.
dbGroup
[
conName
]
.
SetMaxOpenConns
(
db
.
MaxOpenCons
)
}
}
return
d
}
...
...
@@ -110,8 +114,11 @@ func redisSetup(d *dao) *dao {
func
Init
()
{
Dao
=
&
dao
{}
once
.
Do
(
func
()
{
//单、主数据 master
Dao
.
db
=
make
(
map
[
string
]
*
xorm
.
Engine
,
0
)
//主从数据 master slave
Dao
.
dbGroup
=
make
(
map
[
string
]
*
xorm
.
EngineGroup
,
0
)
//redis连接池 支持主从 master slave
Dao
.
redisGroup
=
make
(
map
[
string
]
*
redisPool
.
Pool
,
0
)
Dao
=
mysqlSetup
(
Dao
)
Dao
=
redisSetup
(
Dao
)
...
...
go.mod
View file @
5b340abf
...
...
@@ -18,7 +18,7 @@ require (
github.com/go-redis/redis/v7 v7.4.0
github.com/go-sql-driver/mysql v1.5.0
github.com/gorilla/sessions v1.2.1 // indirect
github.com/ichunt2019/cfg v0.0.0-20210
225081543-828ee9831d70
github.com/ichunt2019/cfg v0.0.0-20210
310074903-4b1bcab17717
github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5
github.com/ichunt2019/ichunt-micro-registry v1.0.1
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
...
...
go.sum
View file @
5b340abf
...
...
@@ -215,6 +215,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ichunt2019/cfg v0.0.0-20210225081543-828ee9831d70 h1:k6dwr6fDj5P/0cEvSHtgPDdEUA0FiHK/Je231+S2Mto=
github.com/ichunt2019/cfg v0.0.0-20210225081543-828ee9831d70/go.mod h1:No53Lxkw+Z8MViyp07p5WVYnKAXSfLqNIxidtWxKhhA=
github.com/ichunt2019/cfg v0.0.0-20210310074903-4b1bcab17717 h1:re7Ju7lqA0/hUfp1/rpzboHdsmLbPkTtAtvn/8jzSts=
github.com/ichunt2019/cfg v0.0.0-20210310074903-4b1bcab17717/go.mod h1:No53Lxkw+Z8MViyp07p5WVYnKAXSfLqNIxidtWxKhhA=
github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5 h1:oP7r2xFbIppW2iWYbBuXF6oVrjULZ7wU3DeMTqUuZSo=
github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5/go.mod h1:63lgYPBXcNBPUtKz14iQeFZBm2318xG/zGvCczRlfSs=
github.com/ichunt2019/ichunt-micro-registry v1.0.1 h1:IrNy60FRIPzqf+Fk/hH5BbEwQgxfzpz62nNNTqpl2x8=
...
...
util/regist_etcd/regist.go
View file @
5b340abf
...
...
@@ -11,17 +11,17 @@ import (
func
Init
(){
nodes
:=
[]
*
registry
.
Node
{
{
IP
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_regist.ip"
),
Port
:
lib
.
Instance
(
"proxy"
)
.
GetInt
(
"etcd_regist.port"
),
Weight
:
lib
.
Instance
(
"proxy"
)
.
GetInt
(
"etcd_regist.weight"
),
IP
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_regist.ip"
),
Port
:
lib
.
Instance
(
"proxy"
)
.
GetInt
(
"etcd_regist.port"
),
Weight
:
lib
.
Instance
(
"proxy"
)
.
GetInt
(
"etcd_regist.weight"
),
},
}
etcdConfig
:=
registry
.
EtcdConfig
{
Address
:
lib
.
Instance
(
"proxy"
)
.
GetStringSlice
(
"etcd_config.addrs"
),
Username
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_config.username"
),
Password
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_config.password"
),
Path
:
"/ichuntMicroService/"
,
Address
:
lib
.
Instance
(
"proxy"
)
.
GetStringSlice
(
"etcd_config.addrs"
),
Username
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_config.username"
),
Password
:
lib
.
Instance
(
"proxy"
)
.
GetString
(
"etcd_config.password"
),
Path
:
"/ichuntMicroService/"
,
}
econfig
.
Register
(
lib
.
Instance
(
"proxy"
)
.
GetString
(
"micro_service_name"
),
etcdConfig
,
nodes
)
}
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