1. 程式人生 > >thrift跨語言呼叫服務,以nodejs和Java為例

thrift跨語言呼叫服務,以nodejs和Java為例

使用thrift的流程:

1、下載thrift的exe,編寫thrift介面檔案,使用thrift --gen java + 檔名生成Java的介面檔案,使用thrift --gen js:node +檔名

生成nodejs介面檔案。介面檔案PrintService.thrift如下:

namespace java com.huangshisheng

service Print {

bool println(1: string str)

}

2、Java編寫服務端,需要下載thrift的依賴包,之後實現thrift介面。依賴的包如下:

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <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.7.5</version>
        </dependency>

服務端程式碼如下:

public class Server implements Print.Iface {

    public boolean println(String str) throws TException {         // TODO Auto-generated method stub         System.out.println(str);         try {             Thread.sleep(1000);         } catch (InterruptedException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return true;     }          public static void main(String[] as) {

          TNonblockingServerTransport serverTransport = null;           try {             serverTransport = new TNonblockingServerSocket(8888);           } catch (TTransportException e) {             e.printStackTrace();           }           //Server 為Hello介面的實現類           Print.Processor<Print.Iface> processor = new Print.Processor<Print.Iface>(                new Server());           Factory protFactory = new TBinaryProtocol.Factory(true, true);           TNonblockingServer.Args args = new TNonblockingServer.Args(                serverTransport);           args.processor(processor);           args.protocolFactory(protFactory);           TServer server = new TNonblockingServer(args);           System.out.println("Start server on port 19090 ...");           server.serve();        }

}

Java客戶端程式碼如下:

public class Client {     public static void main(String[] args) {           long startTime = System.currentTimeMillis();           try {             TTransport transport = new TFramedTransport(new TSocket(                   "localhost", 8888));             TBinaryProtocol protocol = new TBinaryProtocol(transport);             // TCompactProtocol protocol = new TCompactProtocol(transport);             Print.Client client = new Print.Client(protocol);             transport.open();             for (int i = 0; i < 1000; i++) {                System.out.println(client.println("login"));             }             transport.close();           } catch (TException x) {             x.printStackTrace();           }           long endTime = System.currentTimeMillis();           System.out.println(" 本次呼叫完成時間:" + (endTime - startTime));        } }

3、將生成的nodejs的介面檔案拷貝到nodejs專案下,nodejs請求服務的程式碼如下:

var Print = require('./gen-nodejs/Print'),

type = require('./gen-nodejs/PrintService_types'),

thrift = require('thrift');

var transport = thrift.TFramedTransport;

var protocol = thrift.TBinaryProtocol;

var connection = thrift.createConnection('localhost', 8888, {

transport: transport,

protocol: protocol

});

var client = thrift.createClient(Print, connection);

connection.on('error', function (err) {

console.log(err);

});

client.println("hello", function (err, result) {

console.log(result)

connection.end();

});

參考資料: