Apache Thrift系列详解(一) - 概述与入门

前言

Thrift是一个轻量级跨语言远程服务调用框架,最初由Facebook开发,后面进入Apache开源项目。它经过自身的IDL中间语言, 并借助代码生成引擎生成各类主流语言的RPC服务端/客户端模板代码。java

Thrift支持多种不一样的编程语言,包括C++JavaPythonPHPRuby等,本系列主要讲述基于Java语言的Thrift的配置方式和具体使用。apache

正文

Thrift的技术栈

Thrift软件栈的定义很是的清晰, 使得各个组件可以松散的耦合, 针对不一样的应用场景, 选择不一样是方式去搭建服务。编程

Thrift软件栈分层从下向上分别为:传输层(Transport Layer)、协议层(Protocol Layer)、处理层(Processor Layer)和服务层(Server Layer)。后端

  • 传输层(Transport Layer):传输层负责直接从网络中读取写入数据,它定义了具体的网络传输协议;好比说TCP/IP传输等。缓存

  • 协议层(Protocol Layer):协议层定义了数据传输格式,负责网络传输数据的序列化反序列化;好比说JSONXML二进制数据等。bash

  • 处理层(Processor Layer):处理层是由具体的IDL接口描述语言)生成的,封装了具体的底层网络传输序列化方式,并委托给用户实现的Handler进行处理。服务器

  • 服务层(Server Layer):整合上述组件,提供具体的网络线程/IO服务模型,造成最终的服务。网络

Thrift的特性

(一) 开发速度快

经过编写RPC接口Thrift IDL文件,利用编译生成器自动生成服务端骨架(Skeletons)和客户端桩(Stubs)。从而省去开发者自定义维护接口编解码消息传输服务器多线程模型等基础工做。多线程

  • 服务端:只须要按照服务骨架接口,编写好具体的业务处理程序(Handler)即实现类便可。
  • 客户端:只须要拷贝IDL定义好的客户端桩服务对象,而后就像调用本地对象的方法同样调用远端服务。

(二) 接口维护简单

经过维护Thrift格式的IDL(接口描述语言)文件(注意写好注释),便可做为给Client使用的接口文档使用,也自动生成接口代码,始终保持代码和文档的一致性。且Thrift协议可灵活支持接口可扩展性架构

(三) 学习成本低

由于其来自Google Protobuf开发团队,因此其IDL文件风格相似Google Protobuf,且更加易读易懂;特别是RPC服务接口的风格就像写一个面向对象Class同样简单。

初学者只需参照:thrift.apache.org/,一个多小时就能够理解Thrift IDL文件的语法使用。

(四) 多语言/跨语言支持

Thrift支持C++JavaPythonPHPRubyErlangPerlHaskellC#CocoaJavaScriptNode.jsSmalltalk等多种语言,便可生成上述语言的服务器端客户端程序

对于咱们常用的JavaPHPPythonC++支持良好,虽然对iOS环境的Objective-C(Cocoa)支持稍逊,但也彻底知足咱们的使用要求。

(五) 稳定/普遍使用

Thrift在不少开源项目中已经被验证是稳定高效的,例如CassandraHadoopHBase等;国外在Facebook中有普遍使用,国内包括百度、美团小米、和饿了么等公司。

Thrift的数据类型

Thrift 脚本可定义的数据类型包括如下几种类型:

  1. 基本类型:
    • bool: 布尔值
    • byte: 8位有符号整数
    • i16: 16位有符号整数
    • i32: 32位有符号整数
    • i64: 64位有符号整数
    • double: 64位浮点数
    • string: UTF-8编码的字符串
    • binary: 二进制串
  2. 结构体类型:
    • struct: 定义的结构体对象
  3. 容器类型:
    • list: 有序元素列表
    • set: 无序无重复元素集合
    • map: 有序的key/value集合
  4. 异常类型:
    • exception: 异常类型
  5. 服务类型:
    • service: 具体对应服务的类

Thrift的协议

Thrift可让用户选择客户端服务端之间传输通讯协议的类别,在传输协议上整体划分为文本(text)和二进制(binary)传输协议。为节约带宽提升传输效率,通常状况下使用二进制类型的传输协议为多数,有时还会使用基于文本类型的协议,这须要根据项目/产品中的实际需求。经常使用协议有如下几种:

  • TBinaryProtocol:二进制编码格式进行数据传输
  • TCompactProtocol:高效率的、密集二进制编码格式进行数据传输
  • TJSONProtocol: 使用JSON文本的数据编码协议进行数据传输
  • TSimpleJSONProtocol:只提供JSON只写的协议,适用于经过脚本语言解析

Thrift的传输层

经常使用的传输层有如下几种:

  • TSocket:使用阻塞式I/O进行传输,是最多见的模式
  • TNonblockingTransport:使用非阻塞方式,用于构建异步客户端
  • TFramedTransport:使用非阻塞方式,按块的大小进行传输,相似于Java中的NIO

Thrift的服务端类型

  • TSimpleServer:单线程服务器端,使用标准的阻塞式I/O
  • TThreadPoolServer:多线程服务器端,使用标准的阻塞式I/O
  • TNonblockingServer:单线程服务器端,使用非阻塞式I/O
  • THsHaServer:半同步半异步服务器端,基于非阻塞式IO读写和多线程工做任务处理
  • TThreadedSelectorServer:多线程选择器服务器端,对THsHaServer异步IO模型上进行加强

Thrift入门示例

(一) 编写Thrift IDL文件

a). 下载0.10.0Thrift IDL编译器,下载地址:http://thrift.apache.org/docs/install。 经过编译生成器生成.java接口的类文件。

b). 下载Windows安装环境的.exe文件,将thrift.exe的路径加入环境变量中。在Idea上安装Thrift编辑插件。

c). 编写hello.thriftIDL文件:

service HelloWorldService {
  string say(1: string username) } 复制代码

d). 使用代码生成工具生成代码,执行如下命令:

thrift -gen java hello.thrift
复制代码

e). 因为未指定代码生成的目标目录,生成的类文件默认存放在gen-java目录下。这里生成一个HelloWorldService.java类文件,文件大小超过数千行,下面截取一部分核心代码

public class HelloWorldService {
    public interface Iface {
        public String say(String username) throws org.apache.thrift.TException;
    }

    public interface AsyncIface {
        public void say(String username, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException;
    }

    public static class Client extends org.apache.thrift.TServiceClient implements Iface {
        public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
            public Factory() {
            }

            public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
                return new Client(prot);
            }

            public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
                return new Client(iprot, oprot);
            }
        }

        public Client(org.apache.thrift.protocol.TProtocol prot) {
            super(prot, prot);
        }

        public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
            super(iprot, oprot);
        }

        public String say(String username) throws org.apache.thrift.TException {
            send_say(username);
            return recv_say();
        }
        // 省略.....
    }

    public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
        public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
            private org.apache.thrift.async.TAsyncClientManager clientManager;
            private org.apache.thrift.protocol.TProtocolFactory protocolFactory;

            public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
                this.clientManager = clientManager;
                this.protocolFactory = protocolFactory;
            }

            public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
                return new AsyncClient(protocolFactory, clientManager, transport);
            }
        }

        public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {
            super(protocolFactory, clientManager, transport);
        }

        public void say(String username, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException {
            checkReady();
            say_call method_call = new say_call(username, resultHandler, this, ___protocolFactory, ___transport);
            this.___currentMethod = method_call;
            ___manager.call(method_call);
        }
        // 省略.....
    }
    // 省略.....
}
复制代码

对于开发人员而言,使用原生的Thrift框架,仅须要关注如下四个核心内部接口/类Iface, AsyncIface, ClientAsyncClient

  • Iface服务端经过实现HelloWorldService.Iface接口,向客户端的提供具体的同步业务逻辑。
  • AsyncIface服务端经过实现HelloWorldService.Iface接口,向客户端的提供具体的异步业务逻辑。
  • Client客户端经过HelloWorldService.Client的实例对象,以同步的方式访问服务端提供的服务方法。
  • AsyncClient客户端经过HelloWorldService.AsyncClient的实例对象,以异步的方式访问服务端提供的服务方法。

(二) 新建Maven工程

a). 新建maven工程,引入thrift的依赖,这里使用的是版本0.10.0

<dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.10.0</version>
  </dependency>
复制代码

b). 将生成类的HelloWorldService.java源文件拷贝进项目源文件目录中,并实现HelloWorldService.Iface的定义的say()方法。

HelloWorldServiceImpl.java

public class HelloWorldServiceImpl implements HelloWorldService.Iface {
    @Override
    public String say(String username) throws TException {
        return "Hello " + username;
    }
}
复制代码

c). 服务器端程序编写:

SimpleServer.java

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(ServerConfig.SERVER_PORT);
        TServerSocket serverTransport = new TServerSocket(serverSocket);
        HelloWorldService.Processor processor =
                new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldServiceImpl());

        TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
        TSimpleServer.Args tArgs = new TSimpleServer.Args(serverTransport);
        tArgs.processor(processor);
        tArgs.protocolFactory(protocolFactory);

        // 简单的单线程服务模型 通常用于测试
        TServer tServer = new TSimpleServer(tArgs);
        System.out.println("Running Simple Server");
        tServer.serve();
    }
}
复制代码

d). 客户端程序编写:

SimpleClient.java

public class SimpleClient {
    public static void main(String[] args) {
        TTransport transport = null;
        try {
            transport = new TSocket(ServerConfig.SERVER_IP, ServerConfig.SERVER_PORT, ServerConfig.TIMEOUT);
            TProtocol protocol = new TBinaryProtocol(transport);
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            transport.open();

            String result = client.say("Leo");
            System.out.println("Result =: " + result);
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }
}
复制代码

e). 运行服务端程序,服务端指定端口监听客户端链接请求,控制台输出启动日志:

f). 运行客户端程序,客户端经过网络请求HelloWorldServicesay()方法的具体实现,控制台输出返回结果:

这里使用的一个基于单线程同步简单服务模型,通常仅用于入门学习和测试!

总结

本文对Thrift的概念作了相关介绍,体验了一番thrift程序如何编写!

相关连接

  1. Apache Thrift系列详解(一) - 概述与入门

  2. Apache Thrift系列详解(二) - 网络服务模型

  3. Apache Thrift系列详解(三) - 序列化机制


欢迎关注技术公众号: 零壹技术栈

image

本账号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。

相关文章
相关标签/搜索