Tips for thrift

简介: Introduction I have designed and developed game servers successfully with thrift (http://thrift.apache.

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 0;
}

 

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 {
  1: list<i32> val1
  2: string    val2
  3: 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
目录
相关文章
|
测试技术 流计算
gRPC阅读日记(四)Client-side streaming RPC and Bidirectional streaming RPC
gRPC阅读日记(四)Client-side streaming RPC and Bidirectional streaming RPC
|
分布式计算 数据挖掘 程序员
Thrift简介—续
Thrift简介—续
Thrift简介—续
|
存储 自然语言处理 负载均衡
Thrift简介
Thrift简介
|
XML JSON 自然语言处理
JavaWeb - JSON、Protobuf、Thrift、MessagePack 对比和开发指南
JavaWeb - JSON、Protobuf、Thrift、MessagePack 对比和开发指南
830 0
JavaWeb - JSON、Protobuf、Thrift、MessagePack 对比和开发指南
|
JSON 网络协议 Java
thrift 原理浅析
thrift 原理浅析
322 0
thrift 原理浅析
|
SQL 分布式计算 Java
Hbase入门(五)——客户端(Java,Shell,Thrift,Rest,MR,WebUI)
Hbase的客户端有原生java客户端,Hbase Shell,Thrift,Rest,Mapreduce,WebUI等等。 下面是这几种客户端的常见用法。
1571 0
Hbase入门(五)——客户端(Java,Shell,Thrift,Rest,MR,WebUI)
|
编译器 Apache
Thrift架构与使用方法
Thrift架构与使用方法
242 0
Thrift架构与使用方法
|
SQL 分布式计算 Hadoop
Hive Tips
在Hive中,某些小技巧可以让我们的Job执行得更快,有时一点小小的改动就可以让性能得到大幅提升,这一点其实跟SQL差不多。 首先,Hive != SQL,虽然二者的语法很像,但是Hive最终会被转化成MapReduce的代码去执行,所以数据库的优化原则基本上都不适用于 Hive。
1426 0
Flink1.7.2 Source、Window数据交互源码分析
Source如何按partition发射数据到对应的Window Window 如何处理对应的Source发过来的对应的partition数据 理解Flink 数据从Source到Window,上下游数据交换的过程
1652 0
|
Java 测试技术
Thrift源码解析--TBinaryProtocol
本文为原创:http://www.cnblogs.com/leehfly/p/4958206.html,未经许可禁止转载。 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚。
1439 0