谷歌Protobuf编译器不为服务标签生成类?

google protobuf compiler doesn't generate class for service tag?

本文关键字:标签 服务 Protobuf 编译器 谷歌      更新时间:2023-10-16

我正在尝试使用 protobuf 使用 RpcChannel 和 RpcController 生成服务。我参考了谷歌protobuf的语言指南,并且:

我有这样的示例原型文件:

语法 = "proto2";

message SearchRequest
{
    required string Request = 1;
}
message SearchResponse
{
    required string Response = 2;
}
service SearchService {
    rpc Search (SearchRequest) returns (SearchResponse);
}

然后我用:

protoc --cpp_out=./ examples.proto

我有 .h 和 .cc 文件。但是当我搜索生成的代码时,我只找到了"请求"和"响应"的类,但没有找到"SearchService"的类:

examples.pb.h:class SearchRequest;
examples.pb.h:class SearchResponse;
examples.pb.h:class SearchRequest : public ::google::protobuf::Message {
examples.pb.h:  // @@protoc_insertion_point(class_scope:SearchRequest)
examples.pb.h:class SearchResponse : public ::google::protobuf::Message {
examples.pb.h:  // @@protoc_insertion_point(class_scope:SearchResponse)

语言指南网页提供了一个示例(https://developers.google.com/protocol-buffers/docs/proto#services),它需要使用"SearchService"类:但在生成的代码中,没有搜索服务。该指南没有提供RpcChannel/RpcController用法的完整示例。

那么我该如何修复该示例以使其正常工作呢?我搜索了谷歌,但没有找到任何好的cpp示例,该示例提供了RpcChannel/RpcController如何工作的完整示例。任何提示或链接?

谢谢!

protobuf本身不提供RPC实现;你应该使用插件接口来创建自己的,或者使用grpc。

例如,grpc 使用它grpc_cpp_plugin插件。

$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto

https://github.com/grpc/grpc/blob/master/examples/cpp/cpptutorial.md