简体   繁体   English

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

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

I wanted to submit the same data to multiple servers at the same time using gRPC. 我想使用gRPC同时将相同的数据提交到多个服务器。

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

From the example: In order to create 1 channel you can just do this: 从示例中:为了创建1个通道,您可以执行以下操作:

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

As this would create the stub for the channel: 因为这将为通道创建存根:

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

And I would be able to submit data using 我将能够使用提交数据

greeter.SayHello("hello world");  

But what if I want to submit data to 2 different servers using 2 different channels? 但是,如果我想使用2个不同的通道将数据提交到2个不同的服务器怎么办?

If I would just add another GreeterClient object called greeter2: 如果我只添加另一个称为greeter2的GreeterClient对象:

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

I get a segmentation fault when trying to submit the data to the second server: 尝试将数据提交到第二台服务器时出现分段错误:

greeter2.SayHello("hello world");  

What you are doing seems okay, I will need to see the stack trace to see what is going wrong. 您正在做的事情似乎还可以,我将需要查看堆栈跟踪信息以了解问题所在。

Consider the channel as an abstract data pipe to a service and if you want to talk to multiple servers, just create multiple channels and send rpcs on them separately. 将通道视为服务的抽象数据管道,如果要与多个服务器通信,只需创建多个通道并分别在它们上发送rpcs。

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

相关问题 考虑到遍历的路径每次都是相同的,如何在C ++中实现有向图而不使用邻接表? - How do I implement a directed graph in C++ without using an adjacency list, given that the path of traversal will be the same every time? 当我设置截止时间时,gRPC C++ 客户端阻塞 - gRPC C++ client blocking when I set a deadline time 如何在c ++中计算进程? - How do I time a process in c++? gRPC C ++,客户端:“ 14:连接失败” - gRPC C++, client: “14: Connect Failed” 如何在 VS - Code (Windows) 中同时编译和运行我的 C++ 代码 - How do I compile and run my c++ code at the same time in VS - Code (Windows) 在客户端上使用 gRPC C++,如何保持库单线程? - Using gRPC C++ on the Client, how can I keep the library single threaded? C++中如何同时比较多个变量? - How to Compare multiple variables at the same time in the C++? 如何在 Windows 上使用 Visual Studio 2017 连接到 C++ 中的 sqlite 数据库? - How do I connect to an sqlite database in C++ using visual studio 2017 on Windows? 如何避免使用不同类型的C ++执行相同操作的多个函数 - How can I avoid multiple functions which do the same thing with different types C++ 如何使用 C++ 套接字编程将数据并发发送到多个服务器? - How to concurrently send data to multiple servers using C++ socket programming?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM