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<PersonServiceImpl> 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.结果

在这里插入图片描述

在这里插入图片描述

指定位置生成文件

# java
thrift -out user-service/src/main/java/com/liyuan/exception --gen java thrift/userServiceApi/exception.thrift

thrift -out user-service/src/main/java/com/liyuan/pojo--gen java thrift/userServiceApi/data.thrift

thrift -out user-service/src/main/java/com/liyuan/service--gen java thrift/userServiceApi/service.thrift


#python同理
thrift -out tt --gen py thrift/userServiceApi/userServiceApi.thrift


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值