1. 程式人生 > >Thrift RPC實現跨語言服務呼叫

Thrift RPC實現跨語言服務呼叫

工程開始之前的準備:

Thrift RPC類似與JAVA RMI,它們都是RPC(遠端過程呼叫協議)協議的具體實現,Thrift RPC中間多個IDL(介面描述語言-跨平臺開發的基礎),使得它可以支援多語言介面的呼叫。

1.使用 IDL 描述語言建立 .thrift 介面檔案(介面檔案的編寫方式請百度)

namespace java com.micro.thriftDemo

// 服務名
service HelloWorldService {
    string sayHello(1: string name);
}
//(JAVA作為SERVER或者CLIENT時使用)
thrift-0.11
.0.exe -r --gen java acsuser.thrift //(PYTHON作為SERVER或CLIENT時使用) thrift-0.11.0.exe -r --gen py acsuser.thrift

3.工程依賴
JAVA工程需要新增的MAVEN依賴:

<!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
        <dependency>
            <groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId> <version>0.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId
>
slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency>

PYTHON工程需要安裝Thrift模組:
安裝anaconda-3.6.0之後直接安裝thrift即可

conda install Thrift

一、JAVA Client呼叫JAVA Server
建立MAVEN工程並將生成的JAVA程式碼拷貝至工程目錄下,然後編寫CLIENT與SERVER
1.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 com.micro.thriftDemo.HelloWorldService;

public class Client {
    public static void main(String[] args) throws TException {
        TTransport transport = new TSocket("localhost", 9090); // 127.0.0.1
        //TTransport transport = new TSocket("192.168.1.170", 9299); // 127.0.0.1
        transport.open();
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloWorldService.Client client = new HelloWorldService.Client(protocol);

        String content = "【印尼執行伊斯蘭教法引關注】";

        String result = client.sayHello(content);
        System.out.println("呼叫結果:" + result);
        transport.close();
    }
}

2.Server端程式碼:

import org.apache.thrift.TException;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.server.TServer.Args;
import com.micro.thriftDemo.HelloWorldService;

public class Server {
    public static class HelloWorldServiceImpl implements HelloWorldService.Iface {
    //----------------介面方法實現---------------------
        public String sayHello(String name) throws TException {
            return "hello  world! i am implements Iface";
        }
    }

    public static void main(String[] args) throws TTransportException {
        // 獲取實現
        HelloWorldServiceImpl impl = new HelloWorldServiceImpl();
        // 介面與實現類的繫結關係在這裡完成
        HelloWorldService.Processor<HelloWorldServiceImpl> processor = new HelloWorldService.Processor<HelloWorldServiceImpl>(impl);
        // 構建伺服器
        TServerTransport serverTransport = new TServerSocket(9090);
        TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));

        System.out.println("Starting the thrift server ...");
        server.serve();
    }
}

二、JAVA Client呼叫PYTHON Server
這裡寫圖片描述
1.Client端程式碼(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 com.micro.thriftDemo.HelloWorldService;

public class Client {
    public static void main(String[] args) throws TException {
        TTransport transport = new TSocket("localhost", 9090); // 127.0.0.1
        transport.open();
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloWorldService.Client client = new HelloWorldService.Client(protocol);

        String content = "【印尼執行伊斯蘭教法引關注】";

        String result = client.sayHello(content);
        System.out.println("呼叫結果:" + result);
        transport.close();
    }
}

2.Server端程式碼:
將生成的gen-py使用pycharm開啟,然後再編寫Server.py檔案

# -*- coding:utf-8 -*-
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
# 根據實際的包結構去引入
from test import HelloWorldService


# test.thrift的具體實現
class TestServiceHandler:
    def __init__(self):
        self.log = {}

    # ----------------介面方法實現---------------------
    def sayHello(self, name):
        print("Input value:" + name)
        return 'hello  world! i am isiteam Iface'

if __name__ == '__main__':
    handler = TestServiceHandler()
    processor = HelloWorldService.Processor(handler)
    transport = TSocket.TServerSocket(host='127.0.0.1', port=9090)  # localhost
    # transport = TSocket.TServerSocket(host='localhost', port=9090) # localhost

    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
    print('python server:ready to start')
    server.serve()