您的位置:首页 > 编程语言 > C语言/C++

Thrift C++ Server demo 实现

2015-12-04 20:02 567 查看
参考文档:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++-thrift