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 <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的更多相关文章
- Mybatis Learning Notes 1
Mybatis Learning Notes 主要的参考是博客园竹山一叶的Blog,这里记录的是自己补充的内容 实体类属性名和数据库不一致的处理 如果是实体类的结果和真正的数据库的column的名称不 ...
- Apache thrift RPC 双向通信
在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...
- Apache Thrift 跨语言服务开发框架
Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...
- Apache Thrift 环境配置
在 Ubuntu 14.04 下Apache Thrift 的安装方法: 1安装依赖包 sudo apt-get install libboost-dev libboost-test-dev libb ...
- Apache Thrift 服务开发框架学习记录
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...
- Apache Thrift
Baidu Thrift Google Thrift Apache Thrift - 可伸缩的跨语言服务开发框架
- Apache Thrift - 可伸缩的跨语言服务开发框架
To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...
- Apache Thrift学习之二(基础及原理)
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且 ...
- Apache Thrift学习之一(入门及Java实例演示)
目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...
随机推荐
- CSS的字体样式
CSS的字体样式 1. span标签(约定俗成:重要的东西用它括起来) 首选介绍一个约定俗成的东西:span标签.一般将想要突出的东西,比较重要的东西,用span标签括起来. 比如,”学习Java“这 ...
- centos7解压压缩zip文件
一.安装支持ZIP的工具 yum install -y unzip zip 二.解压zip文件 unzip 文件名.zip 二. 压缩一个zip文件 zip 文件名.zip 文件夹名称或文件名称
- [HEOI2016] 字符串 - 后缀数组,主席树,ST表,二分
[HEOI2016] 字符串 Description 给定一个字符串 \(S\), 有 \(m\) 个询问,每个询问给定参数 \((a,b,c,d)\) ,求 \(s[a..b]\) 的子串与 \(s ...
- docker installation and usage
From 一.环境配置的难题 软件开发最大的麻烦事之一,就是环境配置.用户计算机的环境都不相同,你怎么知道自家的软件,能在那些机器跑起来? 用户必须保证两件事:操作系统的设置,各种库和组件的安装.只有 ...
- windows下划分逻辑分区
运行命令窗口后,我们输入命令"diskpart"回车! 然后我们在DISKPART>后面输入select disk 0 选择我们的硬盘,然后回车!当然如果你电脑上有好几块硬盘 ...
- C++中类成员变量的初始化问题
C++11之后允许对非静态成员变量进行初始化(in-class initialization),不过对于非fundamental(非基本数据)类型需要采用的是initializer_list来实现的 ...
- java_jsp_导入第三方jar包
问题:把第三方jar包放在tomcat common/lib目录下之后,在jsp页面中引用不到 解决方法:将jar包放在你的项目目录下的WEB-INF/lib/目录下 解决 希望对大家又所帮助 以上
- 微服务监控平台获取网关(zuul)配置列表
步骤: (1)读取zuul的配置文件,获取路由配置项信息: private static Properties props; static { String fileName = "appl ...
- MySQL学习(九)小结
redo-log 和 bin-log 是如何联系起来的? update 语句在更新的时候先更新内存后,写 redo-log 然后 bin-log ,其中后面一步是使用了两阶段提交, 也就是每一个更新都 ...
- Windows下PHP安装 Imagick 扩展
1.下载拓展下载地址一: http://windows.php.net/downloads/pecl/releases/imagick/下载地址二: https://pecl.php.net/pack ...