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();
}
}
}
4:编译,发布到Centos7虚拟机上。
5;运行客户端,测试
三:摸索过程。(由于没有找到相应文档,自以为是的操作过程),以下是错误示范,以下是错误示范,以下是错误示范
1:Gate服务中使用DB,就在Gate服务中添加DB组件:
2:修改配置,在Gate服务配置中添加DB.
3:DB服务还原到本地测试时的访问方式
4:编译,发布,启动,配置文件也上传到相应位置
运行前端,发现报错。
ET框架对MongoDB的使用的更多相关文章
- Yii2框架与MongoDB拓展、Redis拓展的安装流程
@author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...
- 基于Django框架对MongoDB实现增删改查
在上一篇中,咱们已经实现了Scrapy框架爬取数据,并且存储到Mongodb数据库, 接下来呢,,,,,,,,,,,,, 咱们就要对这些数据进行操作. 目标:从Mongodb数据库取出数据,通过Dja ...
- php yii框架使用MongoDb
1.安装 运行 php composer.phar require --prefer-dist yiisoft/yii2-mongodb or add "yiisoft/yii2-mongo ...
- 使用Morphia框架操作mongodb
1. mac 下 安装mongodb sudo brew update sudo brew install mongodb sudo brew services mongodb start 2. ...
- 【Node.js】二、基于Express框架 + 连接MongoDB + 写后端接口
在上节,我们讲了如何搭建express环境,现在我们说说如何通过node.js写服务接口给前端调用 1. 首先通过MongoDB建好数据库与表格 例如,我的数据库名字为db_demo,数据库表格为go ...
- nodejs之使用express框架连接mongodb数据库
var express = require('express');var router = express.Router();var app = express();var MongoClient = ...
- TP框架连接mongodb报错及解决办法
mongodb版本3.4.7 1.认证错误:Failed to connect to: localhost:27017: Authentication failed on database 'test ...
- NODE 基于express 框架和mongoDB的cookie和session认证 和图片的上传和删除
源码地址 https://gitee.com/zyqwasd/mongdbSession 本项目的mongodb是本地的mongodb 开启方法可以百度一下 端口是默认的27017 页面效果 1. 注 ...
- Morphia - mongodb之ORM框架
一.简介 二.注解 1.@Entity 2.@Id3.@Indexed4.@Embedded5.@Transient和@Property6.@Reference 三.示例 四.参考资料 Morphia ...
- MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(2)-Swagger框架集成
Swagger是什么? Swagger是一个规范且完整API文档管理框架,可以用于生成.描述和调用可视化的RESTful风格的 Web 服务.Swagger 的目标是对 REST API 定义一个标准 ...
随机推荐
- llamacpp转换hf、vllm运行gguf
Linux通过huggingface安装大模型 huggingface官网 https://huggingface.co/ wget https://repo.anaconda.com/minicon ...
- 【QT】使用Qxlsx读取Excel单元格中函数表达式的结果值
[QT]使用Qxlsx读取Excel单元格中函数表达式的结果值 零.起因 是这样的,目前朋友托我写一款模板生成软件,任务是先把他写的程序文件复制一份出来,然后再根据Excel中对应位置的单元格的值,修 ...
- Git Bash 无法输入中文
场景重现 有个小伙伴的电脑上 Git Bash 里死活无法输入中文, 导致 git 提交信息没法用中文写... git commit -m "无法输入中文" 解决办法 在 Git ...
- 『Plotly实战指南』--面积图绘制与应用
在数据可视化领域,面积图是一种强大而直观的工具,它通过填充线条与坐标轴之间的区域来量化数据大小, 从而帮助我们清晰地展示数据的总量.趋势变化以及不同类别之间的对比. 无论是分析随时间变化的累积量,还是 ...
- 使用DVC管理大文件变更历史(基于git)
DVC(Data Version Control) 是一个专门用于管理数据和二进制文件版本控制 的工具,它特别适合那些需要处理大量非文本文件(如图像.视频.模型.数据集等)的项目. 一般地,如果项目中 ...
- cloudflare xss绕过未修复
- 【工具】F_Record|和画世界一样录制PS的画画过程的插件(亲测好用)
亲测日期:2024/04/11 亲测版本:PS 2024 首先, 作者自己制作的使用视频在这里:https://www.bilibili.com/video/BV1bm411Z762 作者的Githu ...
- 一文掌握 Ubuntu 全场景扩容操作
此文章为搬运,原作者链接 一文掌握 Ubuntu 全场景扩容操作 - 南北东西万里程的文章 - 知乎https://zhuanlan.zhihu.com/p/707918020 为 ubuntu扩容, ...
- yysxky
哎,还有三天目标分数50分pde能否及格,我其实是有一些信心的我觉得我可以前两天觉得教资pde傻逼班里的模拟教资给我搞得非常恶心当时再加上根本本学不会,我真的会要崩溃的 现在结束了面试感觉其实还好会有 ...
- 从传统搜索到智能问答:自研 RAG 系统的技术实践与工程落地
一.引言 在数字化转型浪潮下,企业知识服务体系正经历着深刻变革.如何让用户高效获取所需信息,成为提升产品竞争力和用户满意度的关键.葡萄城作为企业级开发工具与解决方案提供商,长期致力于知识服务体系的建设 ...