使用thrift进行RPC通信(附c程序示例)

文章介绍了在需要跨进程、跨主机通信时,可以使用MQ或RPC框架,如Thrift和gRPC。对于实时性要求高的同步场景,gRPC是一个好选择。Thrift以其丰富的语言支持脱颖而出,尤其适合C程序与其他语言的通信。文中详细阐述了Thrift在C程序中的使用步骤,包括环境安装、定义接口描述文件、生成接口代码和整合到项目中,并提供了CentOS下安装Thrift的详细步骤,以及解决编译和运行时可能遇到的问题。

前言

为了实现不同语言的程序跨进程、跨主机通信,一般可以采用mq或rpc框架来实现。
对于异步通知的场景可以使用mq,如zeroMQ
zeroMq

但对于某些实时性较强且同步的应用场景,使用成熟的rpc框架来实现也是一种比较更好的选择。

开源的rpc框架有很多,其中跨语言的rpc框架以使用google公司采用protobuf协议的grpc框架,和facebook公司的thrift框架最为知名。
grpc
thrift

thrift与grpc对比

关于这两个框架详细信息的对比文章有许多,此处省略5000字……

这里仅从语言支持度来进行对比一下:

grpcthrift
C#、NET、C++、Dart、Go、Java、Kotlin、Node、Objective-C、PHP、Python、RubyC (glib)、C++、C#、Cocoa、Common LISP、D、Dart、.NET、Erlang、Haxe、Go、Java、JavaScript、Lua、Node.js、OCaml、Perl、PHP、Python、Ruby、Rust、Smalltalk、Swift

从支持的语言方面来看,thrift官方支持的语言类型则更丰富一些。

如需要C程序和其他语言进行通信,则可以更加倾向于使用thrift框架。(尽管grpc官方没有对c的支持,但实际使用中有对应的解决方案,网上相关的资料也较多。以go版本的onos为例,可以在它的onos-e2-sm项目中看到c与go程序的调用示例代码。)

本文中主要介绍c程序使用thrift的主要步骤。

thrift使用步骤

thrift的使用步骤一般分为以下3个步骤:

  1. 定义thrift接口描述文件
  2. 生成对应语言的接口代码
  3. 将生成的代码整合到项目中
    thrift-useage
    整体来看还是比较简单的,官方示例也比较多。

thrift-c程序环境安装

需要特殊说明的是,由于c程序和go、java程序相比由于没有包管理器,且处于系统的较底层,安装方式上看也稍微复杂一些。
使用前需要先将thrift的c依赖包在系统中进行安装,否则可能无法编译。
具体安装方式也可支持参考thrift官网的安装说明
centos下安装thrift
ubuntu下安装thrift

这里以centos为例,主要的操作步骤如下:

基础依赖安装

yum install autoconf automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel

glib安装,以支持生成及编译thrift的c依赖库

yum install glib*

下载thrift源码

git clone --branch 0.18.1 https://github.com/apache/thrift.git

开始准备编译依赖库

#切换到thrift源码根目录
cd thrift
#生成配置文件
./bootstrap.sh
#执行配置
./configure --prefix=/usr/local/

./configure阶段会输出支持的thrift语言,片段如下:

thrift 0.18.1

Building C (GLib) Library .... : yes
Building C++ Library ......... : no
Building Common Lisp Library.. : no
Building D Library ........... : no
Building Dart Library ........ : no
Building .NET Standard Library : no
Building Erlang Library ...... : no
Building Go Library .......... : yes
Building Haxe Library ........ : no
Building Java Library ........ : no
Building Kotlin Library ...... : no
Building Lua Library ......... : no
Building NodeJS Library ...... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Python Library ...... : yes
Building Py3 Library ......... : no
Building Ruby Library ........ : no
Building Rust Library ........ : no
Building Swift Library ....... : no

如果要编译的语言右边显示为no,则说明当前系统缺少对应的依赖项,将无法完成所选语言依赖库的安装。
以C (Glib)Library为例如果显示为no,则可能是缺少glib依赖,需要先在系统中安装好glib后再执行。

如果想要不安装某个语言的库,可以在configure后面跟上 –without-[语言] 屏蔽某语言库的安装。
如要屏蔽perl、nodejs、cpp的依赖库,则命令如下:

./configure --prefix=/usr/local/ --without-perl --without-nodejs --without-cpp

执行make编译thrift及依赖库

sudo make
sudo make install

thrift的编译过程较长,稍等个几分钟左右就能编译好了。

示例程序运行

在编译完thrift的源码后,如果没有报错会在thrift的 /tutorial/c_glib下将demo的client和server程序编译出来,生成tutorial_clienttutorial_server可执行文件。

可以先后执行tutorial_servertutorial_client进行验证:
tutorial_run
从图片上可以看到,demo示例程序通信成功啦~

常见错误解决办法

如果在编译成功后,运行程序时出现以下报错

thrift: error while loading shared libraries: libthriftc.so.0: cannot open shared object file: No such file or directory

可通过如命令解决,以确保 /etc/ld.so.conf文件中有 /usr/local/lib的配置

root@ubuntu:/home/phy/thrift# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

root@ubuntu:/home/phy/thrift# echo "/usr/local/lib" >> /etc/ld.so.conf
root@ubuntu:/home/phy/thrift# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

/usr/local/lib
root@ubuntu:/home/phy/thrift# ldconfig
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水中加点糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值