What's BRPC: A RPC framework from BAIDU in c-plus-plus.
Why try: For the sake of BRPC being very Huo these days.
Which tools: Visual Studio Community 2017(the best IDE in the universe)
、Ubuntu 14
Following the QUICK START from BRPC project:
For Ubuntu only as below:
$sudo apt-get install git g++ make libssl-dev
$sudo apt-get install libgflags-dev libprotobuf-dev libprotoc-dev protobuf-compiler libleveldb-dev
$sudo apt-get install libsnappy-dev
$git clone https://github.com/brpc/brpc.git
$cd brpc
$sh config_brpc.sh --headers=/usr/include --libs=/usr/lib
$make
$tar -zcvf brpc.tar.gz output/*
- Download and unzip
brpc.tar.gz/output/include/*
to Windows include directory. In my machine the default including path isC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\Linux\include\usr\include
. - Copy the LIBS
brpc/output/lib/*
to/usr/lib
- Copy
/brpc/output/include/*
to/usr/include
option cc_generic_services = true;//java_generic_service for Java and py_generic_service for Python
message EchoRequest {
required string message = 1;
};
message EchoResponse {
required string message = 1;
};
service EchoService {
rpc Echo(EchoRequest) returns (EchoResponse);
};
more usage of protobuf。
protoc command
[usage] protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR path/to/file.proto
IMPORT_PATH
specifies a directory in which to look for .proto
files when resolving import directives, use -I
in short.
upload proto file path_to_your_project/proto/echo.proto
(Windows) to ~/brpc/*
(Ubuntu).
cd ~/brpc/
mkdir proto_gen
cd proto
protoc --cpp_out=../proto_gen echo.proto
cd ~/brpc/proto_gen
output: echo.pb.cc
、echo.pb.h
download directory proto_gen/*
from Ubuntu to Windows project dir.
include ../proto_gen/echo.pb.h
file in the project then implement EchoService:
pack the protobuf header files.
cd /usr/include
sudo tar -zcvf google.protobuf.tar.gz google/*
sudo mv google.protobuf.tar.gz ~/
cd ~/
sudo chown ubuntu:ubuntu google.protobuf.tar.gz
download to Windows, unzip and copy to including directory
.
...
same way to pack /usr/lib/gflags/*
as gflags.include.tar.gz
and copy to Windows including directory
.
include the generated file echo.pb.h
and **echo.pb.cc**
.
Add head file EchoServiceImpl.h
implement EchoService.
#include "echo.pb.h"
...
class EchoServiceImpl :public EchoService
{
public:
EchoServiceImpl() {};
virtual ~EchoServiceImpl() {};
void virtual Echo(::google::protobuf::RpcController* cntl_base,
const ::EchoRequest* request,
::EchoResponse* response,
::google::protobuf::Closure* done);
};
Implements the functions according to the guide of BRPC/实现生成的service接口
- Add a macro
GFLAGS_NS
#ifndef GFLAGS_NS
#define GFLAGS_NS GFLAGS_NAMESPACE
#endif // !GFLAGS_NS
-
Include all the files generated by protoc into the project.
-
Simple doc(CN)
-
Generating the project(remoting debug machine configured) will compile the project and put the
*.out
to the remote path~/projects/personal_service
. -
Run.
$cd ~/projects/personal_service/bin/x64/Debug
$./personal_service.out
- Test Every Protobuf Service is HTTP Enabled, so we can test it by CURL.
$ curl -d '{"message":"hello"}' http://{your_ubuntu_debug_machine_ip}:8000/EchoService/Echo
Implement more complex service, including:
-
GPSService:Read/Write GPS locations for my app.
-
BlogService:Load index/post pages from the service through port 80.