我想使用 gRPC 同时向多个服务器提交相同的数据。
我查看了 greeterAsyn2 c++ 示例: https://github.com/grpc/grpc/blob/v1.8.x/examples/cpp/helloworld/greeter_async_client2.cc
从示例中:为了创建 1 个 channel ,您可以这样做:
GreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
因为这将为 channel 创建 stub :
class GreeterClient {
public:
explicit GreeterClient(std::shared_ptr<Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
}
我可以使用
提交数据greeter.SayHello("hello world");
但是如果我想使用 2 个不同的 channel 将数据提交到 2 个不同的服务器怎么办?
如果我只添加另一个名为 greeter2 的 GreeterClient 对象:
GreeterClient greeter2(grpc::CreateChannel(
"10.0.0.3:9008", grpc::InsecureChannelCredentials()));
我在尝试将数据提交到第二台服务器时遇到段错误:
greeter2.SayHello("hello world");
最佳答案
你所做的似乎没问题,我需要查看堆栈跟踪以了解哪里出了问题。
将 channel 视为服务的抽象数据管道,如果您想与多个服务器通信,只需创建多个 channel 并分别在它们上发送 rpc。
关于c++ - 如何在 C++ 中使用 gRPC 同时连接到多个服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48290319/