1、下載
去官網 https://thrift.apache.org/download 下載兩個文件,下載地址 http://archive.apache.org/dist/thrift/0.9.3/:
- thrift-0.9.3.exe
用於將 .thrift 文件編譯成其他語言文件(如 .cs, .cpp 等)。

- thrift-0.9.3.tar.gz
源代碼,用於生成不同語言的 thrift 庫文件。
2、Thrift-0.9.3.exe配置
- 新建thrift文件夾,將下載的thrift-0.9.3.exe重新命名為thrift.exe后放到thtift文件夾下,配置環境變量


- 打開cmd,輸入thrift -version

3、thrift.dll編譯
將 thrift-0.9.3.tar.gz 解壓,進入 lib/csharp/src 文件夾,使用 Visual Studio 打開 Thrift.sln 文件,編譯。完成后可在 bin/Debug 中找到 Thrift.dll 文件,就是我們每次使用 Thrift 必須加載的庫文件。(提示:編譯時可能會彈框提示需要 .net framework 3.5 的庫,按照提示鏈接去官網 https://www.microsoft.com/en-us/download/details.aspx?id=22 下載安裝即可)
4、編寫 Thrift 腳本
假設服務端(Server)為客戶端(Client)提供簡單的數學運算功能。編寫 demo.thrift 腳本如下:
namespace csharp MathClient
namespace csharp MathServer
service MathService{
i32 add (1:i32 a, 2:i32 b),
i32 sub (1:i32 a, 2:i32 b),
i32 mul (1:i32 a, 2:i32 b),
i32 div (1:i32 a, 2:i32 b),
i32 mod (1:i32 a, 2:i32 b)
}
從控制台進入 demo.thrift 的目錄后,編譯該文件為 C# 代碼:
隨后會生成一個 gen-csharp 的文件夾,其中包括 MathService.cs 文件,該文件需要同時導入到 Server 和 Client 的程序中,下面將詳細說明。
如果是兩種語言,如 Java 和 C# 通信的話,需要 –gen csharp 和 –gen java 分別編譯一次,將得到的 .cs 和 .class 文件分別導入到兩個項目中。
5、Server 程序
在 Visual Studio 中新建 C# Console Application 程序:ThriftServer。將 Thrift.dll. 導入到 Reference 中,同時將之前生成的 MathService.cs 導入項目中,並在 Program.cs 中包含:
using Thrift.Server; using Thrift.Transport;
新建一個類 MathServer.cs (注意不是 MathService.cs),實現 MathService.Iface 接口:
public class MathServer : MathService.Iface { public MathServer() { } public int add(int a, int b) { Console.WriteLine("Called add({0},{1})={2}", a, b, a + b); return a + b; } public int sub(int a, int b) { Console.WriteLine("Called sub({0},{1})={2}", a, b, a - b); return a + b; } public int mul(int a, int b) { Console.WriteLine("Called mul({0},{1})={2}", a, b, a * b); return a + b; } public int div(int a, int b) { Console.WriteLine("Called div({0},{1})={2}", a, b, a / b); return a / b; } public int mod(int a, int b) { Console.WriteLine("Called mod({0},{1})={2}", a, b, a % b); return a % b; } }
然后修改 Program.cs 中的 Main 函數為:
static void Main(string[] args) { try { MathServer handler = new MathServer(); MathService.Processor processor = new MathService.Processor(handler); TServerTransport serverTransport = new TServerSocket(9095); TServer server = new TSimpleServer(processor, serverTransport); Console.WriteLine("Starting the server..."); server.Serve(); } catch (Exception x) { Console.WriteLine(x.StackTrace); } Console.WriteLine("done."); }
這樣 ThriftServer 項目就可以運行了。它開啟 9095 端口(可以自設),然后等待 Client 與其通信。
6、Client 端
同樣新建一個C#控制台項目:ThriftClient,並將 Thrift.dll 和 MathService.cs 導入到項目中,在Program.cs 中包含:
using Thrift.Transport; using Thrift.Protocol;
修改 Program.cs 中的 Main 函數如下:
static void Main(string[] args) { TTransport transport = new TSocket("localhost", 9095); transport.Open(); TProtocol protocal = new TBinaryProtocol(transport); MathService.Client client = new MathService.Client(protocal); Console.WriteLine(client.add(10, 20)); Console.WriteLine(client.sub(10, 20)); Console.WriteLine(client.mul(10, 20)); Console.WriteLine(client.div(10, 20)); Console.WriteLine(client.mod(10, 20)); }
7、運行
首先運行 MathServer,可以看到:
然后運行 MathClient,可以看到:
同時 MathServer 的控制台顯示: