C++:是否可以在单个原型文件中添加多个 gRPC 服务定义

问题描述 投票:0回答:0

如果multiple服务在1原型文件中定义,我试图了解gRPC服务是如何创建的以及服务器是如何链接到它们的。 (请原谅长消息)

使用 this 文档中的示例,我创建了基本服务器(在 1 个原型文件中定义了 1 个服务)并且能够运行客户端应用程序。

中的服务定义

route_services.proto

中的示例服务器代码片段

service RouteGuide {
  rpc GetFeature(Point) returns (Feature) {} 
}

message Feature {
  // The name of the feature.
  string name = 1;

  // The point where the feature is detected.
  Point location = 2;
}

但是,我想知道是否有可能在 1 个原型文件中有 超过 1 个服务,并像下面这样在服务器中实现它的接口。

route_guide_server.cc

中更新服务定义
(注意:RouteGuidanceV2服务和NewFeature消息)

class RouteGuideImpl : public RouteGuide::Service {
...
// implement the service GetFeature
...
}

Update Server Code Snippet

route_services.proto
(注意:这里继承了RouteGuideV2类)

service RouteGuide {
  rpc GetFeature(Point) returns (Feature) {} 
}

message Feature {
  // The name of the feature.
  string name = 1;

  // The point where the feature is detected.
  Point location = 2;
}

/////////////////// Newly Added Service ///////////////////
service RouteGuideV2 {
  rpc GetFeature(Point) returns (NewFeature) {} 
}

message NewFeature {

  // The point where the feature is detected.
  Point location = 1;
}

但是,我看到了这个错误:

route_guide_server.cc
。 它发生在编译服务器文件时(特别是在我调用
class RouteGuideImpl : public RouteGuide::Service, public RouteGuideV2::Service {
...
// implement the service GetFeature (coming from RouteGuide interface)
// implement V2 service GetFeature (coming from RouteGuideV2 interface)
...
}
注册我的课程
‘grpc::Service’ is an ambiguous base
的那一行)

builder->RegisterService(this)
编译器的角度来看,更新的原型文件也是有效的,它可以毫无问题地生成服务器和客户端代码。

假设在 1 个 proto 文件中定义 1 个服务似乎是一种理想的方式并且工作得很好,我想就此主题提出几个问题以了解 gRPC 用户的常见做法。

  1. 是否不能将多个 gRPC 服务类派生为单个类来实现它的服务方法?这可能不是最好的方法,但我的意思是从技术上讲,应该可以覆盖和实现来自 2 个不同服务接口(每个 1 个)的 gRPC 虚拟方法。

  2. 如果 RouteGuideV2 服务

    RouteGuideImpl
    在功能上与响应消息(Feature --> NewFeature)中的微小变化相同,那么在同一个原型文件
    protoc
    而不是单独的
    GetFeature
    文件中定义它是否有意义?

  3. 如果定义多个服务不正确,那么

    route_guide.proto
    添加类似服务的方法是什么?? (或者服务器应该如何定义和运行服务是否有任何限制)

感谢阅读,等待回复...!!

c++ server client protocol-buffers grpc-c++
© www.soinside.com 2019 - 2024. All rights reserved.