Apache thrift 安裝及使用


本篇介紹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 short
typedef i64 long

service 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!

這篇只是簡單介紹thrift客戶端的調用的單向通信,接下來的文章將介紹thrift的雙向通信,更貼近業務需求。

 

RSS:

Apache Thrift java版案例:http://thrift.apache.org/tutorial/java

Apache Thrift 詳解:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM