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:编译,发布,启动,配置文件也上传到相应位置

 

 

 运行前端,发现报错。

 

 

 

posted @ 2020-12-11 15:55  Foto_CShow  阅读(1251)  评论(0编辑  收藏  举报