Thrift introduce

  • data type

bool               A boolean value, true or false
byte               A signed byte
i16                  A 16-bit signed integer
i32                  A 32-bit signed integer
i64                 A 64-bit signed integer
double         A 64-bit floating point number
string           An encoding-agnostic text or binary string

  • struct

A struct is essentially equivalent to a class in object oriented
programming languages. A struct has a set of strongly typed
fields, each with a unique name identifier. The basic syntax for
defining a Thrift struct looks very similar to a C struct definition.
Fields may be annotated with an integer field identifier (unique to
the scope of that struct) and optional default values. Field identifiers
will be automatically assigned if omitted, though they are strongly
encouraged for versioning reasons discussed later.

Example of struct

 


  • Service

Services are defined using Thrift types. Definition of a service is
semantically equivalent to defining an interface (or a pure virtual
abstract class) in object oriented programming. The Thrift compiler
generates fully functional client and server stubs that implement the
interface. Services are defined as follows:

 

An example is  :

 

Note :  an async modifier keyword may be added to a void function,  which will generate code that does not wait for a response from the server.

 

  • Servers and Multithreading

Thrift services require basic multithreading to handle simultaneous
requests from multiple clients

 


****  Important 

Now I will create a RouteServer which supply a “QueryRoute” function , client send Anumber and Bnumber as parameter , server will reply a struct include 2 int and 1 string ,  how to write this service ?

  • First define the struct in thrift file

  • then define the interface

 

Full thrift file is :    name is “RouteServer.thrift”

 

goto folder :   /home/liuyang/thrift-0.11.0/tutorial

run command :  rm -r gen-csharp    ,   delete  existing folder

then run command :   thrift -r –gen csharp RouteServer.thrift

 

Then goto /home/liuyang/thrift-0.11.0/tutorial/gen-csharp , copy “RouteServer.cs” and “InvalidOperation.cs” and “Ret.cs” to windows machine console application ,  add to project .

For server side “Program.cs” ,change it to

 

For client “Program.cs” ,change it to

 

Now client can call server function and get reply .

 

 

 

 

 

 

 

build thrift-0.11.0 from source on ubuntu 17

  •  On office 190 ubuntu machine  124.82.4.190 ,  9900

wget  http://mirror.bit.edu.cn/apache/thrift/0.11.0/thrift-0.11.0.tar.gz

  • tar xvzf thrift-0.11.0.tar.gz
  • install pre – requirements modules  , because 190 is ubantu machine , so following these steps

https://thrift.apache.org/docs/install/debian

  • CSharp additonal need following modules :    mono-gmcs mono-devel libmono-system-web2.0-cil nunit nunit-console

Now will install them one by one

 

a)  install mono complete by  https://gist.github.com/carolynvs/4520736f6017ca252a44

only exec first 5 steps

b) install nunit by this command :  sudo apt-get install nunit nunit-console

from  https://zxtech.wordpress.com/2015/01/05/install-nunit-on-ubuntu/

Now all the pre-requirement are installed .


  • Build 

From https://thrift.apache.org/docs/BuildingFromSource

goto /home/liuyang/thrift-0.11.0/

run command one by one :

 

    • Make a test c# project

  A cSharp project which use thrift should add reference to “Thrift.dll”

goto folder :   /home/liuyang/thrift-0.11.0/tutorial

run command :  rm -r csharp    ,   delete  existing csharp folder

then run command :   thrift -r –gen csharp tutorial.thrift

there will be a new folder named  “gen-csharp” ,  and here is the file will be used as server and client ,But no .sln file .  

Thrift will gen both server and client side code  , Now try to make a server side project ,  it need these files :

then change server side ‘s Program.cs to

 

For client side , need same cs files with server ,  only it ‘s Program.cs file content is :

 


Q&A

  • If anyone can call the remote procedure , No any permission limit ?  or any anthentication ?
  • How server know how many client connect to it ?

Thrift script analyze