Commit b75dbd03 authored by wangxuefeng's avatar wangxuefeng

Fix native server issue

parent 43ee7e1c
...@@ -17,7 +17,7 @@ before_script: ...@@ -17,7 +17,7 @@ before_script:
before_install: before_install:
- wget --no-clobber https://github.com/bazelbuild/bazel/releases/download/0.8.1/bazel_0.8.1-linux-x86_64.deb - wget --no-clobber https://github.com/bazelbuild/bazel/releases/download/0.8.1/bazel_0.8.1-linux-x86_64.deb
- sudo dpkg -i bazel_0.8.1-linux-x86_64.deb - sudo dpkg -i bazel_0.8.1-linux-x86_64.deb
- wget http://www.us.apache.org/dist/thrift/0.9.3/thrift-0.9.3.tar.gz && tar -xf thrift-0.9.3.tar.gz && cd thrift-0.9.3/ && ./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no && make -j 3 -s && sudo make install && cd - - wget http://www.us.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz && tar -xf thrift-0.11.0.tar.gz && cd thrift-0.11.0/ && ./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no && make CPPFLAGS=-DFORCE_BOOST_SMART_PTR -j 3 -s && sudo make install && cd -
install: install:
- sudo apt-get install -qq realpath libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev libgoogle-perftools-dev libboost-dev libssl-dev libevent-dev libboost-test-dev - sudo apt-get install -qq realpath libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev libgoogle-perftools-dev libboost-dev libssl-dev libevent-dev libboost-test-dev
......
...@@ -14,11 +14,11 @@ ...@@ -14,11 +14,11 @@
安装thrift依赖, ubuntu环境下 安装thrift依赖, ubuntu环境下
```bash ```bash
wget http://www.us.apache.org/dist/thrift/0.9.3/thrift-0.9.3.tar.gz wget http://www.us.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz
tar -xf thrift-0.9.3.tar.gz tar -xf thrift-0.11.0.tar.gz
cd thrift-0.9.3/ cd thrift-0.11.0/
./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no ./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no
make -j 3 -s make CPPFLAGS=-DFORCE_BOOST_SMART_PTR -j 3 -s
sudo make install sudo make install
``` ```
配置brpc支持thrift协议 配置brpc支持thrift协议
...@@ -69,12 +69,12 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti ...@@ -69,12 +69,12 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti
} }
... ...
``` ```
构造thrift请求, 并发送, ThriftTemplateMessage是模板类, 里面托管了thrift原生消息, 通过raw()方法可以可以直接操作原生thrift消息 构造thrift请求, 并发送, ThriftMessage是模板类, 里面托管了thrift原生消息, 通过raw()方法可以可以直接操作原生thrift消息
```c++ ```c++
// wrapper thrift raw request into ThriftMessage // wrapper thrift raw request into ThriftMessage
// example::[EchoRequest/EchoResponse]是thrfit原生定义的消息(通过thrift代码生成工具生成) // example::[EchoRequest/EchoResponse]是thrfit原生定义的消息(通过thrift代码生成工具生成)
brpc::ThriftTemplateMessage<example::EchoRequest> req; brpc::ThriftMessage<example::EchoRequest> req;
brpc::ThriftTemplateMessage<example::EchoResponse> res; brpc::ThriftMessage<example::EchoResponse> res;
req.raw().data = "hello"; req.raw().data = "hello";
...@@ -112,8 +112,8 @@ public: ...@@ -112,8 +112,8 @@ public:
return; return;
} }
example::EchoRequest* req = request->cast<example::EchoRequest>(); example::EchoRequest* req = request->Cast<example::EchoRequest>();
example::EchoResponse* res = response->cast<example::EchoResponse>(); example::EchoResponse* res = response->Cast<example::EchoResponse>();
// process with req and res // process with req and res
res->data = req->data + "user data"; res->data = req->data + "user data";
......
...@@ -13,11 +13,11 @@ In order to not depend on and compile with thrift library for most of the users, ...@@ -13,11 +13,11 @@ In order to not depend on and compile with thrift library for most of the users,
Install Thrift in Ubuntu Install Thrift in Ubuntu
```bash ```bash
wget http://www.us.apache.org/dist/thrift/0.9.3/thrift-0.9.3.tar.gz wget http://www.us.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz
tar -xf thrift-0.9.3.tar.gz tar -xf thrift-0.11.0.tar.gz
cd thrift-0.9.3/ cd thrift-0.11.0/
./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no ./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no
make -j 3 -s make CPPFLAGS=-DFORCE_BOOST_SMART_PTR -j 3 -s
sudo make install sudo make install
``` ```
Configure with thrift support Configure with thrift support
...@@ -68,13 +68,13 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti ...@@ -68,13 +68,13 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti
} }
... ...
``` ```
construct thrift request and send to server, ThriftTemplateMessage is a template class, the native thrift message was delegated by and can be accessed directly by raw() method. construct thrift request and send to server, ThriftMessage is a template class, the native thrift message was delegated by and can be accessed directly by raw() method.
```c++ ```c++
// wrapper thrift raw request into ThriftMessage // wrapper thrift raw request into ThriftMessage
// example::[EchoRequest/EchoResponse]is thrift native message generated by thrift toolkits // example::[EchoRequest/EchoResponse]is thrift native message generated by thrift toolkits
brpc::ThriftTemplateMessage<example::EchoRequest> req; brpc::ThriftMessage<example::EchoRequest> req;
brpc::ThriftTemplateMessage<example::EchoResponse> res; brpc::ThriftMessage<example::EchoResponse> res;
req.raw().data = "hello"; req.raw().data = "hello";
...@@ -111,8 +111,8 @@ public: ...@@ -111,8 +111,8 @@ public:
return; return;
} }
example::EchoRequest* req = request->cast<example::EchoRequest>(); example::EchoRequest* req = request->Cast<example::EchoRequest>();
example::EchoResponse* res = response->cast<example::EchoResponse>(); example::EchoResponse* res = response->Cast<example::EchoResponse>();
// process with req and res // process with req and res
res->data = req->data + "user data"; res->data = req->data + "user data";
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <butil/logging.h> #include <butil/logging.h>
#include "gen-cpp/EchoService.h" #include "gen-cpp/EchoService.h"
#include <thrift/protocol/TBinaryProtocol.h> #include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h> #include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h> #include <thrift/transport/TServerSocket.h>
...@@ -27,9 +28,11 @@ ...@@ -27,9 +28,11 @@
#include <thrift/concurrency/PosixThreadFactory.h> #include <thrift/concurrency/PosixThreadFactory.h>
// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0 // _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
#include <thrift/TProcessor.h> // to include stdcxx.h if present
#ifndef THRIFT_STDCXX #ifndef THRIFT_STDCXX
#if defined(_THRIFT_STDCXX_H_) #if defined(_THRIFT_STDCXX_H_)
# define THRIFT_STDCXX apache::thrift::stdcxx # define THRIFT_STDCXX apache::thrift::stdcxx
#include <thrift/transport/TNonblockingServerSocket.h>
#else #else
# define THRIFT_STDCXX boost # define THRIFT_STDCXX boost
#endif #endif
...@@ -67,14 +70,23 @@ int main(int argc, char *argv[]) { ...@@ -67,14 +70,23 @@ int main(int argc, char *argv[]) {
new apache::thrift::transport::TBufferedTransportFactory()); new apache::thrift::transport::TBufferedTransportFactory());
THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::ThreadManager> thread_mgr( THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::ThreadManager> thread_mgr(
apache::thrift::concurrency::ThreadManager::newSimpleThreadManager(2)); apache::thrift::concurrency::ThreadManager::newSimpleThreadManager(2));
thread_mgr->threadFactory(thread_factory); thread_mgr->threadFactory(thread_factory);
thread_mgr->start(); thread_mgr->start();
#if defined(_THRIFT_STDCXX_H_)
THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TNonblockingServerSocket> server_transport =
THRIFT_STDCXX::make_shared<apache::thrift::transport::TNonblockingServerSocket>(FLAGS_port);
apache::thrift::server::TNonblockingServer server(processor, apache::thrift::server::TNonblockingServer server(processor,
transport_factory, transport_factory, protocol_factory, transport_factory, transport_factory, protocol_factory,
protocol_factory, FLAGS_port, thread_mgr); protocol_factory, server_transport);
#else
apache::thrift::server::TNonblockingServer server(processor,
transport_factory, transport_factory, protocol_factory,
protocol_factory, FLAGS_port);
#endif
server.serve(); server.serve();
return 0; return 0;
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
# define THRIFT_STDCXX apache::thrift::stdcxx # define THRIFT_STDCXX apache::thrift::stdcxx
#else #else
# define THRIFT_STDCXX boost # define THRIFT_STDCXX boost
#include <boost/make_shared.hpp>
#endif #endif
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment