简介

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 <language> <Thrift filename>
thrift --gen py data.thrift
thrift --gen java data.thrift

源码位置:

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 biaogejiushibiao@outlook.com
* @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();
}
}
}

服务器:

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 biaogejiushibiao@outlook.com
* @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<PersonServiceImpl> 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();
}
}

Apache Thrift Learning Notes的更多相关文章

  1. Mybatis Learning Notes 1

    Mybatis Learning Notes 主要的参考是博客园竹山一叶的Blog,这里记录的是自己补充的内容 实体类属性名和数据库不一致的处理 如果是实体类的结果和真正的数据库的column的名称不 ...

  2. Apache thrift RPC 双向通信

    在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...

  3. Apache Thrift 跨语言服务开发框架

    Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...

  4. Apache Thrift 环境配置

    在 Ubuntu 14.04 下Apache Thrift 的安装方法: 1安装依赖包 sudo apt-get install libboost-dev libboost-test-dev libb ...

  5. Apache Thrift 服务开发框架学习记录

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...

  6. Apache Thrift

    Baidu Thrift  Google Thrift Apache Thrift - 可伸缩的跨语言服务开发框架

  7. Apache Thrift - 可伸缩的跨语言服务开发框架

    To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...

  8. Apache Thrift学习之二(基础及原理)

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且 ...

  9. Apache Thrift学习之一(入门及Java实例演示)

    目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...

随机推荐

  1. python接口自动化测试之根据excel中的期望结果是否存在于请求返回的响应值中来判断用例是否执行成功

    1.首先在excel中填写好预期结果的值 这里判断接口成功的依据是预期结果值是否存在于接口的返回数据中. 一般接口的返回值都是json对象,我们需要将json对象转换为json格式的字符串 如下图,进 ...

  2. 2020算法设计竞赛 H 坐火车

    链接:https://ac.nowcoder.com/acm/contest/3005/H来源:牛客网 大致题意:让我们针对每一个数,求这个数左区间和右区间颜色相同(也就是数字相同)得对数: 比如:左 ...

  3. Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099

    方法:把catalina.bat 文件中set JAVA_OPTS= -Xmx1024M -Xms512M -XX:MaxPermSize=256m这行去掉,具体看下面两篇博客 https://blo ...

  4. SP1716 GSS3 - Can you answer these queries III - 动态dp,线段树

    GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) ...

  5. ORA-01935: missing user or role name

    问题描述 ORA-01935: missing user or role name ORA-01935:缺少用户或角色名

  6. 140. 单词拆分 II

    Q: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使用字典 ...

  7. 算法导论2-4 O(nlgn)时间复杂度求逆序对

    def mergesort(nums,le,ri): if le>ri-2: return 0 mi=le+(ri-le)//2 a=mergesort(nums,le,mi) b=merges ...

  8. NetMQ用作IPC的实例

    发送端/接收端 using System; using System.Threading; using NetMQ; using NetMQ.Sockets; namespace NetMQIPCSe ...

  9. JS高级---继承

    继承 面向对象编程思想: 根据需求, 分析对象, 找到对象有什么特征和行为, 通过代码的方式来实现需求, 要想实现这个需求,就要创建对象 ,要想创建对象, 就应该显示有构造函数, 然后通过构造函数来创 ...

  10. NOIP做题练习(day5)

    A - 中位数图 题面 题解 先找出题意中的\(b\)所在的位置. 再以这个位置为中心,向右\(for\)一遍有多少个大于/小于该数的数 大于就\(++cs\) 小于就\(--cs\). 因为这个数是 ...