thrift

1.简介

    常见的服务调用有基于soap的web service方式,也有基于restful方式的接口方式,数据传输格式有json和xml,xml方式传输数据效率相对较低,json方式体积小,相对不够完善。Facebook 开发的远程服务调用框架 Apache Thrift,它采用接口描述语言定义并建立服务,支持可扩展的跨语言服务开发,所包含的代码生成引擎能够在多种语言中,如 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk 等建立高效的、无缝的服务,其传输数据采用二进制格式,相对 XML 和 JSON 体积更小,对于高并发、大数据量和多语言的环境更有优点。--来自html

2.thrift原理

image.png

TTransport层:表明thrift的数据传输方式,thrift定义了以下几种经常使用数据传输方式java

  • TSocket: 阻塞式socket;
  • TFramedTransport: 以frame为单位进行传输,非阻塞式服务中使用;
  • TFileTransport: 以文件形式进行传输;

TProtocol层:表明thrift客户端和服务端之间传输数据的协议,通俗来说就是客户端和服务端之间传输数据的格式(例如json等),thrift定义了以下几种常见的格式web

  • TBinaryProtocol: 二进制格式;
  • TCompactProtocol: 压缩格式;
  • TJSONProtocol: JSON格式;
  • TSimpleJSONProtocol: 提供只写的JSON协议;

thrift支持的Server模型apache

  • TSimpleServer: 简单的单线程服务模型,经常使用于测试;
  • TThreadPoolServer: 多线程服务模型,使用标准的阻塞式IO;
  • TNonBlockingServer: 多线程服务模型,使用非阻塞式IO(须要使用TFramedTransport数据传输方式);
  • THsHaServer: THsHa引入了线程池去处理,其模型读写任务放到线程池去处理,Half-sync/Half-async处理模式,Half-async是在处理IO事件上(accept/read/write io),Half-sync用于handler对rpc的同步处理;

3.使用流程

  1. 在windows上下载编译器

    下载地址json

  2. 编写thrift idl文件windows

    文件名:GeneralInterface.thrift
    
    namespace java com.xiayu.thrift
    
    service GeneralThriftService{
        string generalService(1:string generalPar)
    }
  3. 生成java文件restful

    thrift-0.13.0.exe -r -gen java ./GeneralInterface.thrift
  4. 项目中加入thrift依赖(maven)多线程

    <dependency>
         <groupId>org.apache.thrift</groupId>
         <artifactId>libthrift</artifactId>
         <version>0.13.0</version>
     </dependency>
     <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>1.7.5</version>
     </dependency>
  5. 服务端并发

    package com.xiayu.thrift;
    
      import org.apache.thrift.TProcessor;
      import org.apache.thrift.protocol.TBinaryProtocol;
      import org.apache.thrift.protocol.TCompactProtocol;
      import org.apache.thrift.server.*;
      import org.apache.thrift.transport.*;
    
      public class ThriftServer {
    
          private static int port = 8089;
    
          public static void main(String[] args) {
              try {
      //              -----------单线程阻塞io类型-----------------------
      //            TProcessor processor = new GeneralThriftService.Processor<GeneralThriftService.Iface>(new ThriftGeneralService());
      //            TServerSocket serverTransport = new TServerSocket(port);
      //            TTransportFactory transportFactory = new TFramedTransport.Factory();
      //            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
      //            TServer.Args tArgs = new TServer.Args(serverTransport);
      //            tArgs.protocolFactory(factory);
      //            tArgs.transportFactory(transportFactory);
      //            tArgs.processor(processor);
      //            TServer server = new TSimpleServer(tArgs);
      //            server.serve();
      //            System.out.println("Thrift Server started");
      //            -------------------------------------
      //             使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
      //            TProcessor processor = new GeneralThriftService.Processor<GeneralThriftService.Iface>(new ThriftGeneralService());
      //            TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(port);
      //            TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
      //            tnbArgs.processor(processor);
      //            tnbArgs.transportFactory(new TFramedTransport.Factory());
      //            tnbArgs.protocolFactory(new TCompactProtocol.Factory());
      //            TServer server = new TNonblockingServer(tnbArgs);
      //            server.serve();
      //            -------------------------------------
      //          使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
      //            TProcessor processor = new GeneralThriftService.Processor<GeneralThriftService.Iface>(new ThriftGeneralService());
      //            TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(port);
      //            TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
      //            tnbArgs.processor(processor);
      //            tnbArgs.transportFactory(new TFramedTransport.Factory());
      //            tnbArgs.protocolFactory(new TCompactProtocol.Factory());
      //            TServer server = new TNonblockingServer(tnbArgs);
      //            server.serve();
      //          使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
                  TProcessor processor = new GeneralThriftService.Processor<GeneralThriftService.Iface>(new ThriftGeneralService());
                  TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(port);
                  TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
                  tnbArgs.processor(processor);
                  tnbArgs.transportFactory(new TFramedTransport.Factory());
                  tnbArgs.protocolFactory(new TCompactProtocol.Factory());
                  TServer server = new TNonblockingServer(tnbArgs);
                  server.serve();
              } catch (TTransportException e) {
                  System.out.println("Starting Thrift Server......Error!!!");
                  e.printStackTrace();
              }
          }
      }
  6. 客户端框架

    package com.xiayu.thrift;
    
      import org.apache.thrift.TException;
      import org.apache.thrift.protocol.TBinaryProtocol;
      import org.apache.thrift.protocol.TCompactProtocol;
      import org.apache.thrift.protocol.TProtocol;
      import org.apache.thrift.transport.TFramedTransport;
      import org.apache.thrift.transport.TSocket;
      import org.apache.thrift.transport.TTransport;
      import org.apache.thrift.transport.TTransportException;
    
      public class ThriftClient {
    
          private static int port = 8089;
    
          public static void main(String[] args) {
              try {
      //              -----------单线程阻塞io类型-----------------------
      //            TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",port , 5000));
      //            TProtocol protocol = new TBinaryProtocol(transport);
      //            GeneralThriftService.Client client = new GeneralThriftService.Client(protocol);
      //            transport.open();
      //            String string = client.generalService("request paramter");
      //            System.out.println(string);
      //            transport.close();
      //            -------------------------------------------------------
      //            使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
      //            TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",port , 5000));
      //            TProtocol protocol = new TCompactProtocol(transport);
      //            GeneralThriftService.Client client = new GeneralThriftService.Client(protocol);
      //            transport.open();
      //            String string = client.generalService("request paramter");
      //            System.out.println(string);
      //            transport.close();
      //          使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
      //            TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",port , 5000));
      //            TProtocol protocol = new TCompactProtocol(transport);
      //            GeneralThriftService.Client client = new GeneralThriftService.Client(protocol);
      //            transport.open();
      //            String string = client.generalService("request paramter");
      //            System.out.println(string);
      //            transport.close();
      //          使用非阻塞式IO,服务端和客户端须要指定TFramedTransport数据传输的方式
                  TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",port , 5000));
                  TProtocol protocol = new TCompactProtocol(transport);
                  GeneralThriftService.Client client = new GeneralThriftService.Client(protocol);
                  transport.open();
                  String string = client.generalService("request paramter");
                  System.out.println(string);
                  transport.close();
              } catch (TTransportException e) {
                  e.printStackTrace();
              } catch (TException e) {
                  e.printStackTrace();
              }
          }
      }
  7. 结果
    image.png

参考

相关文章
相关标签/搜索