简介

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. Codeforces 524C.The Art of Dealing with ATM(暴力)

    我先采用了智障解法(n * n枚举...刚开始把n看成1000了还以为能过) 理所当然的t了,不过我怀疑优化一下能过?(感觉数据不太行的亚子 然后就是O(n * k * k)的解法,看到好多人快乐二分 ...

  2. Centos7添加软链接

    1.pycharm添加软连接: 命令行模式中输入命令: ln -s /root/pycharm-2018.1/bin/pycharm.sh /usr/bin/pycharm ps:代码中/root/p ...

  3. C# ODP.Net oracle数据库操作 支持不安装客户端

    下载: http://download.oracle.com/otn/other/ole-oo4o/ODTwithODAC1110720.zip?AuthParam=1414811820_e61f2f ...

  4. python itertool 浅谈迭代工具

    1.概述 Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个“无限”迭代器: import itertools natuals ...

  5. python3练习100题——012

    今天继续,答案都通过py3测试. 原题链接:http://www.runoob.com/python/python-exercise-example12.html 题目:判断101-200之间有多少个 ...

  6. bitlocker对磁盘进行加密解密

    1,bitlocker是什么? BitLocker即Windows BitLocker驱动器加密.是微软在Windows Vista中新增的一种数据保护功能.使用BitLocker可以加密磁盘.主要用 ...

  7. 配置Mongodb

    MS Windows: 常用命令: 查看帮助 mongod help/mongod -h 查看版本 mongod --version 启动/停止/重启服务 net start/stop/restart ...

  8. Photoshop——APP设计规范

    随着Android和iOS语言的兴起,能够在手机上运行的APP软件已经成为了目前移动应用技术的焦点,APP的UI设计随之也越来越受到重视. 用户的需求不断增加,技术也在不断的更新,UI设计也越来越被重 ...

  9. mybatis(六):设计模式 - 装饰器模式

  10. Web API和Web Service

    首先,Web API是由Web Service演变而来,它们两者关系就是所有Web Service都是API,但并非所有API都是Web Service.其次,两者都有利于信息的传输,但Web API ...