Apache Thrift使用学习

Apache Thrift使用学习,第1张

Apache Thrift使用学习 1. Apache Thrift学习 1.1.编写thrift文件

//thrift文件有点类似于java rpc中的接口文件,定义一种规范

namespace java thrift.generated
namespace py py.thrift.generated

typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String

struct Person{
    1: optional String username;
    2: optional int age;
    3: optional boolean married;
}

exception DataException{
    1: optional String message;
    2: optional String callBack;
    3: optional String date;
}

service PersonService{
    Person getPersonByUsername(1: required String username) throws (1:DataException e);
    void savePerson(1: required Person person);
}
1.2.编写java服务端文件

//依照thrift定义文件生成java代码
thrift  -out src/main/java --gen java thrift/dat
a.thrift
//服务端 ThriftServer.java 
package thrift;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TframedTransport;
import thrift.generated.PersonService;
import thrift.handler.PersonServiceImpl;

public class ThriftServer {
    public static void main(String[] args) throws TTransportException {
        try {
            //定义服务使用socket类型
            TNonblockingServerSocket tNonblockingServerSocket = new TNonblockingServerSocket(8899);
            //创建服务器参数
            THsHaServer.Args arg = new THsHaServer.Args(tNonblockingServerSocket).minWorkerThreads(2).maxWorkerThreads(4);
            //请求处理器
            PersonService.Processor processor = new PersonService.Processor<>(new PersonServiceImpl());
            //配置传输数据的格式
            arg.protocolFactory(new TCompactProtocol.Factory());
            //配置数据传输方式
            arg.transportFactory(new TframedTransport.Factory());
            //配置处理器处理rpc请求
            arg.processorFactory(new TProcessorFactory(processor));
            //版同步版异步服务器
            TServer server = new THsHaServer(arg);
            System.out.println("Thrift Server Started");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

package thrift.handler;

import org.apache.thrift.TException;
import thrift.generated.Person;
import thrift.generated.PersonService;

public class PersonServiceImpl implements PersonService.Iface {

    @Override
    public Person getPersonByUsername(String username) throws TException {
        System.out.println("Got client param: " + username);
        return new Person().setUsername("liyuan").setAge(20).setMarried(false);
    }

    @Override
    public void savePerson(Person person) throws TException {
        System.out.println("Got client param: " + person);
    }
}

1.3.编写python客户端文件

thrift  -out thrift-client --gen py thrift/data.
thrift
import thrift.Thrift
from thrift.protocol import TCompactProtocol
from thrift.transport import *

from py.thrift.generated import PersonService
from py.thrift.generated.ttypes import Person

if __name__ == '__main__':
    try:
        ts = TSocket.TSocket("localhost", 8899)
        ts.setTimeout(1000)
        transport = TTransport.TframedTransport(ts)
        protocol = TCompactProtocol.TCompactProtocol(transport)
        client = PersonService.Client(protocol)
        transport.open()
        person = client.getPersonByUsername("liyuan")
        print(person)
        client.savePerson(Person(username="yuanxi", age=21, married=False))
        transport.close()
    except Exception as e:
        print(e)
1.4.结果

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5688601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存