thrift RPC通信

thrift RPC通信

thrift 作为一种RPC机制,可以用作本地进程间通信,进程间通信常用的方法有封装socket、封装管道等自研方式,还有开源的的通信库libevent、thrift、dbus等方式。

thrift简介

Thrift是一种接口描述语言和二进制通讯协议,它被用来定义和创建跨语言的服务。它被当作一个远程过程调用(RPC)框架来使用,是由Facebook为“大规模跨语言服务开发”而开发的。它通过一个代码生成引擎联合了一个软件栈,来创建不同程度的、无缝的跨平台高效服务,可以使用C#、C++(基于POSIX兼容系统)、Cappuccino、Cocoa、Delphi、Erlang、Go、Haskell、Java、Node.js、OCaml、Perl、PHP、Python、Ruby和Smalltalk。虽然它以前是由Facebook开发的,但它现在是Apache软件基金会的开源项目了。该实现被描述在2007年4月的一篇由Facebook发表的技术论文中,该论文现由Apache掌管。
以上引自百度百科。

thrift有自己的一套框架,可以根据使用者编写的接口文件,生成不同语言的实现方式,可以在不同语言程序编写的进程间通信。
thrift是CS框架,多个客户端对应一个服务端,每个进程就是一个客户端。

编译安装

需要注意一下几点:
1、早期thrift需要依赖boost库,boost库很大
2、thrift库依赖于openssl库

linux系统安装步骤

  1. 官方下载源码:https://thrift.apache.org/download
  2. 安装依赖:apt install libboost-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
  3. 解压、配置、编译
tar xvf thrift-0.16.0.tar.gz
cd thrift-0.16.0.tar.gz
./configure --with-qt5=no --with-cl=no --with-java=no --with-php=no --with-python=no --disable-tests
make ;make install
thrift -version

编码

编写接口文件

接口文件时一种IDL(接口定义语言),其中包括数据结构的声明和方法接口的声明。
thrift支持的数据类型为:

bool: 布尔类型(true or false)
byte: 	8位带符号整数
i16:	16位带符号整数
i32:	32位带符号整数
i64:	16位带符号整数
double:		64位浮点数
string:		字符串(utf-8)
map<k, v>: 	图
list<v>:	列表
set<v>:		集合

#required 必选
#optional 可选
struct User{
	1: required string name,
	2: required bool isA,
	3: optional i32 age
}

enum level{
	low,
	mod,
	high
}

一个idl文件hello.thrift


service HelloService
{
	void Hello(1: string name);
}

可以看出在idl中定义数据结构和接口的规则,每个参数前面需要有参数索引加’:’ 符号。

生成接口代码

使用thrift根据idl生成代码

zjy@zjy-T440:~/thrift/testHello$ thrift --gen cpp hello.thrift 
zjy@zjy-T440:~/thrift/testHello$ tree
.
├── gen-cpp
│   ├── HelloService.cpp
│   ├── HelloService.h
│   ├── HelloService_server.skeleton.cpp
│   └── hello_types.h
└── hello.thrift
1 directory, 5 files

重命名HelloService_server.skeleton.cpp为service.cpp
增加client.cpp文件

zjy@zjy-T440:~/thrift/testHello$ cat client.cpp 
#include <stdio.h>
#include <string>
#include "transport/TSocket.h"
#include "protocol/TBinaryProtocol.h"
#include "server/TSimpleServer.h"
#include "transport/TServerSocket.h"
#include "transport/TBufferTransports.h"

#include "hello_types.h"
#include "HelloService.h"

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
 
int main()
{
    //shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    HelloServiceClient client(protocol);
	try
	{
		transport->open();
		client.hello("cpper.info");
		transport->close();
	}
	catch(TException& tx)
	{
		printf("ERROR:%s\n",tx.what());
	}
}

至此客户端代码,服务端代码都有了,HelloService.cpp,HelloService.h文件为接口的实现文件,这两个文件被服务端和客户端公用,hello_types.h文件为数据结构文件。

编译 生成client service

编写Makefile

zjy@zjy-T440:~/thrift/testHello/gen-cpp$ cat Makefile 
all:
	g++ -o server service.cpp HelloService.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift
	g++ -o client client.cpp HelloService.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift
zjy@zjy-T440:~/thrift/testThrift/gen-cpp$ make
g++ -o server service.cpp HelloService.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift
g++ -o client client.cpp HelloService.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift
zjy@zjy-T440:~/thrift/testThrift/gen-cpp$ ls
client  client.cpp  HelloService.cpp  HelloService.h  hello_types.h  Makefile  server  service.cpp
zjy@zjy-T440:~/thrift/testThrift/gen-cpp$ 

同时打开两个终端,一个运行client、一个运行server,可以正常调用了。
server程序可放到其他电脑上,便可实现远程调用。
源码连接:
下载源码
https://download.csdn.net/download/caochenxian/85519069

thrift使用场景

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值