Chinaunix首页 | 论坛 | 博客
  • 博客访问: 21047
  • 博文数量: 5
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-27 10:46
文章分类

全部博文(5)

文章存档

2011年(4)

2010年(1)

我的朋友

分类: LINUX

2011-04-27 15:37:49

本文仅介绍Linux平台上的入门,语言采用C++
 
一、安装
1、从官网下载thrift安装包,目前最新版本是0.5.0,链接如下:
下载完成后解压,进入thrift-0.5.0 目录,阅读其README文档,里面有安装方法,基本上就是configure & make & make install 一路下来。
笔者在安装过程中遇到一个小插曲:
configure的时候提示找不到php-config,可能跟我们系统上php安装不在标准目录下有关系吧
解决方法如下:
在configure的时候加上--with-php-config=path(php-config 的目录,在我的系统上是--with-php-config=/usr/local/php5/bin/php-config)
 
安装完成后先查看thrift-0.5.0/tutorial/README文件。
 
二、示例
编辑demo.thrift文件,内容如下:
struct UserProfile{
        1:i32 id, //注意这里是逗号,而不是分号
        2:string name,
        3:string blurb
} //这里没有分号
 
service UserStorage{
        void store(1: UserProfile user), //注意这里是逗号,而不是分号
        UserProfile getUser(1: i32 uid)
}
 
运行如下命令:
bash$thrift -r --gen cpp demo.thrift
 
可以看到在当前目录下产生了一个gen-cpp的目录,该目录下即以上命令产生的文件:
UserStorage.cpp
UserStorage.h
UserStorage_server_skeleton.cpp
demo_constants.cpp
demo_constants.h
demo_types.cpp
demo_types.h
 
注意:在以上文件中,只有UserStorage_server_skeleton.cpp是跟业务相关的,是可以修改的,其余文件都是框架相关的。
 
UserStorage_server_skeleton.cpp文件内容如下:
 
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
 
#include "UserStorage.h"
#include
#include
#include
#include
#include
 
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
 
using boost::shared_ptr;
 
class UserStorageHandler : virtual public UserStorageIf {
 public:
  UserStorageHandler() {
    // Your initialization goes here
  }
 
  void store(const UserProfile& user) {
    // Your implementation goes here
    printf("store\n");
  }
 
  void getUser(UserProfile& _return, const int32_t uid) {
    // Your implementation goes here
    printf("getUser\n");
  }
};
 
int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr handler(new UserStorageHandler());
  shared_ptr processor(new UserStorageProcessor(handler));
  shared_ptr serverTransport(new TServerSocket(port));
  shared_ptr transportFactory(new TBufferedTransportFactory());
  shared_ptr protocolFactory(new TBinaryProtocolFactory());
 
  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}
 
可以看到,该文件只是一个框架,用户可以根据需要扩展该文件,笔者修改如下(蓝色部分为添加的代码,同时将文件改名为UserStorage_server.cpp):
 
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "UserStorage.h"
#include 
#include 
#include 
#include 
#include 
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
class UserStorageHandler : virtual public UserStorageIf {
 public:
  UserStorageHandler() {
    // Your initialization goes here
  }
  void store(const UserProfile& user) {
    // Your implementation goes here
        log[user.id] = user; //实际的保存操作
    printf("store\n");
  }
  void getUser(UserProfile& _return, const int32_t uid) {
    // Your implementation goes here
        _return = log[uid]; //实际获取操作,注意:在实际生产中,这里还需要异常处理
    printf("getUser\n");
  }
  //增加成员变量,用户保存用户数据,为了简单起见,这里只将数据保存在内存,当然可以可以保存在数据库、文件等等,主要注意,如果保存在其他介质的话
  //在初始化的时候记得加载进内存或者打开访问句柄
  protected:
        map log;
};
int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr handler(new UserStorageHandler());
  shared_ptr processor(new UserStorageProcessor(handler));
  shared_ptr serverTransport(new TServerSocket(port));
  shared_ptr transportFactory(new TBufferedTransportFactory());
  shared_ptr protocolFactory(new TBinaryProtocolFactory());
  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}
 
可以看到以上程序还包括一个简单的服务端代码,问了进行测试,笔者从thrift/tutorial/cpp/目录下copy了一个客户端代码和Makefile
修改如下:
CppClient.cpp (蓝色部分为客户化代码) 
 
#include 
#include 
#include 
#include 
#include 
#include 
#include "UserStorage.h"
using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace boost;
int main(int argc, char** argv) {
  shared_ptr socket(new TSocket("localhost", 9090));
  shared_ptr transport(new TBufferedTransport(socket));
  shared_ptr protocol(new TBinaryProtocol(transport));
  UserStorageClient client(protocol);
  try {
    transport->open();
        UserProfile user;
        user.id = 1;
        user.name = "liqb";
        user.blurb = "aaaaaa";
        client.store(user);
        UserProfile user2;
        client.getUser(user2, 1);
        printf("user.id = %d user.name = %s user.blurb = %s\n", user2.id, user2.name.c_str(), user2.blurb.c_str());
    transport->close();
  } catch (TException &tx) {
    printf("ERROR: %s\n", tx.what());
  }
}
 
Makefile
 
BOOST_DIR = /usr/local/boost/include/boost-1_33_1/
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = UserStorage.cpp demo_constants.cpp demo_types.cpp
default: server client
server: UserStorage_server.cpp
        g++ -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR}  -I../gen-cpp -L${LIB_DIR} -lthrift UserStorage_server.cpp ${GEN_SRC}
client: CppClient.cpp
        g++ -o CppClient -I${THRIFT_DIR} -I${BOOST_DIR}  -I../gen-cpp -L${LIB_DIR} -lthrift CppClient.cpp ${GEN_SRC}
clean:
        $(RM) -r CppClient CppServer
 
编译之后产生CppServer 和CppClient 两个可执行程序,分别运行CppServer 和CppClient,即可以看到测试结果。
阅读(4014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~