參考文檔:
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