Commit 5b340abf by 孙龙

up

parent 48e8e430
......@@ -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
......@@ -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"),
},
}
......
......@@ -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)
......
......@@ -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,33 +45,37 @@ func mysqlSetup(d *dao) *dao {
)
DatabaseList := config.BuildDatabaseList()
GroupDatabaseList := config.BuildGroupDatabaseList()
for conName, db := range DatabaseList {
d.db[conName], err = xorm.NewEngine("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
//日志打印SQL
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
d.db[conName].ShowSQL(ShowSql)
if len(GroupDatabaseList) > 0 {
for conName, db := range DatabaseList {
d.db[conName], err = xorm.NewEngine("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
//日志打印SQL
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
d.db[conName].ShowSQL(ShowSql)
//设置连接池的空闲数大小
d.db[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
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)
//设置连接池的空闲数大小
d.db[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
d.db[conName].SetMaxOpenConns(db.MaxOpenCons)
}
//日志打印SQL
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
d.dbGroup[conName].ShowSQL(ShowSql)
}
if len(GroupDatabaseList) > 0 {
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].SetMaxOpenConns(db.MaxOpenCons)
//设置连接池的空闲数大小
d.dbGroup[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
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)
......
......@@ -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-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/ichunt-micro-registry v1.0.1
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
......
......@@ -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=
......
......@@ -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)
}
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