Apache Thrift Learning Notes

简介

 Apache Thrift软件框架(用于可扩展的跨语言服务开发)将软件堆栈与代码生成引擎结合在一起,以构建可在C ++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C#,可可,JavaScript,Node.js,Smalltalk,OCaml和Delphi等语言。

Thrift是Facebook开发的一个软件库和一套代码生成工具,用于加快高效且可扩展的后端服务。它的主要目标是通过抽象每种语言中倾向于需要在每种语言实现的公共库中进行最大程度的自定义。
具体来说,节俭允许开发者在单一语言中立中定义数据类型和服务接口归档并生成生成生成rpc客户端和服务器。

更多信息参考:http://thrift.apache.org/static/files/thrift-20070401.pdf

安装

参考指南:http://thrift.apache.org/docs/install/

sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config

下载apache thrift:   https://thrift.apache.org/download

编译源代码:

./configure

如果你想禁用某种语言, 例如java, 可以使用下面的语句:

./configure --without-java

如果你需要指定boost文件的位置, 例如你将boost库安装在/usr/local, 你要按下面方式运行configure:

./configure --with-boost=/usr/local

默认情况下thrift的C++库是以debug方式编译, 如果希望以其他方式编译, 可以使用CXXFLAGS选项, 例如

./configure CXXFLAGS=’-g -O2’
./configure CFLAGS=’-g -O2’
./configure CPPFLAGS=’-DDEBUG_MY_FEATURE’

调用完configure之后, 然后调用下面的命令:

make
make check
sh test/test.sh #跨语言测试

安装可以通过以下命令:

sudo make install

如果出现get github.com/golang/mock/gomock超时错误, 可以把golang/x/net/context包拷贝到thrift-version/test/go目录中, 最后context文件夹位于thrift-version/test/go/src/golang.org/x/net文件夹中, 然后重新调用上述安装命令.

编写TDF文件

Apache Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, 
the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages.
Instead of writing a load of boilerplate code to serialize and transport your objects and invoke remote methods, you can get right down to business. The following example is a simple service to store user objects
for a web front end.

 

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 callStack,
    3:optional String date
}

service PersonService {
    Person getPersonByUsername(1:required String username) throws (1:DataException dataException),
    void savePerson(1:required Person person) throws (1:DataException dataException)
}
data.thrift

语法规则:

Thrift interface description language

 参考官方:http://thrift.apache.org/docs/idl

生成代码:

thrift --gen  
thrift --gen py data.thrift
thrift --gen java data.thrift

 Apache Thrift Learning Notes_第1张图片

 源码位置:

https://github.com/mikeygithub/netty/tree/master/src/main/java/com/mikey/thrift

https://github.com/mikeygithub/netty/tree/master/src/main/java/com/mikey/thriftpython 

 简单运行

客户端:

package com.mikey.thrift;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;

/**
 * @ProjectName netty
 * @Author 麦奇
 * @Email [email protected]
 * @Date 9/29/19 4:13 PM
 * @Version 1.0
 * @Description:
 **/

public class ThriftClient {
    public static void main(String[] args) {

        TFramedTransport transport = new TFramedTransport(new TSocket("localhost", 8899), 600);

        TCompactProtocol tCompactProtocol = new TCompactProtocol(transport);

        PersonService.Client client = new PersonService.Client(tCompactProtocol);

        try {
            transport.open();
            Person mikey = client.getPersonByUsername("mikey");
            System.out.println(mikey.getUsername());
            System.out.println(mikey.getAge());
            System.out.println(mikey.isMarried());
            client.savePerson(mikey);

        }catch (Exception e){
            throw new RuntimeException(e.getMessage(),e);
        }finally {
            transport.close();
        }
    }
}
View Code

服务器:

package com.mikey.thrift;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;

/**
 * @ProjectName netty
 * @Author 麦奇
 * @Email [email protected]
 * @Date 9/29/19 4:06 PM
 * @Version 1.0
 * @Description:
 **/

public class ThriftServer {
    public static void main(String[] args) throws TTransportException {

        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        //连接设置
        THsHaServer.Args args1 = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
        //处理器
        PersonService.Processor personServiceProcessor = new PersonService.Processor<>(new PersonServiceImpl());
        //协议工厂
        args1.protocolFactory(new TCompactProtocol.Factory());
        args1.transportFactory(new TFastFramedTransport.Factory());
        args1.processorFactory(new TProcessorFactory(personServiceProcessor));
        //服务
        THsHaServer tHsHaServer = new THsHaServer(args1);
        //启动:异步非阻塞(死循环)
        tHsHaServer.serve();
    }
}
View Code

 

你可能感兴趣的:(Apache Thrift Learning Notes)