Browse Source

负载同步

zhengtao 7 months ago
parent
commit
70dca1cc86

+ 2 - 1
game/game_cluster/internal/mdb/component.go

@@ -11,6 +11,7 @@ import (
 
 type ActorDB struct {
 	cactor.Base
+	Name string
 }
 
 var (
@@ -19,7 +20,7 @@ var (
 )
 
 func (p *ActorDB) AliasID() string {
-	return "db"
+	return "db" + p.Name
 }
 
 // OnInit Actor初始化前触发该函数

+ 2 - 2
game/game_cluster/nodes/game/game.go

@@ -39,8 +39,8 @@ func main() {
 	app.Register(mhayaMongo.NewComponent())
 
 	app.AddActors(
-		&player.ActorPlayers{},
-		&mdb.ActorDB{},
+		&player.ActorPlayers{Name: nodeId},
+		&mdb.ActorDB{Name: nodeId},
 	)
 
 	app.Startup()

+ 2 - 2
game/game_cluster/nodes/game/game/game.go

@@ -38,8 +38,8 @@ func Run(profileFilePath, nodeId string) {
 	app.Register(mhayaMongo.NewComponent())
 
 	app.AddActors(
-		&player.ActorPlayers{},
-		&mdb.ActorDB{},
+		&player.ActorPlayers{Name: nodeId},
+		&mdb.ActorDB{Name: nodeId},
 	)
 
 	app.Startup()

+ 2 - 1
game/game_cluster/nodes/game/module/player/actor_players.go

@@ -16,12 +16,13 @@ type (
 	// ActorPlayers 玩家总管理actor
 	ActorPlayers struct {
 		pomelo.ActorBase
+		Name          string
 		childExitTime time.Duration
 	}
 )
 
 func (p *ActorPlayers) AliasID() string {
-	return "player"
+	return "player" + p.Name
 }
 
 func (p *ActorPlayers) OnInit() {

+ 1 - 1
game/game_cluster/nodes/web/controller/controller.go

@@ -204,7 +204,7 @@ func (p *Controller) auth(c *mhayaGin.Context) {
 	params["ip"] = c.ClientIP()
 
 	// 输出客户端 IP 地址
-	mhayaLogger.Debugf("[auth] [c.ClientIP() => ip = %s]", c.ClientIP())
+	//mhayaLogger.Debugf("[auth] [c.ClientIP() => ip = %s]", c.ClientIP())
 	// invoke login
 	sdkInvoke.Login(config, params, func(statusCode int32, result sdk.Params, error ...error) {
 		if code.IsFail(statusCode) {

+ 2 - 2
game/game_cluster/nodes/web/sdk/quick_sdk.go

@@ -73,7 +73,7 @@ func (p quickSdk) Login(config *data.SdkConfigRow, params Params, callback Callb
 		return
 	}
 
-	targetPath := cfacade.NewChildPath(nodeId, "player", account.UserName)
+	targetPath := cfacade.NewChildPath(nodeId, "player"+nodeId, account.UserName)
 
 	callback(code.OK, map[string]string{
 		sessionKey.PlayerID:   account.UserName, //返回 quick的uid做为 open id
@@ -102,7 +102,7 @@ func (p quickSdk) Reconnect(token *token.Token) (int32, *token.Token) {
 			clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", token.OpenID, nodeId, err)
 			return code.AccountAuthFail, nil
 		}
-		path := cfacade.NewChildPath(nodeId, "player", token.PlayerID)
+		path := cfacade.NewChildPath(nodeId, "player"+nodeId, token.PlayerID)
 		token.TargetPath = path
 	}
 	return code.OK, token

+ 17 - 21
game/game_cluster/nodes/web/sdk/sdk.go

@@ -98,31 +98,27 @@ func SetNode(app cfacade.IApplication) (string, int32) {
 	if lNode > 0 {
 		m, err := mdb.RDB.HGetAll(context.Background(), constant.ServerLoadHKey).Result()
 		if err != nil {
-			nodeId = list[key].GetNodeId()
+			mhayaLogger.Warnf("[SetNode] Load too high. err=%v", err)
 		}
-		if len(m) == 0 {
-			nodeId = list[key].GetNodeId()
-		} else {
-			var maps = make(map[string]int, 10)
-			for _, v := range list {
-				if d, ok := m[v.GetNodeId()]; ok {
-					p, ok := mhayaString.ToInt(d)
-					if !ok {
-						nodeId = list[key].GetNodeId()
-					}
-					maps[v.GetNodeId()] = p
-				} else {
-					maps[v.GetNodeId()] = 0
+		var maps = make(map[string]int, 10)
+		for _, v := range list {
+			if d, ok := m[v.GetNodeId()]; ok {
+				p, ok := mhayaString.ToInt(d)
+				if !ok {
+					nodeId = list[key].GetNodeId()
 				}
+				maps[v.GetNodeId()] = p
+			} else {
+				maps[v.GetNodeId()] = 0
 			}
-			wwr := NewWeightedRoundRobin(maps)
-			info := wwr.Pick()
-			if info == nil {
-				mhayaLogger.Warnf("[SetNode] Load too high. data=%v", maps)
-				return "", code.Error
-			}
-			nodeId = info.NodeId
 		}
+		wwr := NewWeightedRoundRobin(maps)
+		info := wwr.Pick()
+		if info == nil {
+			mhayaLogger.Warnf("[SetNode] Load too high. data=%v", maps)
+			return "", code.Error
+		}
+		nodeId = info.NodeId
 	}
 	return nodeId, code.OK
 }

+ 1 - 1
game/game_cluster/nodes/web/web.go

@@ -32,7 +32,7 @@ func main() {
 	app.Register(mhayaMongo.NewComponent())
 
 	app.AddActors(
-		&mdb.ActorDB{},
+		&mdb.ActorDB{Name: "m-web-1"},
 	)
 	// 加载sdk逻辑
 	sdk.Init(app)

+ 1 - 1
game/game_cluster/nodes/web/web/web.go

@@ -32,7 +32,7 @@ func Run(profileFilePath, nodeId string) {
 	app.Register(mhayaMongo.NewComponent())
 
 	app.AddActors(
-		&mdb.ActorDB{},
+		&mdb.ActorDB{Name: nodeId},
 	)
 	// 加载sdk逻辑
 	sdk.Init(app)