Thrift C++ Server demo 实现

参考文档:
http://blog.163.com/zhangjie_0303/blog/static/9908270620140311022650/

Thrift C++ Server demo 实现

安装 thrift

cd /home/yuzx/data/download
tar -xf thrift-0.9.3.tar.gz
cd thrift-0.9.3
./configure --with-boost=/usr/local \
  --with-qt4=no \
  --with-qt5=no \
  --with-nodejs=no \
  --with-d=no \
  --with-haxe=no \
  --with-haskell=no \
  --with-ruby=no \
  --with-php=no \
  --with-php_extension=no \
  --with-perl=no \
  --with-lua=no \
  --with-erlang=no \
  --with-csharp=no \
  --with-c_glib=no
make
sudo make install

# 单独安装 python-thrift
cd lib/py
sudo python setup.py install --root=/ --prefix=/usr/local
thrift -version

thrift IDL

// 定义数据格式
struct UserProfile {
  1:i32 id, // 注意这里是逗号,而不是分号
  2:string name,
  3:string blurb
} // 这里没有分号

service UserStorage{
  void store(1: UserProfile user), // 注意这里是逗号,而不是分号
  UserProfile getUser(1: i32 uid)
}

生成 c++ 代码

thrift -r --gen cpp demo.thrift

会在当前目录中生成 gen-cpp,修改 UserStorage_server.skeleton.cpp,重命名为 demo_server.cpp

在 thrift 的源码中找到:
thrift-0.9.3/tutorial/cpp/CppClient.cpp => demo_client.cpp
thrift-0.9.3/tutorial/cpp/CMakeLists.txt
thrift-0.9.3/build/cmake/ThriftMacros.cmake => ThriftMacros

修改后的 CMakeLists.txt


cmake_minimum_required(VERSION 3.2)

find_package(Boost 1.53.0 REQUIRED)
include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")

# Make sure gen-cpp files can be included
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
include_directories("${CMAKE_CURRENT_BINARY_DIR}/gen-cpp")
include_directories("${PROJECT_SOURCE_DIR}/lib/cpp/src")

# include(ThriftMacros)

# 源码集合
set(demo_SOURCES 
  gen-cpp/demo_server.cpp
  gen-cpp/UserStorage.cpp
  gen-cpp/demo_types.cpp
  gen-cpp/demo_constants.cpp
  )

# 生成静态库目标
add_library(demo STATIC ${demo_SOURCES})
target_link_libraries(demo thrift)

# 同下
add_executable(demo_server gen-cpp/demo_server.cpp)
target_link_libraries(demo_server demo)
target_link_libraries(demo_server thrift)

# 生成 demo_client 可执行程序,要求链接 demo 静态库, thrift XX库
add_executable(demo_client demo_client.cpp)
target_link_libraries(demo_client demo)
target_link_libraries(demo_client thrift)

demo_server.cpp

#include "UserStorage.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

#include <map>

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");

    log[user.id] = user;
  }

  void getUser(UserProfile& _return, const int32_t uid) {
    // Your implementation goes here
    printf("getUser\n");

    _return = log[uid];
  }

 protected:
  map<int32_t, UserProfile> log;

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<UserStorageHandler> handler(new UserStorageHandler());
  shared_ptr<TProcessor> processor(new UserStorageProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}

server 的代码大部分都是生成好的,只加入一点点 ^_^

demo_client.cpp

#include <iostream>

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>

#include "UserStorage.h"

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

int main() {
  boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
  boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
  boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  UserStorageClient client(protocol);

  try {
    transport->open();

    UserProfile user;
    user.id = 1;
    user.name = "oneUser";
    user.blurb = "hix";

    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) {
    cout << "ERROR: " << tx.what() << endl;
  }
}

编译执行

cmake .
make
./server_server
./client_server
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值