繁体   English   中英

如何在C ++中使用gRPC同时连接到多个服务器?

[英]How do I connect to multiple servers at the same time using gRPC in C++?

我想使用gRPC同时将相同的数据提交到多个服务器。

我看了看greeterAsyn2 c ++示例: https : //github.com/grpc/grpc/blob/v1.8.x/examples/cpp/helloworld/greeter_async_client2.cc

从示例中:为了创建1个通道,您可以执行以下操作:

GreeterClient greeter(grpc::CreateChannel(
            "localhost:50051", grpc::InsecureChannelCredentials()));

因为这将为通道创建存根:

class GreeterClient {
  public:
    explicit GreeterClient(std::shared_ptr<Channel> channel)
            : stub_(Greeter::NewStub(channel)) {}
}

我将能够使用提交数据

greeter.SayHello("hello world");  

但是,如果我想使用2个不同的通道将数据提交到2个不同的服务器怎么办?

如果我只添加另一个称为greeter2的GreeterClient对象:

GreeterClient greeter2(grpc::CreateChannel(
            "10.0.0.3:9008", grpc::InsecureChannelCredentials()));

尝试将数据提交到第二台服务器时出现分段错误:

greeter2.SayHello("hello world");  

您正在做的事情似乎还可以,我将需要查看堆栈跟踪信息以了解问题所在。

将通道视为服务的抽象数据管道,如果要与多个服务器通信,只需创建多个通道并分别在它们上发送rpcs。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM