下面我们通过一个具体例子来呈现
example.proto
IDL描述文件protoc example.proto --cpp_out=./ --proto_path=./
获得example.pb.h
和example.pb.cpp
两个文件srpc_generator protobuf ./example.proto ./
获得example.srpc.h
文件Example::Service
来实现具体的rpc业务逻辑,这就是一个RPC Service注意这个Service没有任何网络、端口、通信协议等概念,仅仅负责完成实现从EchoRequest
输入到输出EchoResponse
的业务逻辑
class ExampleServiceImpl : public Example::Service
{
public:
void Echo(EchoRequest *request, EchoResponse *response, RPCContext *ctx) override
{
response->set_message("Hi, " + request->name());
printf("get_req:\n%s\nset_resp:\n%s\n",
request->DebugString().c_str(),
response->DebugString().c_str());
}
};