Thrift RPC实现跨语言服务调用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/superman_xxx/article/details/79935290

工程开始之前的准备:

Thrift RPC类似与JAVA RMI,它们都是RPC(远程过程调用协议)协议的具体实现,Thrift RPC中间多个IDL(接口描述语言-跨平台开发的基础),使得它可以支持多语言接口的调用。

1.使用 IDL 描述语言建立 .thrift 接口文件(接口文件的编写方式请百度)

namespace java com.micro.thriftDemo

// 服务名
service HelloWorldService {
    string sayHello(1: string name);
}

2.下载Thrift:https://mirrors.cnnic.cn/apache/thrift/0.11.0/
将定义好的接口文件和.exe文件放在同一目录下后,执行一下命令分别生成JAVA和PYTHON代码文件:

//(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()

猜你喜欢

转载自blog.csdn.net/superman_xxx/article/details/79935290