一:本地测试:

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();
}
}
}

4:编译,发布到Centos7虚拟机上。

5;运行客户端,测试

三:摸索过程。(由于没有找到相应文档,自以为是的操作过程),以下是错误示范,以下是错误示范,以下是错误示范

1:Gate服务中使用DB,就在Gate服务中添加DB组件:

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

3:DB服务还原到本地测试时的访问方式

4:编译,发布,启动,配置文件也上传到相应位置

运行前端,发现报错。

 

ET框架对MongoDB的使用的更多相关文章

  1. Yii2框架与MongoDB拓展、Redis拓展的安装流程

    @author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...

  2. 基于Django框架对MongoDB实现增删改查

    在上一篇中,咱们已经实现了Scrapy框架爬取数据,并且存储到Mongodb数据库, 接下来呢,,,,,,,,,,,,, 咱们就要对这些数据进行操作. 目标:从Mongodb数据库取出数据,通过Dja ...

  3. php yii框架使用MongoDb

    1.安装 运行 php composer.phar require --prefer-dist yiisoft/yii2-mongodb or add "yiisoft/yii2-mongo ...

  4. 使用Morphia框架操作mongodb

    1.  mac 下 安装mongodb sudo brew update sudo brew install mongodb sudo brew services mongodb start 2.   ...

  5. 【Node.js】二、基于Express框架 + 连接MongoDB + 写后端接口

    在上节,我们讲了如何搭建express环境,现在我们说说如何通过node.js写服务接口给前端调用 1. 首先通过MongoDB建好数据库与表格 例如,我的数据库名字为db_demo,数据库表格为go ...

  6. nodejs之使用express框架连接mongodb数据库

    var express = require('express');var router = express.Router();var app = express();var MongoClient = ...

  7. TP框架连接mongodb报错及解决办法

    mongodb版本3.4.7 1.认证错误:Failed to connect to: localhost:27017: Authentication failed on database 'test ...

  8. NODE 基于express 框架和mongoDB的cookie和session认证 和图片的上传和删除

    源码地址 https://gitee.com/zyqwasd/mongdbSession 本项目的mongodb是本地的mongodb 开启方法可以百度一下 端口是默认的27017 页面效果 1. 注 ...

  9. Morphia - mongodb之ORM框架

    一.简介 二.注解 1.@Entity 2.@Id3.@Indexed4.@Embedded5.@Transient和@Property6.@Reference 三.示例 四.参考资料 Morphia ...

  10. MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(2)-Swagger框架集成

    Swagger是什么? Swagger是一个规范且完整API文档管理框架,可以用于生成.描述和调用可视化的RESTful风格的 Web 服务.Swagger 的目标是对 REST API 定义一个标准 ...

随机推荐

  1. 测试用例Xmind转XML格式教程

    运行环境: Python版本:3.7(Python2.x和Python2.x均可) 第三方库:xmind2testlink/xmind2testcase 1.安装Python(以Python3.x为例 ...

  2. Debug调试(使用IDEA的断点调试功能,查看程序的运行过程)

    一. 1. 在有效代码行,点击行号右边的空白区域,设置断点,程序执行到断点将停止,我们可以手动来运行程序 2. 点击Debug运行模式 3. 程序停止在断点上不再执行,而IDEA最下方打开了Debug ...

  3. 康谋分享 | 在基于场景的AD/ADAS验证过程中,识别挑战性场景!

    基于场景的验证是AD/ADAS(自动驾驶和高级驾驶辅助)系统开发过程中的重要步骤,它包括对自动化系统进行一系列预定义场景的测试.测试中包含的场景越多,尤其挑战性场景越多,人们对正在测试的AD/ADAS ...

  4. 代码随想录第三天 | Leecode 203. 移除链表元素、707. 设计链表、206. 翻转链表

    Leecode 203 移除链表元素 题目链接:https://leetcode.cn/problems/remove-linked-list-elements/ 题目描述 给你一个链表的头节点 he ...

  5. AtCoder Beginner Contest 404 C-G(无F)题解

    C. Cycle Graph? 题意 给你一个 \(N\) 个顶点 \(M\) 条边的简单(无重边.自环)无向图,第 \(i\) 条边连接节点 \(A_i\) 和 \(B_i\),判断这个图是不是一个 ...

  6. 基于Kubernetes可扩展的Selenium 并行自动化测试部署及搭建(1)——Win10环境下Docker部署

    前言: 最近逛swtestacademy时候发现一篇基于kubernetes搭建selenium并行自动化测试的帖子,主要描述的是通过k8s动态扩展selenium grid来实现自动化测试用例在不同 ...

  7. MySQL 8.0 修改密码 新建用的正确方式

    mysql 更新完密码,总是拒绝连接.登录失败?MySQL8.0 不能通过直接修改 mysql.user 表来更改密码.正确更改密码的方式备注: 清空root密码MySQL8.0 不能通过直接修改 m ...

  8. 树-BST基本实现

    之前的数组, 栈, 链表, 队列等都是顺序数据结构, 这里来介绍一个非顺序数据结构, 树. 树在处理有层级相关的数据时非常有用, 还有在存储数据如数据库查询实现等场景也是高频使用. 作为一种分层数据的 ...

  9. WPF中Resx文件中添加Byte[]

    参考:https://learn.microsoft.com/zh-tw/dotnet/api/system.resources.resxresourcewriter.generate?view=wi ...

  10. HanTTS简单文档

    先下载 https://gitee.com/dhfhub/HanTTS 然后输入命令 main.py synthesize --text 你的话 --src "syllables/" ...