Introduction

I have designed and developed game servers successfully with thrift (http://thrift.apache.org/). I am writing here some tips to share my experience. I hope that this will help someone who is just trying to solve the same problems that I have met.

Background

Thrift is mainly for scalable cross-language services development ( what it's website has written).Someone may mention Google ProtocolBuff. I have used ProtocolBuff some projects. However, I like thrift more than Google protocolbuff. There are two reasons:

  • Thrift support array and map. That is so nice.
  • The Codes that thrift framework generated are much more readable just like which I writes them myself.

When developing MMO Server, I use messages to communicate with client which is may implemented by different language e.g., as3. So thrift is very suitable for interchange messages. But there is another circumstance to use thrift.As you know the part of the logic of game server, is modified frequently. Along with project progressing, More property will be added to player object or sub structure. So we will add more fields or rows to db for storing extra data. With version issues being considered, that's much more annoying. I create a blob field to db table that stores a thrift object. With advantages of version supporting, It's easy to add more property to player.

Flash AS3 Wrap

Thrift is originally designed for RPC. How it works for RPC is easy to find if you Google it. I will not write more for this part. But there is less articles to introduce how thrift can be used to communicate message in real time system. Thrift support as3 but just for RPC. So I add some files for supporting message communicating. That another reason why I like thrift that the codes are so readable that it's so easy to extend thrift. Though I am not a professional as3 engineer (I am professional in C++), I implement that easily. I implement two functions to wrap serialization and deserialization.

public class FFUtil  {

    public static function EncodeMsg(msg:TBase, ba:ByteArray):void
{
var tran:FFMemoryTransport = new FFMemoryTransport(ba);
var proto:TBinaryProtocol = new TBinaryProtocol(tran);
msg.write(proto);
}
public static function DecodeMsg(msg:TBase, ba:ByteArray):Boolean
{
var tran:FFMemoryTransport = new FFMemoryTransport(ba);
var proto:TBinaryProtocol = new TBinaryProtocol(tran);
msg.read(proto);
return true;
}
}

I have uploaded flash_example.zip file which includes my extending as3 lib files and a exampl.as file. Actually it's a chat tutorial application using thrift.

C++ Wrap

There are some dissatisfied aspects in thrift C++ implement codes that they depend boost library. Generally it's not a big problem as most of C++ projects have been using boost. But someone who just want to integrate thrift easily finds it annoying. In some mobile app project with C++, they will not want to depend boost too. Actually I don't find good enough reason to must implement thrift of C++ version with boost. Most of use of boost in thrift are for assert test and smart pointer. Thanks to readable codes, I find it easy to wipe dependency of boost. Actually I still recommend you to use official version. But if you just want try thrift as soon as possible you can try this version of no boost. By the way, this C++ version of no boost supports binary encoding only.

int main(int argc, char **argv)
{
shared::SharedStruct msg, dest;
msg.value = "nihao";
std::string data;
ffthrift_t::EncodeToString(msg, data);
printf("data size=%u\n", data.size());
ffthrift_t::DecodeFromString(dest, data);
printf("dest value=%s\n", dest.value.c_str());
return ;
}

I have uploaded ffthrif_cpp_example.zip file which includes my extending C++ files and a example file.

For Storing

I use MySQL. There I show the model of game structure as an example. For storing players' data I design a table named player_info for players' base property such as level, gold, exp...

The field of Extra_Data is special and important. It is a blob type for storing thrift. For example, I design a such thrif file:

namespace cpp ff
namespace as3 ff
namespace py ff
struct extra_data_t {
: list<i32> val1
: string val2
: map<i32, string> val3
}

thrift-0.9.0.exe -gen cpp msg_def.thrift

I will store  extra_data_t to field of Extra_Data. There are such features:

  • Because of supporting of version, we can add more field to  extra_data_t but not to modify MySQL table structure. We have hundreds of game server so that we always update some of server first, if anything is ok, we make all servers updated to newest version. Thrift and blob make us agile enough to handle these situations. We don't need to modify db frequently and the part of db operations is stable.
  • Thrift supports list and map which it's convenient for us and it‘s codes are very readable, we can use extra_data_t as base structure of player_t class.

But we must use thrift properly. If you chose to store thrift object to db blob, you will give up query this field by SQL. So my tips are:

  • store those field as base type of db that needs to be queried by SQL. Such as level, exp, gold, socre...
  • store those field in thrift object that don't needs to queried by SQL. Though thrift object in db blob can't be queried by SQL, not mean it can be queried no way. Thanks to thrift supporting for lots of languages, thrift objects can be queried by scripts. We developed some web pages with PHP to show data from db.

Summary

  • Actually here thrift is a serialization and deserialization framework other than RPC framework.
  • RPC mechanism doesn't fit for me. I just use thrift to gen message that supports version.
  • With supporting version, thrift can be used in db storing data. We will get more agile by storing thrift object in db blob.
  • file download: http://www.codeproject.com/Articles/660671/Tips-for-thrift-Message-and-store

Tips for thrift的更多相关文章

  1. Thrift入门及Java实例演示<转载备用>

    Thrift入门及Java实例演示 作者: Michael 日期: 年 月 日 •概述 •下载配置 •基本概念 .数据类型 .服务端编码基本步骤 .客户端编码基本步骤 .数据传输协议 •实例演示(ja ...

  2. RPC学习----Thrift快速入门和Java简单示例

    一.什么是RPC? RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. RPC协议 ...

  3. C++库(Thrift)

    Thrift通信框架 0 简介 Thrift是一个软件通讯框架,用来进行可扩展且跨语言的服务的开发,最初由Facebook于2007年开发,2008年进入Apache开源项目.它结合了功能强大的软件堆 ...

  4. Thrift入门及Java实例演示

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

  5. Apache Thrift学习之一(入门及Java实例演示)

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

  6. Thrift入门

    简介 Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Has ...

  7. Thrift IDL使用方式

    I.背景 众所周知,Thrift是一个RPC的框架,其可用于不同语言之间的服务相互调用.比如最近接触到的一个运用环境: *前端使用Node.Js重构了部分我们的老旧代码(前后端未分离的SpringBo ...

  8. Thrift快速入门

    Thrift 简单示例 2017-01-19 16:47:57 首先通过先面两个示例简单感受一下Thrift(RPC)服务端与客户端之间的通信...... RPC学习----Thrift快速入门和Ja ...

  9. Apache Thrift with Java Quickstart(thrift入门及Java实例)

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl ...

随机推荐

  1. Webpack使用教程五(Babel)

    Babel是一个JavaScript编译和工具平台,使用Babel我们可以:使用新版本的JavaScript(ES6/ES2015,ES7/ES2016),尽管有些浏览器不能全部支持新特性:使用Jav ...

  2. Block传值

    typedef void(^DemoBlock)(); typedef void(^INTBlock)(int a, int b); @property(nonatomic,assign)DemoBl ...

  3. 46 个非常有用的 PHP 代码片段

    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧- ...

  4. 作业七:团队项目——Alpha版本冲刺阶段

      本次作业为期三周时间,要求各组结合所选项目并阅读教材<构建之法>第六章内容,完成项目的Alpha版本.本阶段的主要内容如下:(20分) 1. 每天组织一次站立会议,讨论每个成员的昨天进 ...

  5. 配置Linux自动挂载

    使用mount命令来挂载硬件,在Linux重启后这些挂载信息会丢失,因此对应磁盘这类硬件,需要配置自动挂载来保证系统重启时进行自动挂载. 自动挂载信息保存在文件/etc/fstab文件中 查看该文件的 ...

  6. 在Visio2010中修改默认字体的大小

    由于我常需要在Visio2010中画流程图和UML图,但是Visio2010中的字体默认8px,这对眼睛是个挑战.摸索了好久终于找到在visio2010中修改字体大小的方式. 1.点一下红色箭头所指的 ...

  7. 实验四 时序逻辑电路的VHDL设计

    一.实验目的 熟悉QuartusⅡ的VHDL文本设计过程,学习简单时序逻辑电路的设计.仿真和测试方法. 二.实验 1. 基本命题 用VHDL文本设计触发器,触发器的类型可任选一种.给出程序设计.仿真分 ...

  8. 解读SQL Server 2014可更新列存储索引——存储机制

    概述 SQL Server 2014被号称是微软数据库的一个革命性版本,其性能的提升的幅度是有史以来之最. 可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性 ...

  9. glyphicons-halflings-regular.woff 404

    此乃服务器不支持这种文件,因此需要修改一下配置,让IIS支持这种文件的访问 <system.webServer> 下增加节点: <staticContent>  <rem ...

  10. 相对路径获取项目文件 及报错 No mapping found for HTTP request with URI XXX in DispatcherServlet with name ‘springmvc’解决方法

    首先一点,WebRoot目录下的文件是都可以通过浏览器输入路径,直接读取到的 例如这样: 而WebRoot下面WEB-INF是无法浏览器输入路径直接读取的. 因为是受保护的. 如果jsp读取一个图片的 ...