Use of Apache Thrift

Source: Internet
Author: User

Apache Thrift

Thrift is a cross-language service deployment framework that was originally developed by Facebook in 2007 and entered the Apache Open source project in 2008. Thrift defines the interface and data type of RPC (remote Procedure call, remoted procedure calls) through IDL (Interface definition Language, Interface Definition language). Then generate code in different languages via the thrift compiler (currently supports C++,java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, Smalltalk, and OCaml), And the generated code is responsible for the RPC protocol layer and the Transport layer implementation.

    • Apache Thrift
    • Apache Thrift Tutorial
    • Apache Thrift Interface Description Language
    • Apache Thrift Whitepaper
    • Thrift Getting Started Tutorial
    • The principle and use of thrift
    • Who can explain in plain language what is the RPC framework?

Process:

    • Call ID Mapping
    • Serialization and deserialization
    • Network transmission

What is RPC remote procedure call?
RPC remote Procedure Call, a protocol that requests services from a remote computer program over a network without needing to know the underlying network technology.

Sample Whattime.thrift File
namespace cpp ldstruct Example{    1:i32     number=10,    2:i64     bigNumber,    3:double  decimals,    4:string  name="thrifty"}service TimeInterface{    i32  GetTime(),    void SetTime()}

Generate the source from a thrift file

thrift [-r] --gen <language> <Thrift filename># -r 可选

Client.cpp

#include "TimeInterface.h"#include <thrift/transport/TSocket.h>#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/transport/TBufferTransports.h>using namespace ::apache::thrift;using namespace ::apache::thrift::protocol;using namespace ::apache::thrift::transport;using boost::shared_ptr;using namespace  ::ld;int main(int argc, char **argv) {    int port = 9090;    boost::shared_ptr<TSocket> socket(new TSocket("localhost", port)); //注意此处的ip和端口    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));    transport->open();    // 我们的代码写在这里    TimeInterfaceClient client(protocol);  // 客户端接口,声明于TimeInterface.h中。    client.SetTime();    client.GetTime();        transport->close();    return 0;}

Makefile

all : server clientserver : TimeInterface.cpp  TimeInterface.h  TimeInterface_server.skeleton.cpp  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h        g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp  TimeInterface.h  TimeInterface_server.skeleton.cpp  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h -o serverclient: TimeInterface.cpp  TimeInterface.h  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h client.cpp        g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp  TimeInterface.h  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h client.cpp -o client

Use of Apache Thrift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.