• thrift使用案例


    参考资料:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/

    首先是定义thrift IDL接口,如下(SunTelTc.thrift):

    
    namespace cpp SunTelTc
    namespace java SunTelTc
    namespace go SunTelTc
    
    struct TCRequest {
      1: required string desString,
      2: required string accountId,
      3: required i64   tmpTime,
      4: required string caller,
      5: required string called,
      6: optional string servletUrl,
      7: optional string otherParam,
    }
    
    struct TCResponse {
      1: required i32 state,
      2: required string msg,
      3: optional string taskId,
      4: optional string otherParam,
    }
    
    struct TCXdrReq {
      1: required string taskId,
      2: required string ecp,
      3: required i64 t100,
      4: required i64 t180,
      5: required i64 t200,
      6: required i64 t301,
      7: required i64 t302,
      8: required i64 t401,
      9: required i64 t402,
      10: required i64 t501,
      11: optional string otherParam,
    }
    
    struct TCXdrRes {
      1: required i32 state,
      2: required string msg,
    }
    
    /*
    exception InvalidRequest {
      1: i32 what,
      2: string why
    }*/
    
    service TcRequest {
        void ping(),
        TCResponse SendTcReuest(1:TCRequest request),
        oneway void SendTcRequestNonblock(1:TCRequest request),
    }
    
    service TcXdr {
        TCXdrRes ReportXdr (1:TCXdrReq xdr);
        oneway void ReportXdrNonblock (1:TCXdrReq xdr);
    }
    

    生成对应代码

    thrift --gen cpp SunTelTc.thrift
    thrift --gen java SunTelTc.thrift
    thrift --gen go SunTelTc.thrift
    

    以c++为例,响应文件如下

    [root@oracle gen-cpp]# ll
    
    -rw-r--r-- 1 root root    300 5月   8 13:18 SunTelTc_constants.cpp
    -rw-r--r-- 1 root root    388 5月   8 13:18 SunTelTc_constants.h
    -rw-r--r-- 1 root root  23827 5月   8 13:18 SunTelTc_types.cpp
    -rw-r--r-- 1 root root   7887 5月   8 13:18 SunTelTc_types.h
    -rw-r--r-- 1 root root  19102 5月   8 13:18 TcRequest.cpp
    -rw-r--r-- 1 root root  14673 5月   8 13:18 TcRequest.h
    -rw-r--r-- 1 root root   1701 5月   8 14:46 TcRequest_server.skeleton.cpp
    -rw-r--r-- 1 root root  12781 5月   8 13:18 TcXdr.cpp
    -rw-r--r-- 1 root root  10612 5月   8 13:18 TcXdr.h
    -rw-r--r-- 1 root root   1461 5月   8 13:18 TcXdr_server.skeleton.cpp
    

    以C++为例,修改服务器端代码(可选),修改TcRequest_server.skeleton.cpp中SendTcReuest函数,增加如下代码

    
    _return.__set_state(0);
    _return.__set_msg("success");
    _return.__set_taskId("20150508101010");
    printf("SendTcReuest
    ");
    
    

    编写客户端代码

    
    #include <iostream>
    #include <time.h>
    #include "TcRequest.h"
    #include <thrift/transport/TSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/protocol/TBinaryProtocol.h>
    
    using namespace apache::thrift;
    using namespace apache::thrift::protocol;
    using namespace apache::thrift::transport;
    using boost::shared_ptr;
    using namespace SunTelTc;
    using namespace std;
    
    int main(int argc, char **argv)
    {
            shared_ptr<TTransport> socket(new TSocket(argv[1],atoi(argv[2])));
            shared_ptr<TTransport> transport(new TBufferedTransport(socket));
            shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    
            TcRequestClient client(protocol);
            time_t begin=time(NULL);
            try {
                    transport->open();
                    client.ping();
                    while(1){
                            static int i = 0;
                            if(i++ % 1000 == 0)
                            {
                                    time_t end=time(NULL);
                                    cout<<end-begin<<endl;
                            }
                            if(i==100000)
                                    break;
                            TCRequest tcRequest;
                            TCResponse pResponse;
                            tcRequest.__set_desString("abcdefghijk");
                            client.SendTcReuest(pResponse,tcRequest);
                            //cout<<"state="<<pResponse.state<<" msg="<<pResponse.msg<<" taskid="<<pResponse.taskId<<endl;
                    }
                    transport->close();
            } catch (TException &tx) {
                    printf("ERROR: %s
    ", tx.what());
            }
            time_t end=time(NULL);
            cout<<"use time="<<end-begin<<endl;
            return 0;
    }
    
    

    编译

    g++ SunTelTc_constants.cpp SunTelTc_types.cpp client.cpp TcRequest.cpp -o client -lthrift
    g++ SunTelTc_constants.cpp SunTelTc_types.cpp TcRequest_server.skeleton.cpp TcRequest.cpp -o server -lthrift
    

    测试结果

    本机客户端单线程测试100万次请求,花费55s,性能基本上能满足业务需求了。

  • 相关阅读:
    测试用例练习2
    测试小尝试
    两个栈实现队列 Python实现
    treap Python实现
    AVL树Python实现(使用递推实现添加与删除)
    AVL树Python实现
    跳表(skiplist)Python实现
    红黑树Python实现
    Django Middleware 之 SessionMiddleware
    软件测试——Peer Review(简介)
  • 原文地址:https://www.cnblogs.com/cqvoip/p/8078975.html
Copyright © 2020-2023  润新知