Apache thrift 安裝及使用

來源:互聯網
上載者:User

標籤:types   targe   name   工廠   service   long   package   執行   block   

本篇介紹Apache thrift windows安裝教程。

一.首先在Apache官網下載thrift 編譯應用,戳這裡下載 http://thrift.apache.org/download

    下載好windows 版本的exe檔案。在c盤建立一個Thrift檔案夾,將下載好的thrift-xxx.exe檔案該名成 thrift.exe,放入Thrift檔案夾中。

二.配置環境變數

    

 

 然後運行cmd命令,輸入 thrift  -version。看到如下版本資訊,則表示thrift 安裝成功。

是不是很easy! 接下來就可以幹正事了。

三.編寫thrift指令檔,生產對應語言的代碼,此處我使用java作為案例。

namespace java com.zhj.thrift.server  // defines the namespace   typedef i32 int  //typedefs to get convenient names for your types  typedef i16 shorttypedef i64 longservice HelloService{    int add(1:int num1,2:int num2)    short getShort(1:short value)    long getLong(1:long value)    string sayHello(1:string name)}

儲存該檔案命名為:helloService.thrift。之後執行該檔案會產生一個helloService介面的java代碼.

四.找到上述檔案的目錄,執行該檔案。執行後會在相同的目錄下產生一個gen-java的包。裡面就生產了helloService.java的一個類

五.建立一個Java 項目,將上述介面拷到項目中,並實現該介面。實現介面類如下:

package com.zhj.thrift.server.impl;import org.apache.thrift.TException;import com.zhj.thrift.server.HelloService;public class HelloServiceImpl implements HelloService.Iface {    @Override    public int add(int num1, int num2) throws TException {        return num1+num2;    }    @Override    public short getShort(short value) throws TException {        return (short) (value*2);    }    @Override    public long getLong(long value) throws TException {        Long l_value = value - 24*60*60*1000;        return l_value;    }    @Override    public String sayHello(String name) throws TException {        return "hello " + name;    }}
View Code

需要引入三個jar包

libthrift-0.9.3 依賴包:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/

 六.編寫thrift服務端類,如下:

package com.zhj.thrift.server;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TBinaryProtocol.Factory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TTransportException;import com.zhj.thrift.server.HelloService.Processor;import com.zhj.thrift.server.impl.HelloServiceImpl;public class HelloServiceServer {    public static void main(String[] args) {        try {            // 設定伺服器連接埠            TServerSocket ts = new TServerSocket(9090);            // 設定二進位協議工廠            Factory protocolFactory = new TBinaryProtocol.Factory();            //處理器關聯業務實現            Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());            // 1. 使用單線程標準阻塞I/O模型            TServer.Args simpleArgs = new TServer.Args(ts);            simpleArgs.processor(processor);            simpleArgs.protocolFactory(protocolFactory);            TServer server = new TSimpleServer(simpleArgs);            System.out.println("開啟thrift伺服器,監聽連接埠:9090");            server.serve();                         // 2. 使用線程池服務模型           /* TThreadPoolServer.Args poolArgs = new TThreadPoolServer.Args(ts);            poolArgs.processor(processor);            poolArgs.protocolFactory(protocolFactory);            TServer poolServer = new TThreadPoolServer(poolArgs);            poolServer.serve();*/                   } catch (TTransportException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
View Code

七.編寫thrift用戶端類,如下:

package com.zhj.thrift.client;import org.apache.thrift.TException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;import org.apache.thrift.transport.TTransportException;import com.zhj.thrift.server.HelloService;public class HelloServiceClient {    public static void main(String[] args) {                try {            // 設定調用的服務地址-連接埠            TTransport transport = new TSocket("127.0.0.1", 9090);            //  使用二進位協議            TProtocol protocol = new TBinaryProtocol(transport);            // 使用的介面            HelloService.Client client = new HelloService.Client(protocol);            //開啟socket            transport.open();            System.out.println("求和:"+client.add(100, 150));            System.out.println(client.getLong(System.currentTimeMillis()));            System.out.println(client.sayHello("李四"));            System.out.println(client.getShort((short)10));                    } catch (TTransportException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (TException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
View Code

最後運行效果:

至此,簡單的thrift服務發布和調用已實現,有不合理之處請小夥伴指點出來,thank you!

 

Apache thrift 安裝及使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.