Commit 5b340abf by 孙龙

up

parent 48e8e430
...@@ -61,4 +61,10 @@ go run ./cmd/main.go -config=./config/dev/ -logdir = ./logs/ ...@@ -61,4 +61,10 @@ go run ./cmd/main.go -config=./config/dev/ -logdir = ./logs/
├── go.mod ├── go.mod
参考文献:
xorm:https://www.kancloud.cn/xormplus/xorm/413729
go-redis-pool https://github.com/ichunt2019/go-redis-pool
...@@ -18,7 +18,7 @@ type GroupDatabase struct { ...@@ -18,7 +18,7 @@ type GroupDatabase struct {
} }
//单数据库配置 //单、主数据 master
func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) { func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) {
return map[string]BaseDatabase{ return map[string]BaseDatabase{
"micro": { "micro": {
...@@ -32,14 +32,14 @@ func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) { ...@@ -32,14 +32,14 @@ func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) {
} }
//主从mysql数据库配置 //主从数据 master slave
func BuildGroupDatabaseList() (DatabaseList map[string]GroupDatabase) { func BuildGroupDatabaseList() (DatabaseList map[string]GroupDatabase) {
return map[string]GroupDatabase{ return map[string]GroupDatabase{
"liexin": { "liexin": {
DataSourceName:lib.Instance("db").GetStringSlice("liexin.data_source_name"), DataSourceName:lib.Instance("db").GetStringSlice("liexin.data_source_name"),
Prefix: lib.Instance("db").GetString("micro.table_prefix"), Prefix: lib.Instance("db").GetString("liexin.table_prefix"),
MaxIdleCons:lib.Instance("db").GetInt("micro.max_idle_conn"), MaxIdleCons:lib.Instance("db").GetInt("liexin.max_idle_conn"),
MaxOpenCons:lib.Instance("db").GetInt("micro.max_open_conn"), MaxOpenCons:lib.Instance("db").GetInt("liexin.max_open_conn"),
}, },
} }
......
...@@ -20,8 +20,7 @@ func Hbsdata(ctx *gin.Context) { ...@@ -20,8 +20,7 @@ func Hbsdata(ctx *gin.Context) {
xlog.Instance("spu").Info("Hbsdata") xlog.Instance("spu").Info("Hbsdata")
fmt.Println(lib.Instance("db").GetString("liexin.max_open_conn")) 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.data_source_name"))
fmt.Println(lib.Instance("db").GetStringSlice("liexin.slave.data_source_name"))
//b := lib.Instance("db").GetStringSlice("liexin.slave.data_source_name") //b := lib.Instance("db").GetStringSlice("liexin.slave.data_source_name")
//fmt.Println(b) //fmt.Println(b)
......
...@@ -2,15 +2,15 @@ package dao ...@@ -2,15 +2,15 @@ package dao
import ( import (
"fmt" "fmt"
"github.com/go-redis/redis/v7"
_ "github.com/go-sql-driver/mysql"
"github.com/ichunt2019/cfg/lib"
"ichunt_golang/app/common/config"
"time" "time"
"sync"
"xorm.io/xorm" "xorm.io/xorm"
redisPool "github.com/ichunt2019/go-redis-pool" 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 ( var (
...@@ -19,9 +19,9 @@ var ( ...@@ -19,9 +19,9 @@ var (
) )
type dao struct { type dao struct {
db map[string]*xorm.Engine //非主从mysql数据库 db map[string]*xorm.Engine //非主从mysql数据库 连接池
dbGroup map[string]*xorm.EngineGroup //mysql主从 dbGroup map[string]*xorm.EngineGroup //mysql主从 连接池
redisGroup map[string]*redisPool.Pool redisGroup map[string]*redisPool.Pool //redis 主从 连接池
} }
//获取db实例 //获取db实例
...@@ -45,33 +45,37 @@ func mysqlSetup(d *dao) *dao { ...@@ -45,33 +45,37 @@ func mysqlSetup(d *dao) *dao {
) )
DatabaseList := config.BuildDatabaseList() DatabaseList := config.BuildDatabaseList()
GroupDatabaseList := config.BuildGroupDatabaseList() GroupDatabaseList := config.BuildGroupDatabaseList()
for conName, db := range DatabaseList { if len(GroupDatabaseList) > 0 {
d.db[conName], err = xorm.NewEngine("mysql", db.DataSourceName) for conName, db := range DatabaseList {
if err != nil { d.db[conName], err = xorm.NewEngine("mysql", db.DataSourceName)
panic(err) if err != nil {
} panic(err)
//日志打印SQL }
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL") //日志打印SQL
d.db[conName].ShowSQL(ShowSql) ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
d.db[conName].ShowSQL(ShowSql)
//设置连接池的空闲数大小 //设置连接池的空闲数大小
d.db[conName].SetMaxIdleConns(db.MaxIdleCons) d.db[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数 //设置最大打开连接数
d.db[conName].SetMaxOpenConns(db.MaxOpenCons) d.db[conName].SetMaxOpenConns(db.MaxOpenCons)
}
for conName, db := range GroupDatabaseList {
d.dbGroup[conName], err = xorm.NewEngineGroup("mysql", db.DataSourceName)
if err != nil {
panic(err)
} }
//日志打印SQL }
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL") if len(GroupDatabaseList) > 0 {
d.dbGroup[conName].ShowSQL(ShowSql) for conName, db := range GroupDatabaseList {
d.dbGroup[conName], err = xorm.NewEngineGroup("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
//日志打印SQL
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
d.dbGroup[conName].ShowSQL(ShowSql)
//设置连接池的空闲数大小 //设置连接池的空闲数大小
d.dbGroup[conName].SetMaxIdleConns(db.MaxIdleCons) d.dbGroup[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数 //设置最大打开连接数
d.dbGroup[conName].SetMaxOpenConns(db.MaxOpenCons) d.dbGroup[conName].SetMaxOpenConns(db.MaxOpenCons)
}
} }
return d return d
...@@ -110,8 +114,11 @@ func redisSetup(d *dao) *dao { ...@@ -110,8 +114,11 @@ func redisSetup(d *dao) *dao {
func Init() { func Init() {
Dao = &dao{} Dao = &dao{}
once.Do(func() { once.Do(func() {
//单、主数据 master
Dao.db = make(map[string]*xorm.Engine, 0) Dao.db = make(map[string]*xorm.Engine, 0)
//主从数据 master slave
Dao.dbGroup = make(map[string]*xorm.EngineGroup, 0) Dao.dbGroup = make(map[string]*xorm.EngineGroup, 0)
//redis连接池 支持主从 master slave
Dao.redisGroup = make(map[string]*redisPool.Pool, 0) Dao.redisGroup = make(map[string]*redisPool.Pool, 0)
Dao = mysqlSetup(Dao) Dao = mysqlSetup(Dao)
Dao = redisSetup(Dao) Dao = redisSetup(Dao)
......
...@@ -18,7 +18,7 @@ require ( ...@@ -18,7 +18,7 @@ require (
github.com/go-redis/redis/v7 v7.4.0 github.com/go-redis/redis/v7 v7.4.0
github.com/go-sql-driver/mysql v1.5.0 github.com/go-sql-driver/mysql v1.5.0
github.com/gorilla/sessions v1.2.1 // indirect github.com/gorilla/sessions v1.2.1 // indirect
github.com/ichunt2019/cfg v0.0.0-20210225081543-828ee9831d70 github.com/ichunt2019/cfg v0.0.0-20210310074903-4b1bcab17717
github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5 github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5
github.com/ichunt2019/ichunt-micro-registry v1.0.1 github.com/ichunt2019/ichunt-micro-registry v1.0.1
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042 github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
......
...@@ -215,6 +215,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= ...@@ -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/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 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-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 h1:oP7r2xFbIppW2iWYbBuXF6oVrjULZ7wU3DeMTqUuZSo=
github.com/ichunt2019/go-redis-pool v0.0.0-20210305064829-86b9011c57f5/go.mod h1:63lgYPBXcNBPUtKz14iQeFZBm2318xG/zGvCczRlfSs= 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= github.com/ichunt2019/ichunt-micro-registry v1.0.1 h1:IrNy60FRIPzqf+Fk/hH5BbEwQgxfzpz62nNNTqpl2x8=
......
...@@ -11,17 +11,17 @@ import ( ...@@ -11,17 +11,17 @@ import (
func Init(){ func Init(){
nodes := []*registry.Node{ nodes := []*registry.Node{
{ {
IP: lib.Instance("proxy").GetString("etcd_regist.ip"), IP : lib.Instance("proxy").GetString("etcd_regist.ip"),
Port: lib.Instance("proxy").GetInt("etcd_regist.port"), Port : lib.Instance("proxy").GetInt("etcd_regist.port"),
Weight:lib.Instance("proxy").GetInt("etcd_regist.weight"), Weight : lib.Instance("proxy").GetInt("etcd_regist.weight"),
}, },
} }
etcdConfig := registry.EtcdConfig{ etcdConfig := registry.EtcdConfig{
Address: lib.Instance("proxy").GetStringSlice("etcd_config.addrs"), Address : lib.Instance("proxy").GetStringSlice("etcd_config.addrs"),
Username: lib.Instance("proxy").GetString("etcd_config.username"), Username : lib.Instance("proxy").GetString("etcd_config.username"),
Password:lib.Instance("proxy").GetString("etcd_config.password"), Password : lib.Instance("proxy").GetString("etcd_config.password"),
Path:"/ichuntMicroService/", Path : "/ichuntMicroService/",
} }
econfig.Register(lib.Instance("proxy").GetString("micro_service_name"),etcdConfig,nodes) econfig.Register(lib.Instance("proxy").GetString("micro_service_name"),etcdConfig,nodes)
} }
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