本文章也同时发表在个人博客Thrift在Windows及Linux平台下的安装和使用示例上。

thrift介绍

Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的RPC(远程服务调用)框架。

本文主要目的是分别介绍在Windows及Linux平台下的Thrift安装步骤,以及实现一个简单的demo演示Thrift的使用方法。更多Thrift原理留在以后再行介绍。

thrift安装

源码下载:thrift官网,或者thrift-github地址,我下载的是thrift-0.9.3.tar.gz

安装依赖库

  1. boost

    boost的编译就不再这里介绍了,我分别使用了boost1.55或boost1.49编译通过;
  2. libevent

    按需编译,如果不需要异步server就可以不编译libevent,否则可以点此下载libevent-2.0.21-stable;
  3. openssl

    下载针对你系统版本的openssl库,windows下有编译好的二进制文件,可以直接下载,32位/62位系统openssl; Linux发行版一般都自带ssl库;

thrift在Windows下的安装

我是在Windows7 64bit, VS2010编译的。 Windows下编译倒也不麻烦,简单介绍如下:

  1. 解压缩源代码,进入到lib\cpp目录下,打开Thrift.sln,里面有libthrift和libthriftnb两个工程,其中libthrift工程是常规的阻塞型server端(单线程server,一个连接一个线程server,线程池server),libthriftnb工程是非阻塞(non-blocking)模式的服务server端,也只有编译libthriftnb时才需要依赖libevent库,否则可以不编译libevent库;
  2. 设置依赖库头文件和库文件,这就不再介绍了;
  3. 编译,顺利的话就OK了,会在lib\cpp\Debug目录下生成libthrift.lib和libthriftnb.lib(如果编译的话);

说明: thrift-0.9.3这一版的release其实在windows下是编译不过的,因为vs工程中要编译的Thrift.cpp已经不存在了,从工程中移除就可以顺利编译了,参考thrift-pull-739

另外还可以自行编译thrift文件的生成工具,当然也可以直接从官网下载,这里给出编译步骤:

  1. 将compiler\cpp\src\windows\version.h.in文件拷贝到compiler\cpp\src\目录下,并重命名为version.h;
  2. 到compiler\cpp目录下,打开compiler.sln,编译即可

thrift在linux(Centos)下的安装

我是在Centos6.4 64bit,g++ 4.4.7编译的,编译很简单,分别可以使用cmake或者make工具进行编译,这里不再多做介绍,当然,编译过程中缺少了某些库什么的,就先按照即可,更详细的步骤请看本文的参考文章链接。

开发步骤

  1. 写一个.thrift文件,也就是IDL(Interface Description File,接口描述文件);
  2. 用Thrift的IDL生成工具(windows下就是上面提供下载链接的thrift-0.9.1.exe, Linux下就是/usr/local/bin/thrift程序) ,然后根据需要生成目标语言代码;
  3. server端程序引入第2步生成的代码,实现RPC业务代码;
  4. client端程序引入第2步生成的代码,实现RPC调用逻辑;
  5. 用第4步生成的程序就可以调用第3步实现的远程服务了;

入门示例

下面就演示一个简单的server端和client端程序。

设计thrift文件(IDL)

假设实现这么一个简单服务,client通过hello接口发送自己的名字,且需要server端回复,比如 hello.thrift:

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

通过IDL工具生成源代码

执行thirift命令生成源文件:

thrift --gen cpp hello.thrift   			  # centos下
thrift-0.9.3.exe --gen cpp hello.thrift # Windows下

以上命令表示生成C++语言的源代码,然后会生成一个gen-cpp目录,里面包含自动生成的几个源代码文件:

hello_constants.cpp
hello_constants.h
HelloService.cpp
HelloService.h
HelloService_server.skeleton.cpp
hello_types.cpp
hello_types.h

实现server端程序

HelloService_server.skeleton.cpp就是默认的server端程序入口,可以直接修改该文件,或者拷贝一份再做修改(我是拷贝并重命名为server.cpp),以便增加自己的逻辑处理:

class HelloServiceHandler : virtual public HelloServiceIf {
public:
HelloServiceHandler() {
// Your initialization goes here
} void hello(const std::string& name) {
// Your implementation goes here
// 这里只简单打印出client传入的名称
printf("hello, I got your name %s\n", name.c_str());
}
};

如果是在linux平台下,直接通过g++编译:

g++ -o server hello_constants.cpp  HelloService.cpp hello_types.cpp  server.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

如果是在Windows平台下,通过vs2010新建win32控制台工程,将gen-cpp目录下的所有文件复制到新工程下,设置头文件包含和lib库目录。 比如设置libthrift.lib的头文件目录为thrift-0.9.3\lib\cpp\src\thrift,lib库目录为thrift-0.9.3\lib\cpp\Debug。

实现client端程序

因为没有默认的client实现,所以需要新建一个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(int argc, char** argv)
{
shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
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());
}
}

如果是在linux平台下,直接通过g++编译:

g++ -o client client.cpp hello_constants.cpp  HelloService.cpp hello_types.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

如果是在Windows平台下,通过vs2010新建win32控制台工程,将gen-cpp目录下的所有文件(除HelloService_server.skeleton.cpp之外)复制到新工程下,并增加上面手动实现的client.cpp。

通过以上步骤,就实现一个简单的RPC server和client程序了,可以分别运行进行测试看看效果怎么样。

Reference

Thrift官方安装手册(译)

Thrift在Windows及Linux平台下的安装和使用示例的更多相关文章

  1. Perl Tk在IC设计中的应用、Windows、Linux平台下的安装-各种错误的摸索解决

    本文转自:自己的微信公众号<集成电路设计及EDA教程> <Perl Tk在IC设计中的应用.Windows.Linux平台下的安装-各种错误的摸索解决> Perl在IC设计中有 ...

  2. Windows及Linux平台下的计时函数总结

    本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的各种函数.比如Window平台下特有的Windows API函数GetTickCount().timeG ...

  3. Windows 和 Linux 平台下的端口转发工具

    原文地址: http://unmi.cc/windows-linux-port-forwarding/ 这里记录一下我曾经使用过的几个端口转发工具,即端口映射.端口重定向,和 NAT 也是差不多的概念 ...

  4. Redis在Windows+linux平台下的安装配置(转)

    window平台Redis安装 下载地址: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload Redis文件夹有以下几个文 ...

  5. Windows和Linux环境下Memcached安装与配置(转)

    一.memcached安装配置 windows平台安装 1.memcached-1.2.6-win32-bin.zip下载地址: http://code.jellycan.com/memcached/ ...

  6. AES加密在windows与linux平台下显示结果不同,解决方案

    现象描述: 在 windows 操作系统下加解密正常,但部署到 linux 环境中相同的输入加密结果不正确,并且每次运行返回的结果都不同.也就是说在windows下加解密都正常,一但部署到linux下 ...

  7. windows和linux平台下的通用时间测试函数

    Time.cpp ////////////////////////////////////////////////////////////////////////////// // Timer.cpp ...

  8. socklen_t在windows和linux平台下的头文件定义

    windows平台下:头文件:#include<ws2tcpip.h> linux平台下:下面两个头文件都有定义:1)#include <sys/socket.h>2)#inc ...

  9. Arduino可穿戴教程Linux平台下安装Arduino IDE

    Arduino可穿戴教程Linux平台下安装Arduino IDE Linux平台下安装Arduino IDE Linux平台下的安装方式和Windows下的zip形式安装是类似的,只是Linux下的 ...

随机推荐

  1. Utopian Tree in Java

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during th ...

  2. c/c++ 输入输出缓冲区

    关于缓冲区的详细介绍,请参考 C++编程对缓冲区的理解 CPP的输入输出流和缓冲区 c++输出缓冲区刷新   (1)c++中cin.cout,cerr和c的stdin.stdout.stderr都是同 ...

  3. C++ 代码换行

    1.字符串太长,换行显示,怎么办?2.使用反斜杠,如下: string str = "abcd\ 1234"; 注意:反斜杠后面不准有任何字符.下一行开头的制表符不包含在整个字符串 ...

  4. js计时器 + asp 计时器

    JS: <script type="text/javascript"> ; function starts() { ) { alert('已经开启了实时监控!') re ...

  5. 那些一目了然的3D地质模型 【转】

    http://www.360doc.com/content/16/0830/09/14719766_586950902.shtml

  6. 使用 CountDownLatch 控制多个线程执行顺序

    已同步更新至:http://dxjia.cn/2015/08/countdownlatch-use/ 有时候会有这样的需求,多个线程同时工作,然后其中几个可以随意并发执行,但有一个线程需要等其他线程工 ...

  7. Activity intent经常使用的 FLAG

    Intent.FLAG_ACTIVITY_NEW_TASK 默认的跳转类型,会重新创建一个新的Activity,不过与这种情况,比方说Task1中有A,B,C三个Activity,此时在C中启动D的话 ...

  8. The Monty Hall Problem

    GNG1106 Lab 3The Monty Hall ProblemBackgroundThe Monty Hall Problem is a famous probability puzzle, ...

  9. 打包时Xcode报:此证书的签发者无效Missing iOS Distribution signing identity

    问题描述 今天准备打包上传AppStore,结果Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Deve ...

  10. JAVA本地远程连接linux程序监控状态

    环境:  1.本地window 2.程序部署在centos   一,启动访问权限安全守护程序 新建文件:jstatd.all.policy ,注意路径 grant codebase "$JA ...