[英]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.