ET框架對MongoDB的使用


 

一:本地測試:

1:加載DB組件

 

2:調整用戶ID :  C2G_LoginGateHandler中創建玩家時id調整。(每次重啟服務端創建小人ID是一樣的,插入數據庫會覆蓋掉上傳插入的數據)

 

3:在C2G_EnterMapHandler.cs添加測試代碼

            Player player = session.GetComponent<SessionPlayerComponent>().Player;
            // 在map服務器上創建戰斗Unit,默認第一個Map服務器
            IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
            
            Log.Info("向Map服務器發送創建玩家信息。Map Address :" + mapAddress.Address + "--" + mapAddress.Port);
            Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
            M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
            player.UnitId = createUnit.UnitId;
            response.UnitId = createUnit.UnitId;
            
//新加的代碼,測試DB
// /************ 數據庫demo ************/ //數據庫操作對象 DBProxyComponent dbProxy = Game.Scene.GetComponent<DBProxyComponent>(); // //保存到數據庫 await dbProxy.Save(player); Log.Info("--保存成功---"); //查詢賬號是否存在 Player resultd = await dbProxy.Query<Player>(player.Id); Log.Info(MongoHelper.ToJson(resultd)); //根據Account賬號查找,可能有多個 List<ComponentWithId> results = await dbProxy.Query<Player>(_account => _account.Account == player.Account); Log.Info(results.Count + ""); if (results.Count > 0) { Log.Info(MongoHelper.ToJson(results[0])); }

4:配置DB

  

5:編譯,運行

 

6:查看數據庫 

 

二:在分布式服務端使用(這里是ET5做法,ET6聽說是哪個服務使用,就哪個服務直連DB)

1:啟動一個DB服務器。加載相應組件。

 

2:添加DB配置

 

3:DB使用方法修改

不能直接使用DB組件進行DB訪問。需要使用DB時,向DB服務器發送相應請求,DB服務對數據庫做相應操作。

using System;
using System.Collections.Generic;
using System.Net;
using ETModel;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

namespace ETHotfix
{
    [MessageHandler(AppType.Gate)]
    public class C2G_EnterMapHandler : AMRpcHandler<C2G_EnterMap, G2C_EnterMap>
    {
        protected override async ETTask Run(Session session, C2G_EnterMap request, G2C_EnterMap response, Action reply)
        {
            Log.Info("--create player--");
            
            Player player = session.GetComponent<SessionPlayerComponent>().Player;
            // 在map服務器上創建戰斗Unit,默認第一個Map服務器
            IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
            
            Log.Info("向Map服務器發送創建玩家信息。Map Address :" + mapAddress.Address + "--" + mapAddress.Port);
            Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
            M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
            player.UnitId = createUnit.UnitId;
            response.UnitId = createUnit.UnitId;
            
            // // /************ 數據庫demo ************/
            // //數據庫操作對象
            // DBProxyComponent dbProxy = Game.Scene.GetComponent<DBProxyComponent>();
            // // //保存到數據庫
            // await dbProxy.Save(player);
            // Log.Info("--保存成功---");
            // //查詢賬號是否存在
            // Player resultd = await dbProxy.Query<Player>(player.Id);
            // Log.Info(MongoHelper.ToJson(resultd));
            //
            // //根據Account賬號查找,可能有多個
            // List<ComponentWithId> results = await dbProxy.Query<Player>(_account => _account.Account == player.Account);
            // Log.Info(results.Count + "");
            // if (results.Count > 0)
            // {
            //     Log.Info(MongoHelper.ToJson(results[0]));
            // }
            
            /************ 數據庫demo 分布式服務器中************/
            //獲取DB服務器地址
            IPEndPoint dbAddress = StartConfigComponent.Instance.DBConfig.GetComponent<InnerConfig>().IPEndPoint;
            
            Log.Info("獲取DB服務器地址 :" + dbAddress.Address + "--" + dbAddress.Port);
            Session dbSession = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
            await dbSession.Call(new DBSaveRequest { Component = player});
            
            Log.Info("--保存成功---");
            
            DBQueryResponse dbQueryResponse = (DBQueryResponse)await dbSession.Call(new DBQueryRequest { CollectionName ="Player", Id = player.Id });
            Player result = (Player)dbQueryResponse.Component;
            Log.Info(MongoHelper.ToJson(result));
            
            //序列化查詢json,來着  await dbProxy.Query<Player>(_account => _account.Account == player.Account); 函數中
            ExpressionFilterDefinition<Player> filter = new ExpressionFilterDefinition<Player>(_account => _account.Account == player.Account);
            IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
            IBsonSerializer<Player> documentSerializer = serializerRegistry.GetSerializer<Player>();
            string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
            
            DBQueryJsonResponse resultsList = (DBQueryJsonResponse)await dbSession.Call(new DBQueryJsonRequest { CollectionName ="Player",Json = json});
            List<ComponentWithId> list = resultsList.Components;
            Log.Info(list.Count + "---size");
            if (list.Count > 0)
            {
                Log.Info(MongoHelper.ToJson(list[0]));
            }

            reply();
        }
    }
}
View Code

 

4:編譯,發布到Centos7虛擬機上。

 

  

5;運行客戶端,測試

 

 

 

 

 

 

三:摸索過程。(由於沒有找到相應文檔,自以為是的操作過程),以下是錯誤示范,以下是錯誤示范,以下是錯誤示范

1:Gate服務中使用DB,就在Gate服務中添加DB組件:

 

 2:修改配置,在Gate服務配置中添加DB.

 

3:DB服務還原到本地測試時的訪問方式

 

4:編譯,發布,啟動,配置文件也上傳到相應位置

 

 

 運行前端,發現報錯。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM