Commit 328086f7 by 孙龙

up

parent 66b07f8e
package config
import (
"github.com/ichunt2019/cfg/lib"
)
type BaseDatabase struct {
DataSourceName string
MaxIdleCons int
MaxOpenCons int
Prefix string
}
type GroupDatabase struct {
DataSourceName []string
MaxIdleCons int
MaxOpenCons int
Prefix string
}
//单数据库配置
func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) {
return map[string]BaseDatabase{
"micro": {
DataSourceName: lib.Instance("db").GetString("micro.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"),
},
}
}
//主从mysql数据库配置
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"),
},
}
}
package config
import (
"github.com/ichunt2019/cfg/lib"
"time"
)
type RedisGroupDatabase struct {
MasterHost string
Password string //master密码
SlaveHost []string
ReadonlyPassword string //从服务器密码
DialTimeout time.Duration //连接超时
MinIdleConns int //空间链接
ReadTimeout time.Duration //读超时
WriteTimeout time.Duration //写超时
}
//多数据库配置
func BuildRedisGroupConfigs() ( map[string]RedisGroupDatabase) {
//fmt.Println("6666666666")
//fmt.Println(lib.Instance("redis").GetStringSlice("api.slave.host"))
return map[string]RedisGroupDatabase{
"api": {
MasterHost: lib.Instance("redis").GetString("api.master.host"),
Password:lib.Instance("redis").GetString("api.master.password"),
SlaveHost: lib.Instance("redis").GetStringSlice("api.slave.host"),
ReadonlyPassword:lib.Instance("redis").GetString("api.slave.password"),
DialTimeout: time.Duration(lib.Instance("redis").GetInt("api.dial_timeout"))*time.Second,
ReadTimeout: time.Duration(lib.Instance("redis").GetInt("api.read_timeout"))*time.Second,
WriteTimeout: time.Duration(lib.Instance("redis").GetInt("api.write_timeout"))*time.Second,
MinIdleConns:lib.Instance("redis").GetInt("api.min_idle_conns"),
},
}
}
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
common "ichunt_golang/app/common/function"
"github.com/ichunt2019/cfg/lib"
xlog "github.com/ichunt2019/lxLog/log"
common "ichunt_golang/app/common/function"
"ichunt_golang/app/service"
)
func Ping(ctx *gin.Context) {
xlog.Instance("sku").Info("ping")
service.GetData()
common.Output(ctx, 0, "success",nil)
}
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"))
//b := lib.Instance("db").GetStringSlice("liexin.slave.data_source_name")
//fmt.Println(b)
//fmt.Println(reflect.TypeOf(b))
common.Output(ctx, 0, "success",nil)
}
\ No newline at end of file
package dao
import (
_ "github.com/lib/pq"
"github.com/xormplus/xorm"
"fmt"
"github.com/go-redis/redis/v7"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"github.com/ichunt2019/cfg/lib"
"ichunt_golang/app/common/config"
"time"
redispool "github.com/ichunt2019/go-redis-pool"
"sync"
)
var (
once sync.Once
Dao *dao
)
type dao struct {
db map[string]*xorm.Engine //非主从mysql数据库
dbGroup map[string]*xorm.EngineGroup //mysql主从
redisGroup map[string]*redispool.Pool
}
//获取db实例
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]
}
//获取主从db实例
func(self *dao) GetRedisDbGroup(databases string) *redispool.Pool{
return self.redisGroup[databases]
}
type Dao struct {
LieXinDb *xorm.EngineGroup
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)
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)
}
//日志打印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)
}
return d
}
func redisSetup(d *dao) *dao{
var err error
redisGroupList := config.BuildRedisGroupConfigs()
fmt.Println(redisGroupList)
for redisServerName,redisInfo:= range redisGroupList{
d.redisGroup[redisServerName], err = redispool.NewHA(&redispool.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服务器 针对从服务器
})
if err != nil {
panic(err)
}
func New() (Dao){
conns := lib.Instance("config.mysql.liexin")
lieXinDb, err := xorm.NewEngineGroup("mysql",conns)
if err != nil {
panic(err)
}
return Dao{LieXinDb:lieXinDb}
return d
}
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]*redispool.Pool,0)
Dao = mysqlSetup(Dao)
Dao = redisSetup(Dao)
})
}
\ No newline at end of file
package dao
import "fmt"
func GetUser(){
res,err :=Dao.GetDb("micro").QueryString("select service_name from lie_service_info where is_delete = 1 ")
if err != nil{
}
fmt.Println(res)
res,err =Dao.GetDb("micro").QueryString("select service_desc from lie_service_info where is_delete = 1 ")
if err != nil{
}
fmt.Println(res)
//Dao.GetRedisDbGroup("api").Set("abcdef","123456789",0)
fmt.Println(Dao.GetRedisDbGroup("api").Get("abcdef").String())
}
package service
import "ichunt_golang/app/dao"
func GetData(){
dao.GetUser()
}
......@@ -3,6 +3,7 @@ package boot
import (
"github.com/ichunt2019/cfg/lib"
xlog "github.com/ichunt2019/lxLog/log"
"ichunt_golang/app/dao"
)
func Init(configPath string,logPath string)(err error){
err = lib.Init(configPath)
......@@ -11,5 +12,8 @@ func Init(configPath string,logPath string)(err error){
}
xlog.Init(logPath,"sku","spu","request")
//初始化数据库
dao.Init()
return
}
......@@ -26,11 +26,7 @@ dob = 1979-05-27T07:32:00Z # 日期时间是一等公民。为什么不呢?
data = [["gamma", "delta"],[1, 2]]
[mysql]
liexin = ["mysql://mysql:root@localhost:5432/test?sslmode=disable;",
"mysql://mysql:root@localhost:5432/test1?sslmode=disable;",
"mysql://mysql:root@localhost:5432/test2?sslmode=disable"
]
......
[xorm]
ShowSQL = false
[micro]
data_source_name = "micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing"
max_open_conn = 20
max_idle_conn = 10
table_prefix = ""
max_conn_life_time = 100
[liexin]
max_open_conn = 20
max_idle_conn = 10
table_prefix = ""
max_conn_life_time = 100
data_source_name = [
"micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing",
"micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing",
"micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing",
]
[cms]
dns = "micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing"
max_open_conn = 20
max_idle_conn = 10
table_prefix = ""
max_conn_life_time = 100
[sku]
[sku.sku_0]
dns = "micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing"
max_open_conn = 20
max_idle_conn = 10
table_prefix = ""
max_conn_life_time = 100
[sku.sku_1]
dns = "micro_service:lie_micro_service#zsyM@tcp(192.168.2.232:3306)/lie_micro_service?charset=utf8&parseTime=true&loc=Asia%2FChongqing"
max_open_conn = 20
max_idle_conn = 10
table_prefix = ""
max_conn_life_time = 100
[api]
dial_timeout = 20
min_idle_conns = 10
read_timeout = 10
write_timeout = 10
[api.master]
host = "192.168.1.235:6379"
password = "icDb29mLy2s"
[api.slave]
password = "icDb29mLy2s"
host = [
"192.168.1.235:6379",
"192.168.1.237:6379",
]
......@@ -15,19 +15,22 @@ require (
github.com/fatih/structs v1.1.0 // indirect
github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/go-redis/redis/v7 v7.4.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gorilla/sessions v1.2.1 // indirect
github.com/ichunt2019/cfg v0.0.0-20210225081543-828ee9831d70
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
github.com/lib/pq v1.9.0
github.com/lib/pq v1.9.0 // indirect
github.com/mattn/go-sqlite3 v1.14.6 // indirect
github.com/spf13/viper v1.7.1
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tealeg/xlsx v1.0.5 // indirect
github.com/tidwall/gjson v1.6.8 // indirect
github.com/xormplus/builder v0.0.0-20200331055651-240ff40009be // indirect
github.com/xormplus/xorm v0.0.0-20210107091022-175d736afaae
github.com/xormplus/xorm v0.0.0-20210107091022-175d736afaae // indirect
google.golang.org/grpc/examples v0.0.0-20210226164526-c949703b4b98 // indirect
gopkg.in/flosch/pongo2.v3 v3.0.0-20141028000813-5e81b817a0c4 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
......
package lib
import (
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"ichunt_golang/app/common/config"
"github.com/ichunt2019/cfg/lib"
)
var DatabaseConMap map[string]*xorm.Engine
func Setup() error {
DatabaseConMap = make(map[string]*xorm.Engine, 0)
DatabaseList := config.BuildDatabaseList()
var err error
//循环生成数据库链接
for conName, db := range DatabaseList {
DatabaseConMap[conName], err = xorm.NewEngine("mysql", db.DataSourceName)
if err != nil {
panic(err)
}
//日志打印SQL
ShowSql := lib.Instance("db").GetBool("xorm.ShowSQL")
DatabaseConMap[conName].ShowSQL(ShowSql)
//设置连接池的空闲数大小
DatabaseConMap[conName].SetMaxIdleConns(db.MaxIdleCons)
//设置最大打开连接数
DatabaseConMap[conName].SetMaxOpenConns(db.MaxOpenCons)
}
return nil
}
func Conn(conName string) *xorm.Engine {
return DatabaseConMap[conName]
}
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