diff --git a/.gitignore b/.gitignore index 62e4e995b..ec605a4ef 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ pb.bat cmd/v2/.cache.meta cmd/v2/my.db .idea/ +bin/cross.json +bin/gm.json diff --git a/bin/cross.json b/bin/cross.json deleted file mode 100644 index 5ca6a6600..000000000 --- a/bin/cross.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "dfcross_2": { - "AreaId": "dfcross_2", - "LoaclDB": { - "RedisIsCluster": false, - "RedisAddr": [ - "10.0.0.9:10011" - ], - "RedisPassword": "li13451234", - "RedisDB": 12, - "MongodbUrl": "mongodb://10.0.0.9:10013", - "MongodbDatabase": "dreamfactory12" - }, - "ServiceList": { - "df03": { - "RedisIsCluster": false, - "RedisAddr": [ - "10.0.0.9:10011" - ], - "RedisPassword": "li13451234", - "RedisDB": 3, - "MongodbUrl": "mongodb://10.0.0.9:10013", - "MongodbDatabase": "dreamfactory3" - } - } - } -} \ No newline at end of file diff --git a/bin/gm.json b/bin/gm.json deleted file mode 100644 index 79ec98fc3..000000000 --- a/bin/gm.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "AreaId": "df03", - "ConsulAddr": [ - "10.0.0.9:10012" - ], - "IsCross": false, - "BelongCrossServerId": "dfcross_2", - "LoaclDB": { - "RedisIsCluster": false, - "RedisAddr": [ - "10.0.0.9:10011" - ], - "RedisPassword": "li13451234", - "RedisDB": 3, - "MongodbUrl": "mongodb://10.0.0.9:10013", - "MongodbDatabase": "dreamfactory3" - }, - "Mainte": "127.0.0.1:7896", - "MaintePort": 9572, - "Gateways": [ - "127.0.0.1:7895" - ], - "GatewayPorts": [ - 9571 - ], - "Workers": [ - "127.0.0.1:9573" - ] -} \ No newline at end of file diff --git a/comm/gameconfig.go b/comm/gameconfig.go index 4981936c2..91c8999ee 100644 --- a/comm/gameconfig.go +++ b/comm/gameconfig.go @@ -21,7 +21,7 @@ type DBConfig struct { RedisIsCluster bool //redis 是否是集群模式 RedisAddr []string //redis 地址 RedisPassword string //redis 密码 - RedisDB int32 //redis db + RedisDB int //redis db MongodbUrl string //mgondb 地址 MongodbDatabase string //数据库地址 } diff --git a/modules/gateway/configure_comp.go b/modules/gateway/configure_comp.go index 4403fddff..0955db306 100644 --- a/modules/gateway/configure_comp.go +++ b/modules/gateway/configure_comp.go @@ -40,7 +40,9 @@ func (this *configureComp) GetMsgDistribute(msgmid, msguid string) (rule string, rule = item.Routrules } else { msgid := strings.ToLower(fmt.Sprintf("%s.%s", msgmid, msguid)) - item, ok = v.(*cfg.GameMsgdistrib).GetDataMap()[msgid] + if item, ok = v.(*cfg.GameMsgdistrib).GetDataMap()[msgid]; ok { + rule = item.Routrules + } } } return diff --git a/services/cmd/main.go b/services/cmd/main.go index a9b45d2c3..baa2436dc 100644 --- a/services/cmd/main.go +++ b/services/cmd/main.go @@ -24,8 +24,9 @@ import ( 服务描述:通过读取游戏json配置,启动服务程序 */ var ( - cpath = flag.String("g", "./gm.json", "游戏区服配置") //服务列表下标 - sindex = flag.Int("i", -1, "曲阜列表下标") //服务列表下标 + gmpath = flag.String("g", "./gm.json", "游戏区服配置") //服务列表下标 + crosspath = flag.String("c", "./cross.json", "游戏跨服配置") //服务列表下标 + sid = flag.String("i", "", "曲阜列表下标") //服务列表下标 ) var startCmd = &cobra.Command{ Use: "start", @@ -79,7 +80,7 @@ func Execute() { //启动程序 func start() { - if config, err := readergmconf(*cpath); err != nil { + if config, err := readergmconf(*gmpath); err != nil { log.Error("读取区服配置失败!", log.Field{Key: "err", Value: err}) } else { var ( @@ -90,8 +91,12 @@ func start() { if ss, err := rederServiceSttings(config); err != nil { log.Error("转换服务配置异常!", log.Field{Key: "err", Value: err}) } else { - for i, v := range ss { - if *sindex == -1 || *sindex == i { + for _, v := range ss { + if *sid == "" || fmt.Sprintf("%s_%s", v.Tag, *sid) == v.Id { + if err = writeServiceConfig(fmt.Sprintf("./conf/%s.yaml", v.Id), v); err != nil { + log.Error("写入配置文件失败!", log.Field{Key: "err", Value: err}) + return + } switch v.Type { case comm.Service_Gateway: //网关服务 gateways = append(gateways, v) @@ -139,19 +144,18 @@ func start() { //关闭程序 func stop() { - if config, err := readergmconf(*cpath); err != nil { + if config, err := readergmconf(*gmpath); err != nil { log.Error("读取区服配置失败!", log.Field{Key: "err", Value: err}) } else { if ss, err := rederServiceSttings(config); err != nil { log.Error("转换服务配置异常!", log.Field{Key: "err", Value: err}) } else { - for i, v := range ss { - if *sindex == -1 || *sindex == i { + for _, v := range ss { + if *sid == "" || fmt.Sprintf("%s_%s", v.Tag, *sid) == v.Id { stopService(v) } } - } } } @@ -162,26 +166,24 @@ func rederServiceSttings(config *comm.GameConfig) (ss []*core.ServiceSttings, er var ( ip string port int - n int sseting *core.ServiceSttings ) if ip, port, err = parseaddr(config.Mainte); err != nil { return } else { - if sseting, err = convertServiceSttings(config, n, comm.Service_Mainte, ip, config.MaintePort, port); err != nil { + if sseting, err = convertServiceSttings(config, 0, comm.Service_Mainte, ip, config.MaintePort, port); err != nil { return } - n++ + ss = append(ss, sseting) } - for _, v := range config.Workers { + for i, v := range config.Workers { if ip, port, err = parseaddr(v); err != nil { return } else { - if sseting, err = convertServiceSttings(config, n, comm.Service_Worker, ip, port, 0); err != nil { + if sseting, err = convertServiceSttings(config, i, comm.Service_Worker, ip, port, 0); err != nil { return } - n++ ss = append(ss, sseting) } } @@ -189,10 +191,9 @@ func rederServiceSttings(config *comm.GameConfig) (ss []*core.ServiceSttings, er if ip, port, err = parseaddr(v); err != nil { return } else { - if sseting, err = convertServiceSttings(config, n, comm.Service_Worker, ip, config.GatewayPorts[i], port); err != nil { + if sseting, err = convertServiceSttings(config, i, comm.Service_Gateway, ip, config.GatewayPorts[i], port); err != nil { return } - n++ ss = append(ss, sseting) } } @@ -221,6 +222,9 @@ func readergmconf(path string) (config *comm.GameConfig, err error) { //转换游戏服务配置 func convertServiceSttings(config *comm.GameConfig, id int, stype string, ip string, rport int, lport int) (sseting *core.ServiceSttings, err error) { sseting = &core.ServiceSttings{} + sseting.Tag = config.AreaId + sseting.Ip = ip + sseting.Port = rport sseting.Modules = make(map[string]map[string]interface{}) sseting.Sys = make(map[string]map[string]interface{}) sseting.Sys["rpcx"] = map[string]interface{}{ @@ -228,6 +232,7 @@ func convertServiceSttings(config *comm.GameConfig, id int, stype string, ip str } switch stype { case comm.Service_Gateway: //网关服务 + sseting.Id = fmt.Sprintf("%s_%s%d", config.AreaId, comm.Service_Gateway, id) sseting.Type = comm.Service_Gateway sseting.Sys["rpcx"]["RpcxStartType"] = 1 sseting.Modules["gateway"] = map[string]interface{}{ @@ -236,10 +241,12 @@ func convertServiceSttings(config *comm.GameConfig, id int, stype string, ip str } break case comm.Service_Worker: //业务服务 + sseting.Id = fmt.Sprintf("%s_%s%d", config.AreaId, comm.Service_Worker, id) sseting.Type = comm.Service_Worker sseting.Sys["rpcx"]["RpcxStartType"] = 0 break case comm.Service_Mainte: //维护服务 + sseting.Id = fmt.Sprintf("%s_%s", config.AreaId, comm.Service_Mainte) sseting.Type = comm.Service_Mainte sseting.Sys["rpcx"]["RpcxStartType"] = 0 sseting.Modules["web"] = map[string]interface{}{ @@ -252,11 +259,6 @@ func convertServiceSttings(config *comm.GameConfig, id int, stype string, ip str err = fmt.Errorf("服务类型异常 stype:%s", sseting.Type) return } - sseting.Id = fmt.Sprintf("%s_s%d", config.AreaId, id) - sseting.Tag = config.AreaId - sseting.Ip = ip - sseting.Port = rport - sseting.Sys["log"] = map[string]interface{}{ "FileName": fmt.Sprintf("./log/%s.log", sseting.Id), "Loglevel": config.Loglevel, @@ -265,15 +267,16 @@ func convertServiceSttings(config *comm.GameConfig, id int, stype string, ip str sseting.Sys["configure"] = map[string]interface{}{ "ConfigurePath": "./json", } - sseting.Sys["db"] = map[string]interface{}{} - sseting.Sys["db"]["Loacl"] = map[string]interface{}{ - "Enabled": true, + sseting.Sys["db"] = map[string]interface{}{ + "IsCross": config.IsCross, + "CrossTag": config.AreaId, "RedisIsCluster": config.LoaclDB.RedisIsCluster, "RedisAddr": config.LoaclDB.RedisAddr, "RedisPassword": config.LoaclDB.RedisPassword, "RedisDB": config.LoaclDB.RedisDB, "MongodbUrl": config.LoaclDB.MongodbUrl, "MongodbDatabase": config.LoaclDB.MongodbDatabase, + "CrossConfig": *crosspath, } return } diff --git a/sys/db/db.go b/sys/db/db.go index 807cd13e9..974467a74 100644 --- a/sys/db/db.go +++ b/sys/db/db.go @@ -1,7 +1,12 @@ package db import ( + "fmt" + "go_dreamfactory/comm" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/utils/codec/json" + "io/ioutil" + "os" "sync" ) @@ -26,25 +31,72 @@ type DB struct { } func (this *DB) init() (err error) { - if this.local, err = newDBConn(this.options.Loacl); err != nil { + if this.local, err = newDBConn(DBConfig{ + RedisIsCluster: this.options.RedisIsCluster, + RedisAddr: this.options.RedisAddr, + RedisPassword: this.options.RedisPassword, + RedisDB: this.options.RedisDB, + MongodbUrl: this.options.MongodbUrl, + MongodbDatabase: this.options.MongodbDatabase, + }); err != nil { return } - if this.options.Cross.Enabled { - if this.cross, err = newDBConn(this.options.Cross); err != nil { + err = this.readercrossconf(this.options.CrossConfig) + return +} + +//读取游戏配置文件 +func (this *DB) readercrossconf(path string) (err error) { + config := make(comm.CrossConfigs, 0) + var ( + jsonFile *os.File + byteValue []byte + ) + if jsonFile, err = os.Open(path); err != nil { + return + } else { + defer jsonFile.Close() + if byteValue, err = ioutil.ReadAll(jsonFile); err != nil { return } - } - if this.options.ServerList != nil && len(this.options.ServerList) > 0 { - this.servers = make(map[string]*DBConn, len(this.options.ServerList)) - for k, v := range this.options.ServerList { - if this.servers[k], err = newDBConn(v); err != nil { - return + if err = json.Unmarshal(byteValue, &config); err != nil { + return + } + if cf, ok := config[this.options.CrossTag]; !ok { + err = fmt.Errorf("no found Crossconfig:%s", this.options.CrossTag) + return + } else { + if !this.options.IsCross { + if this.cross, err = newDBConn(DBConfig{ + RedisIsCluster: cf.LoaclDB.RedisIsCluster, + RedisAddr: cf.LoaclDB.RedisAddr, + RedisPassword: cf.LoaclDB.RedisPassword, + RedisDB: cf.LoaclDB.RedisDB, + MongodbUrl: cf.LoaclDB.MongodbUrl, + MongodbDatabase: cf.LoaclDB.MongodbDatabase, + }); err != nil { + log.Error("comment db err!", log.Field{Key: "stag", Value: cf.AreaId}, log.Field{Key: "db", Value: cf.LoaclDB}, log.Field{Key: "err", Value: err}) + return + } + } else { + for k, v := range cf.ServiceList { + if this.servers[k], err = newDBConn(DBConfig{ + RedisIsCluster: v.RedisIsCluster, + RedisAddr: v.RedisAddr, + RedisPassword: v.RedisPassword, + RedisDB: v.RedisDB, + MongodbUrl: v.MongodbUrl, + MongodbDatabase: v.MongodbDatabase, + }); err != nil { + log.Error("comment db err!", log.Field{Key: "stag", Value: k}, log.Field{Key: "db", Value: v}, log.Field{Key: "err", Value: err}) + return + } + } } } } return } - func (this *DB) Local() *DBConn { if this.local == nil { log.Panic("LocalDBConn on init") diff --git a/sys/db/options.go b/sys/db/options.go index 3bbe5c7ab..e2f8ab6b4 100644 --- a/sys/db/options.go +++ b/sys/db/options.go @@ -8,7 +8,6 @@ import ( //DB层配置 type DBConfig struct { - Enabled bool //是否生效 RedisIsCluster bool //是否是集群 RedisAddr []string //redis 的集群地址 RedisPassword string //redis的密码 @@ -19,33 +18,19 @@ type DBConfig struct { type Option func(*Options) type Options struct { - Loacl DBConfig //本服配置 - Cross DBConfig //跨服配置 - ServerList map[string]DBConfig //服务列表配置 - Debug bool //日志是否开启 - Log log.ILogger + IsCross bool //是否是跨服 + CrossTag string //跨服区服id + RedisIsCluster bool //是否是集群 + RedisAddr []string //redis 的集群地址 + RedisPassword string //redis的密码 + RedisDB int //数据库位置 + MongodbUrl string //数据库连接地址 + MongodbDatabase string //数据库名 + CrossConfig string //跨服配置文件地址 + Debug bool //日志是否开启 + Log log.ILogger } -//设置本服配置 -func SetLoacl(v DBConfig) Option { - return func(o *Options) { - o.Loacl = v - } -} - -//设置本服配置 -func SetCross(v DBConfig) Option { - return func(o *Options) { - o.Cross = v - } -} - -//设置跨服区服列表 -func SetServerList(v map[string]DBConfig) Option { - return func(o *Options) { - o.ServerList = v - } -} func SetDebug(v bool) Option { return func(o *Options) { o.Debug = v @@ -58,11 +43,7 @@ func SetLog(v log.ILogger) Option { } } func newOptions(config map[string]interface{}, opts ...Option) (options *Options, err error) { - options = &Options{ - Loacl: DBConfig{}, - Cross: DBConfig{}, - ServerList: make(map[string]DBConfig), - } + options = &Options{} if config != nil { if err = mapstructure.Decode(config, options); err != nil { return