ubuntu下thrift的安裝 - 水木米 - 博客頻道 - CSDN.NET
?
1.下載源代碼
http://thrift.apache.org/download/下載最新版本 thrift-0.8.0.tar.gz
?
2.安裝boost庫sudo apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-*?3.安裝其他相關(guān)工具包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環(huán)境變量。?4.解壓文件,進(jìn)入目錄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
makesudo make install要支持java,需要編譯生成jar包,到lib/java目錄下,執(zhí)行ant命令。將在lib/java/build目錄下生成libthrift-0.8.0.jar和libthrift-0.8.0-javadoc.jar。編譯過程中,可能出錯(cuò),需要檢查lib/java/build/tools/maven-ant-tasks-2.1.3.jar是否正確下載。?5.測(cè)試直接輸入thrift命令,看是否有用法提示??二、thrift自帶的測(cè)試樣例進(jìn)入tutorial文件夾,shared.thrift和tutorial.thrift是接口定義文件。thrift -r --gen java?tutorial.thriftthirft -r --gen cpp?tutorial.thrift執(zhí)行這兩條命令可以生成gen-java和gen-cpp兩個(gè)文件夾,這些是thrift編譯器自動(dòng)生成的代碼。?然后到j(luò)ava目錄下,執(zhí)行ant命令,編譯成功后,在兩個(gè)不同的窗口下執(zhí)行以下命令:./JavaServer./JavaClient simple?進(jìn)入cpp目錄下,執(zhí)行make命令,如果編譯出錯(cuò),第一個(gè)錯(cuò)誤是/usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147:35: error: there are no arguments to ‘htons’ that depend on a template parameter, so a declaration of ‘htons’ must be available則修改Makefile,加上編譯選項(xiàng)-DHAVE_NETINET_IN_Hserver: CppServer.cpp? ? ? ? g++?-DHAVE_NETINET_IN_H?-o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} ?-I../gen-cpp -L${LIB_DIR} -lthrift CppServer.cpp ${GEN_SRC}?client: CppClient.cpp? ? ? ? g++?-DHAVE_NETINET_IN_H?-o CppClient -I${THRIFT_DIR} -I${BOOST_DIR} ?-I../gen-cpp -L${LIB_DIR} -lthrift CppClient.cpp ${GEN_SRC}編譯通過生,將生成CppClient和CppServer兩個(gè)可執(zhí)行程序。同樣,在兩個(gè)不同的窗口執(zhí)行以下命令:./CppServer./CppClient而且java和c++的Client和Server都可以交叉運(yùn)行。比如運(yùn)行JavaServer和CppClient也能得到同樣的結(jié)果。以此達(dá)到了多語言的相互調(diào)用。?三、Hello World仿照tutorial,寫一個(gè)簡(jiǎn)單的hello world。在tutorial平級(jí)目錄下,建立目錄hello,這里只是為了測(cè)試需要。服務(wù)端用java,客戶端用java和c++。?1. 編寫tt.thriftnamespace java demonamespace cpp demo?service Hello{? string helloString(1:string para)}?2. 編譯生成代碼thrift -r --gen java tt.thriftthrift -r --gen cpp tt.thrift生成gen-java和gen-cpp兩個(gè)文件夾gen-cpp下有如下文件Hello.cpp ?Hello.h ?Hello_server.skeleton.cpp ?tt_constants.cpp ?tt_constants.h ?tt_types.cpp ?tt_types.h其中Hello.h,Hello.cpp中定義了遠(yuǎn)程調(diào)用的接口,實(shí)現(xiàn)了底層通信。可以在Hello.h中找到客戶端遠(yuǎn)程調(diào)用需要用到的類HelloClient,調(diào)用方法:void helloString(std::string& _return, const std::string& para);這個(gè)跟thrift文件中申明的方法有點(diǎn)不一定,返回參數(shù)是通過引用傳回來的。Hello_server.skeleton.cpp將實(shí)現(xiàn)Hello.h的服務(wù)端接口,如果要用c++作為服務(wù)端,還需要將這個(gè)文件拷出去,重命名,實(shí)現(xiàn)類HelloHandler的方法helloString,遠(yuǎn)程調(diào)用方法的業(yè)務(wù)邏輯也就寫在helloString中。可能還需要改main函數(shù)中的端口信息。gen-java/demo下只有Hello.java一個(gè)文件,它定義了服務(wù)端和客戶端的接口,實(shí)現(xiàn)了底層的通信。?3. 編寫java服務(wù)端和客戶端仿照tutorial,在hello目錄下建立java目錄,將tutorial/java/下的一些文件和目錄拷到hello/java下build.xml JavaClient ?JavaServer ?src刪除src下所有文件,在src下編寫代碼。?1) HelloImpl.java 遠(yuǎn)程過程調(diào)用的業(yè)務(wù)邏輯import demo.*;import org.apache.thrift.TException;?class HelloImpl implements Hello.Iface {?public HelloImpl() {}?public String helloString(String para) throws org.apache.thrift.TException {??????? //各種業(yè)務(wù)邏輯???????? return "hello " + para;?}}?2)?Server.java ?服務(wù)端程序import demo.*;import java.io.IOException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TBinaryProtocol.Factory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TThreadPoolServer.Args;import org.apache.thrift.server.TThreadPoolServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TTransportException;?public class Server {private void start() {try {? ? TServerSocket serverTransport = new TServerSocket( 7911 );? ? Hello.Processor processor = new Hello.Processor(new HelloImpl());? ? Factory protFactory = new TBinaryProtocol.Factory(true, true);? ? Args args = new Args(serverTransport);? ? args.processor(processor);? ? args.protocolFactory(protFactory);? ? TServer server = new TThreadPoolServer(args);? ? //TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);? ? System.out.println("Starting server on port 7911 ...");? ? server.serve();?? ?} catch (TTransportException e) {? ? e.printStackTrace();? ?} catch (Exception e) {? ? e.printStackTrace();}}?public static void main(String args[]) {? ? Server srv = new Server();? ? srv.start();}}?3)?Client.java 客戶端程序import demo.*;import java.io.IOException;import org.apache.thrift.*;import org.apache.thrift.protocol.*;import org.apache.thrift.transport.*;?public class Client {? ? ? public static void main(String [] args) {? ? ? ? ? ?try {? ? ? ? ? ? ? ? ? ? TTransport transport = new TSocket(" localhost ",? 7911 );? ? ? ? ? ? ? ? ? ? TProtocol protocol = new TBinaryProtocol(transport);? ? ? ? ? ? ? ? ? ? Hello.Client client = new Hello.Client(protocol);? ? ? ? ? ? ? ? ? ? transport.open();? ? ? ? ? ? ? ? ? ? System.out.println (" Client
calls hello ");? ? ? ? ? ? ? ? ? ?? System.out.println(client.helloString("world"));? ? ? ? ? ? ? ? ? ? transport.close();? ? ? ? ? ? ? ?} catch (TException x) {? ? ? ? ? ? ? ? ? ? x.printStackTrace();? ? ? ? ? ? ? ?}? ? ? ? }}?4) 修改 build.xml<project name=" hello " default=" hello "
basedir=".">?? <description> Thrift Hello </description>?? <property name="src" location="src" />? <property name="gen" location="../gen-java" />? <property name="build" location="build" />?? <path id="libs.classpath">? ? <fileset dir="http://www.cnblogs.com/lib/java/build">? ? ? <include name="*.jar" />? ? ? <exclude name="-test.jar" />? ? </fileset>? ? <fileset dir="http://www.cnblogs.com/lib/java/build/lib">? ? ? <include name="*.jar" />? ? </fileset>? </path>? <path id="build.classpath">? ? <path refid="libs.classpath" />? ? <pathelement path="${gen}" />? </path>?? <target name="init">? ? <tstamp />? ? <mkdir dir="${build}"/>? </target>?? <target name="compile" depends="init">? ? <javac srcdir="${gen}" destdir="${build}" classpathref="libs.classpath" />? ? <javac srcdir="${src}" destdir="${build}" classpathref="build.classpath" />? </target>?? <target name=" hello " depends="compile">? ? <jar jarfile=" hello.jar " basedir="${build}"/>? </target>?? <target name="clean">? ? <delete dir="${build}" />? ? <delete file="hello.jar" />? </target>?</project>?5) 編譯ant將生成build文件夾Client.class ?demo ?hello ?HelloImpl.class ?hello.jar ?Server.class?6) 修改執(zhí)行腳本JavaClientjava -cp http://www.cnblogs.com/lib/java/build/lib/*:http://www.cnblogs.com/lib/java/build/*: hello.jar
ClientJavaServerjava -cp http://www.cnblogs.com/lib/java/build/lib/*:http://www.cnblogs.com/lib/java/build/*: hello.jar
Server?4. 編寫c++客戶端同樣仿照tutorial,將tutorial/cpp中的Makefile和CppClient.cpp拷到hello/cpp下。?1) 將CppClient.cpp重命名為Client.cpp,并修改#include <stdio.h>#include <unistd.h>#include <sys/time.h>?#include <protocol/TBinaryProtocol.h>#include <transport/TSocket.h>#include <transport/TTransportUtils.h>?#include "../gen-cpp/Hello.h"#include <string>?using namespace std;using namespace apache::thrift;using namespace apache::thrift::protocol;using namespace apache::thrift::transport;?using namespace demo;?using namespace boost;?int main(int argc, char** argv) {? shared_ptr<TTransport> socket(new TSocket(" localhost ",? 7911 ));? shared_ptr<TTransport> transport(new TBufferedTransport(socket));? shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));?? HelloClient client(protocol);?? try {? ? transport->open();?? ? string ret;? ?? client.helloString(ret, "world");? ? printf("%s\n", ret.c_str());?? ? transport->close();? } catch (TException &tx) {? ? printf("ERROR: %s\n", tx.what());? }?}?2). 修改MakefileBOOST_DIR = /usr/local/boost/include/boost-1_33_1/THRIFT_DIR = /usr/local/include/thriftLIB_DIR = /usr/local/lib?GEN_SRC =? ../gen-cpp/tt_types.cpp ../gen-cpp/Hello.cpp?default: client?client: Client.cpp? ? ? ? g++? -DHAVE_NETINET_IN_H ? -o
client -I${THRIFT_DIR} -I${BOOST_DIR} ?-I../gen-cpp -L${LIB_DIR} -lthrift Client.cpp ${GEN_SRC}?clean:? ? ? ? $(RM) -r client?3). 編譯make生成可執(zhí)行文件client?5. 運(yùn)行程序運(yùn)行服務(wù)端程序,java目錄下:./JavaServer運(yùn)行客戶端程序,cpp目錄下:./client這樣c++程序通過 client.helloString(ret, "client") 可以調(diào)用服務(wù)端的java接口String helloString(String para)。從而實(shí)現(xiàn)了遠(yuǎn)程多語言調(diào)用。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
