RPC(Remote Procedure Call Protocol)——遠程過程調用協議,它是一種經過網絡從遠程計算機程序上請求服務,而不須要了解底層網絡技術的協議。php
RPC協議假定某些傳輸協議的存在,如TCP或UDP,爲通訊程序之間攜帶信息數據。在OSI網絡通訊模型中,RPC跨越了傳輸層和應用層。RPC使得開發包括網絡分佈式多程序在內的應用程序更加容易。html
hrift是一個軟件框架,用來進行可擴展且跨語言的服務的開發。它結合了功能強大的軟件堆棧和代碼生成引擎,以構建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 這些編程語言間無縫結合的、高效的服務。java
thrift最初由facebook開發,07年四月開放源碼,08年5月進入apache孵化器。python
1.下載:git
下載地址:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.3/thrift-0.9.3.tar.gz (目前最新0.9.3)github
2.安裝apache
安裝有2種方法,均可以。編程
第一種:bootstrap
1.安裝boost庫ruby
sudo apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-*
2.安裝其餘相關工具包
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant
若是須要支持java,須要安裝jdk,配置java環境變量。
4.解壓文件,進入目錄thrift-0.8.0安裝
./configure --with-cpp --with-boost --without-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
make
sudo make install
要支持java,須要編譯生成jar包,到lib/java目錄下,執行ant命令。將在lib/java/build目錄下生成libthrift-0.9.3.jar和libthrift-0.9.3-javadoc.jar。編譯過程當中,可能出錯,須要檢查lib/java/build/tools/maven-ant-tasks-2.1.3.jar是否正確下載。
5.測試:
thrift -version
安裝正確會出現 Thrift version 0.9.3
或者用thrift自帶的測試樣例
進入thrift安裝的tutorial文件夾,shared.thrift和tutorial.thrift是接口定義文件。
thrift -r --gen java tutorial.thrift
thirft -r --gen cpp tutorial.thrift
執行這兩條命令能夠生成gen-java和gen-cpp兩個文件夾,這些是thrift編譯器自動生成的代碼。
第二種:
官網教程:http://thrift.apache.org/docs/BuildingFromSource
First make sure your system meets all necessary Apache Thrift Requirements
If you are building from the first time out of the source repository, you will need to generate the configure scripts. (This is not necessary if you downloaded areleased tarball.) From the top directory, do:
./bootstrap.sh
Once the configure scripts are generated, thrift can be configured. From the top directory, do:
./configure
Disable a language:
./configure --without-java
You may need to specify the location of the boost files explicitly. If you installed boost in /usr/local, you would run configure as follows:
./configure --with-boost=/usr/local
If you want to override the logic of the detection of the Java SDK, use the JAVAC environment variable:
./configure JAVAC=/usb/bin/javac
Note that by default the thrift C++ library is typically built with debugging symbols included. If you want to customize these options you should use the CXXFLAGS option in configure, as such:
./configure CXXFLAGS='-g -O2' ./configure CFLAGS='-g -O2' ./configure CPPFLAGS='-DDEBUG_MY_FEATURE'
To see other configuration options run
./configure --help
Once you have run configure you can build Thrift via make:
make
and run the test suite:
make check
and the cross language test suite:
sh test/test.sh
Issues while compiling
"compiler/cpp/thriftl.cc:2190: undefined reference to `yywrap'"
you need to install the Flex library (See also Apache Thrift Requirements ) and re-run the configuration script.
mv: cannot stat "'.deps/TBinaryProtocol.Tpo': No such file or directory" while building the Thrift Runtime Library
Re-reun configure with
--enable-libtool-lock
or by turning off parallel make by placing .NOTPARALLEL: in lib/cpp/Makefile or
make -j 1
Although the thrift compiler build appears to be compatible with parallel make without libtool lock, the thrift runtime build is not.
From the top directory, become superuser and do:
make install
Note that some language packages must be installed manually using build tools better suited to those languages (this applies to Java, Ruby, PHP).
Look for the README file in the lib/<language>/
folder for more details on the installation of each language library package.
總結下來,第1步,先解壓thrift-0.9.3.tar.gz,解壓命令:
tar -xvf thrift-0.9.3.tar.gz
第2步,輸入如下命令:
$cd thrift-0.9.3 $./configure $make #sudo make install
關於配置的問題能夠查看命令:
./configure --help
能夠關閉你不熟悉的語言,由於thrift支持的語言很是多,能夠關閉一些用不到的,如python,gt4等;
關閉命令爲:
./configure --without-qt4
在install的過程當中若是報一些test方面的error能夠忽略.
上面的步驟走完之後,能夠在任意一個目錄下輸入以下命令進行測試:
~/workspace$ thrift -version Thrift version 0.9.3
和c語言很類似,但又有很大區別。也許正由於它什麼都不是,它纔有可能什麼都是。
1.數據類型
2.服務端編碼基本步驟:
3.客戶端編碼基本步驟:
4.數據傳輸協議
tips:客戶端和服務端的協議要一致
1.引入jar包
我這裏用到的是maven進行管理jar包的,因此首先新建一個maven項目,而後在pom.xml中添加以下內容:
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> </dependency>
2.建立Thrift文件
namespace java com.thrift.demo service HelloService { string sayHello(1:string username) }
3.生成java文件:
thrift -r -gen java demoHello.thrift
把生成的HelloWorldService.java文件拷貝到項目中去.
package com.thrift.demo; /** * Created by Jtao on 10/9/16. */ public class HelloImpls implements HelloService.Iface { public HelloImpls() { } @Override public String sayHello(String username) { return "Hi," + username + " ,Welcome to the thrift's world !"; } }
5.TSimpleServer服務端
簡單的單線程服務模型,通常用於測試。
編寫服務端server代碼:HelloServerDemo.java
package com.thrift.demo; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; public class HelloServerDemo { public static final int SERVER_PORT = 8090; /** * @param args */ public static void main(String[] args) { HelloServerDemo server = new HelloServerDemo(); server.startServer(); } public void startServer() { try { System.out.println("HelloWorld TSimpleServer start ...."); // TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl()); HelloService.Processor<HelloService.Iface> tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloImpls()); // 簡單的單線程服務模型,通常用於測試 TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); // tArgs.protocolFactory(new TBinaryProtocol.Factory()); tArgs.protocolFactory(new TCompactProtocol.Factory()); // tArgs.protocolFactory(new TJSONProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (Exception e) { System.out.println("Server start error!!!"); e.printStackTrace(); } } }
6.編寫客戶端代碼
package com.thrift.demo; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; public class HelloClientDemo { public static final String SERVER_IP = "localhost"; public static final int SERVER_PORT = 8090; public static final int TIMEOUT = 30000; /** * @param args */ public static void main(String[] args) { HelloClientDemo client = new HelloClientDemo(); client.startClient("amosli"); } /** * @param userName */ public void startClient(String userName) { TTransport transport = null; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); // 協議要和服務端一致 // TProtocol protocol = new TBinaryProtocol(transport); TProtocol protocol = new TCompactProtocol(transport); // TProtocol protocol = new TJSONProtocol(transport); HelloService.Client client = new HelloService.Client(protocol); transport.open(); String result = client.sayHello(userName); System.out.println("Thrift client result =: " + result); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } finally { if (null != transport) { transport.close(); } } } }
項目最終結構圖:
7.最終運行效果
服務端:
客戶端:
gitHub代碼:https://github.com/wangtao1/rpc_thrift
參考:
1.http://baike.baidu.com/view/1698865.htm?fr=aladdin
2.http://thrift.apache.org/docs/BuildingFromSource
3.http://www.cnblogs.com/amosli/p/3906177.html