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

标签 c++ grpc

我想使用 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/

相关文章:

c++ - 哪些标准 C++ 特性可用于查询机器/操作系统架构?

go - gRPC 连接问题 : How to figure out if it is the server or the client?

c++ - while true 循环函数

java - Java 中的 gRPC - 阻塞/非阻塞 stub

谷歌云顶点 AI 与 Golang : rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 (Not Found)

swift - 防止同时执行任务

c# - gRPC:我应该为整个应用程序使用一个客户端吗?

C++14:返回 {} 时元组中的默认值

c++ - 如何让 Doxygen 显示菱形继承图

c++ - <functional>中有功能组合的东西吗?