Thrift 官网地址:https://thrift.apache.org/   使用版本 0.9.3

Thrift  0.9.3 下载地址:https://thrift.apache.org/download   下载thrift-0.9.3.tar.gz 和  thrift-0.9.3.exe

Thrift allows developers to define datatypes and service interfaces in a single language-neutral file and generate all the necessary code to build RPC clients and servers.

Hello.thrift 文件:

namespace java  service.demo service Hello{ string helloString(1:string para) i32 helloInt(1:i32 para) bool helloBoolean(1:bool para) void helloVoid() string helloNull() }

切换到thrift-0.9.3.exe 所在目录。执行如下命令:
thrift-0.9.3 --gen java Hello.thrift

会在gen-java 目录下面生成Hello.java文件。

建立maven项目,添加libthrift 依赖。

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.9.3</version>
</dependency>

HelloServiceImpl 类来实现在Hello.thrift定义的方法。

public class HelloServiceImpl implements Hello.Iface {@Overridepublic String helloString(String para) throws TException {return para; }@Overridepublic int helloInt(int para) throws TException {try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } return para; }@Overridepublic boolean helloBoolean(boolean para) throws TException {return para; }@Overridepublic void helloVoid() throws TException {System.out.println("Hello World"); }@Overridepublic String helloNull() throws TException {// TODO Auto-generated method stubreturn null;}
}

Thrift RPC 服务器端代码:JavaServer

public class JavaServer {public static Hello.Processor<HelloServiceImpl> processor;public static void main(String [] args) {try {processor = new Hello.Processor<HelloServiceImpl>(new HelloServiceImpl());Runnable simple = new Runnable() {public void run() {simple(processor);}};      new Thread(simple).start();} catch (Exception x) {x.printStackTrace();}}public static void simple(Processor<HelloServiceImpl> processor) {try {TServerTransport serverTransport = new TServerSocket(9090);TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));// Use this for a multithreaded server// TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));System.out.println("Starting the simple server...");server.serve();} catch (Exception e) {e.printStackTrace();}}
}

Thrift 客户端调用代码:JavaClient.java

public class JavaClient {public static void main(String[] args) {try { // 设置调用的服务地址为本地,端口为 9090 TTransport transport = new TSocket("localhost", 9090); transport.open(); // 设置传输协议为 TBinaryProtocol TProtocol protocol = new TBinaryProtocol(transport); Hello.Client client = new Hello.Client(protocol); // 调用服务的 helloVoid 方法client.helloVoid(); try {client.helloNull();} catch (Exception e) {// TODO: handle exception}String helloString = client.helloString("hello world");System.out.println("helloString:"+helloString);int helloInt = client.helloInt(111);System.out.println("helloInt:"+helloInt);transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } }
}

Apache Thrift goal is to make reliable, performant communication and data serialization across languages as efficient and seamless as possible. Originally developed at Facebook, Thrift was open sourced in April 2007 and entered the Apache Incubator in May, 2008. Thrift became an Apache TLP in October, 2010.

Transport

The Transport layer provides a simple abstraction for reading/writing from/to the network. This enables Thrift to decouple the underlying transport from the rest of the system (serialization/deserialization, for instance).

While the above protocols describe "what" is transmitted, Thrift's transports are the "how". Here are a number of transports that Thrift supports:

TSocket - Uses blocking socket I/O for transport.
TFramedTransport - Sends data in frames, where each frame is preceded by a length. This transport is required when using a non-blocking server.
TFileTransport - This transport writes to a file. While this transport is not included with the Java implementation, it should be simple enough to implement.
TMemoryTransport - Uses memory for I/O. The Java implementation uses a simple ByteArrayOutputStream internally.
TZlibTransport - Performs compression using zlib. Used in conjunction with another transport. Not available in the Java implementation.

Here are some of the methods exposed by the Transport interface:

  • open
  • close
  • read
  • write
  • flush

In addition to the  Transport  interface above, Thrift also uses a  ServerTransport  interface used to accept or create primitive transport objects. As the name suggest,  ServerTransport  is used mainly on the server side to create new Transport objects for incoming connections.

  • open
  • listen
  • accept
  • close

Here are some of the transports available for majority of the Thrift-supported languages:

  • file: read/write to/from a file on disk
  • http: as the name suggests

Protocol

The Protocol abstraction defines a mechanism to map in-memory data structures to a wire-format. In other words, a protocol specifies how datatypes use the underlying Transport to encode/decode themselves. Thus the protocol implementation governs the encoding scheme and is responsible for (de)serialization. Some examples of protocols in this sense include JSON, XML, plain text, compact binary etc.

Thrift supports both text and binary protocols. The binary protocols outperform the text protocols, but there are times when the text protocols may be useful (such as in debugging). Some of the protocols Thrift supports:
TBinaryProtocol - A straight-forward binary format encoding numeric values as binary, rather than converting to text.
TCompactProtocol - Very efficient, dense encoding of data (See details below).

TDenseProtocol - Similar to TCompactProtocol but strips off the meta information from what is transmitted, and adds it back in at the receiver. TDenseProtocol is still experimental and not yet available in the Java implementation.
TJSONProtocol - Uses JSON for encoding of data.
TSimpleJSONProtocol - A write-only protocol using JSON. Suitable for parsing by scripting languages
TDebugProtocol - Uses a human-readable text format to aid in debugging.

interface TProcessor {
    bool process(TProtocol in, TProtocol out) throws TException
}

Service-specific processor implementations are generated by the compiler. The Processor essentially reads data from the wire (using the input protocol), delegates processing to the handler (implemented by the user) and writes the response over the wire (using the output protocol).

Server

A Server pulls together all of the various features described above:

  • Create a transport
  • Create input/output protocols for the transport
  • Create a processor based on the input/output protocols
  • Wait for incoming connections and hand them off to the processor

Lastly, Thrift provides a number of servers:

TSimpleServer - A single-threaded server using std blocking io. Useful for testing.
TThreadPoolServer - A multi-threaded server using std blocking io.
TNonblockingServer - A multi-threaded server using non-blocking io (Java implementation uses NIO channels). TFramedTransport must be used with this server.

Creating A Thrift Service

Creating a Thrift service first requires creating a Thrift file describing a service, generating the code for the service, and finally writing some supporting code to start the service and client code to call it.

Thrift 小试牛刀相关推荐

  1. Windows系统下安装Thrift的方法

    安装 下载 下载地址:http://archive.apache.org/dist/thrift/0.13.0/ 将thrift-0.13.0.exe放到一个文件下,如F:\thrift下,将其重命名 ...

  2. Thrift的服务器和客户端Python案例

    服务器 Thrift提供的常见服务端类型有一下几种: thrift.server.TServer.TSimpleServer 单线程服务器 thrift.server.TServer.TThreade ...

  3. Thrift协议与传输选择

    1 协议 Thrift 可以让用户选择客户端与服务端之间传输通信的消息协议类别,如我们前面所讲总体划分为文本 (text) 和二进制 (binary) ,为节约带宽,提高传输效率,一般情况下使用二进制 ...

  4. Thrift的接口定义语言IDL

    Thrift的IDL可以使用下面的语法来定义描述接口. 1 基本类型 bool:布尔值,true 或 false byte:8 位有符号整数 i16:16 位有符号整数 i32:32 位有符号整数 i ...

  5. CentOS7:Thrift的安装

    使用Thrift需要进行安装,主要安装两个工具: 接口定义文件(.thrift)的编译器 不同语言的公共基础库程序 1 安装依赖工具和库 yum install automake libtool fl ...

  6. Thrift架构与使用方法

    Thrift是由Facebook为"大规模跨语言服务开发"而开发的,现在是Apache软件基金会的开源项目. Thrift实现了一种接口描述语言和二进制通讯协议,用来定义和创建跨语 ...

  7. apache thrift分析

    thrift是一个用来实现跨语言的远程调用(RPC Remote Procedure Call)的软件框架.根据接口定义语言(IDL Interface definition lanuage) 并借助 ...

  8. php thrift 报错,thrift安装遇到的问题以及解决方法(必看篇)

    1. 必须安装boost.最新的稳定版是1.48.0. 1.1.先下载:http://sourceforge.NET/projects/boost/files/boost/1.48.0/ 选择tar. ...

  9. Node.js开发WEB项目后端接口API,基于mysql5.7数据库(小试牛刀)

    项目结构 main.js(入口文件,开启9999端口监听,实现RESTful风格接口访问) const express = require("express"); const ap ...

最新文章

  1. 命令行参数tail c语言,osg学习笔记2, 命令行参数解析器ArgumentParser
  2. java可以入侵电脑系统吗_如何通过tomcat入侵远程计算机系统
  3. Spring Boot 数据库连接池入门
  4. 【Flask】jinja2过滤器的使用
  5. Linux中修改登录提示文件
  6. 没有人会告诉您乘坐飞机时的几个事实 但是您一定要知道
  7. 微课与计算机技术的论文,微课在高校计算机教学的运用论文
  8. Java读取指定目录下的所有文件名
  9. python里面的list
  10. mysql去重查询group_MySQL distinct 与 group by 去重(where/having)
  11. The Economist经济学人是如何使用Go语言构建内容平台微服务架构的?
  12. javaScript笔记整理(一)
  13. walking机器人入门教程-slam_toolbox算法建图
  14. 【工作笔记】004 tapestry框架
  15. G711(PCM/PCMA/PCMU),G721,G723,G729等 音频编解码
  16. pacemaker corosync 概念
  17. GlobalSign和DigiCert对比
  18. windows10系统-11-高效软件
  19. 胡喜:从 BASIC 到 basic ,蚂蚁金服技术要解决两个基本的计算问题
  20. 数学统计:均值、标准差、方差、协方差

热门文章

  1. 【众说区块链】公链是否一定要发币,Token到底应该怎么理解?
  2. Android自定义实现按周签到打卡功能
  3. 如何快速将图片中的文字提取出来
  4. 32位系统装8g内存条?能用吗
  5. android手机8g内存够用嘛,安卓旗舰机8GB运行内存到底够不够用?有必要上12GB吗?...
  6. excel表格分割线一分为二_Anki+思维导图的两种方法(Anki+表格,Anki+幕布)
  7. InnerHTML、InnerText、outerHTML的用法与区别
  8. (C语言)汉诺塔与受限汉诺塔
  9. 性能测试能力提升-JVM GC原理
  10. 大学计算机基础知识电子版,大学计算机基础考试知识点(完整版).pdf