Thrift C++ Server demo 实现
2015-12-04 20:02
567 查看
参考文档:
http://blog.163.com/zhangjie_0303/blog/static/9908270620140311022650/
Thrift C++ Server demo 实现
生成 c++ 代码
会在当前目录中生成 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
demo_server.cpp
server 的代码大部分都是生成好的,只加入一点点 ^_^
demo_client.cpp
编译执行
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语言的隐式(自动)数据类型转换
- c++11,for,for each,std::for_each的应用
- c语言中gets ,getschar 和fgets 的用法及三者之间的差别
- c++ 中的观察者模式
- 【转载】c++之类的基本操作(c++ primer 的读书笔记 ,类对象, 类用户, 类成员的含义)
- VS开发】C中调用C++文件中定义的function函数
- 学习笔记——关于指向常量指针的解引用的发现
- C++直接初始化和复制初始化2
- C++直接初始化和复制初始化1
- C语言之字符串
- 【C语言提高24】二级指针做输入的第一种内存模型:数组指针
- 解决iOS报错libc++abi.dylib handler threw exception
- 【C语言提高23】二级指针做输出的内存模型
- c语言中static用法总结
- VS2008 C++ 调用托管C++dll 当前不会命中断点,没有与此关联的代码
- C++ (Bug)--调 C文件,报错 xxxx.0 not fountt, x86
- C++ Timer定时器
- Vector c++ 的基本操作 矢量 *_* !
- printf 格式化输出符号详细说明
- C++终极复习篇(五)----STL标准模板库