C ++中Google Pub/Sub的消费者示例 [英] Consumer example for Google Pub/Sub in C++

查看:194
本文介绍了C ++中Google Pub/Sub的消费者示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Pub/Sub,并且需要将其集成到C ++代码库中.

I am trying to play around Google Pub/Sub and I need to integrate it in C++ code-base.

由于C ++中没有对Google Pub/Sub的本机支持,因此我通过gRPC使用它.因此,我已经通过protoc生成了相应的pubsub.grpc.pb.hpubsub.grpc.pb.ccpubsub.pb.hpubsub.pb.cc文件.

As there is no native support for Google Pub/Sub in C++, I am using it through gRPC. Thus, I have generated corresponding pubsub.grpc.pb.h, pubsub.grpc.pb.cc, pubsub.pb.h and pubsub.pb.cc files via protoc.

问题部分:由于缺少文档,因此使用C ++示例将非常有帮助.我找到了发布商部分的示例,但是不适用于订户部分.我试图深入研究其他语言生成的代码和示例,但是出现了很多问题.订户部分有什么例子吗?还是有人已经有这种经验?

Question part: because of lack of documentation it would be very helpful to have an example in C++. I have found an example for publisher part, but not for the subscriber part. I tried to dive into the generated code and examples in other languages, but quite many question arise. Is there any example for the subscriber part? Or may be someone already had such kind of experience?

推荐答案

就像在进行发布请求一样,您可以进行

Just like you are making Publish requests, you can make StreamingPull requests for messages. Note that this is a simple proof of concept, and, in practice, you’d probably want to make this code more robust; e.g. create multiple streams, have the message processing happen on a thread pool, implement some kind of flow control, etc…

#include <iostream>
#include <memory>

#include <grpc++/grpc++.h>

#include "google/pubsub/v1/pubsub.grpc.pb.h"

auto main() -> int {
    using grpc::ClientContext;
    using grpc::ClientReaderWriter;
    using google::pubsub::v1::Subscriber;
    using google::pubsub::v1::StreamingPullRequest;
    using google::pubsub::v1::StreamingPullResponse;

    auto creds = grpc::GoogleDefaultCredentials();
    auto stub = std::make_unique<Subscriber::Stub>(
        grpc::CreateChannel("pubsub.googleapis.com", creds));

    // Open up the stream.
    ClientContext context;
    std::unique_ptr<ClientReaderWriter<
        StreamingPullRequest, StreamingPullResponse>> stream(
            stub->StreamingPull(&context));

    // Send initial message.
    StreamingPullRequest request;
    request.set_subscription(
        "projects/pubsub-cpp-api-1504713535863/subscriptions/testing");
    request.set_stream_ack_deadline_seconds(10);
    stream->Write(request);

    // Receive messages.
    StreamingPullResponse response;
    while (stream->Read(&response)) {
      // Ack messages.
      StreamingPullRequest ack_request;
      for (const auto &message : response.received_messages()) {
        ack_request.add_ack_ids(message.ack_id());
      }
      stream->Write(ack_request);
    }
}

这是最新的Cloud Pub/Sub API,也是当前推荐的从服务中提取消息的方式;对于期望高吞吐量和低延迟的用户而言尤其如此.当前,尚无适用于C ++的客户端库,但是有一个打开的问题在GitHub上.其他语言(例如Java)的现有客户端库已经使用此API,因此您可以在自己的C ++代码中复制其功能.

This is the newest Cloud Pub/Sub API, and is the currently recommended way of pulling messages from the service; this is especially true for users who expect high throughput and low latency. Currently, there is no existing client library for C++, but there is an open issue on GitHub for it. The existing client libraries for other languages (e.g. Java) already use this API, so you may be able to replicate their functionality in your own C++ code.

对于更简单的用例,您还可以使用较旧的 Pull API,这使得许多独立的消息请求.请注意,为了获得高吞吐量和低延迟,您很可能应该制作许多同时进行的异步RPC:请参阅gRPC

For simpler use-cases, you could also use the older Pull API, which makes many independent requests for messages. Note that, for high throughput and low latency, you should most likely be making many simultaneous asynchronous RPCs: see gRPC documentation.

#include <iostream>
#include <memory>

#include <grpc++/grpc++.h>

#include "google/pubsub/v1/pubsub.grpc.pb.h"

auto main() -> int {
    using grpc::ClientContext;
    using google::pubsub::v1::Subscriber;
    using google::pubsub::v1::PullRequest;
    using google::pubsub::v1::PullResponse;

    auto creds = grpc::GoogleDefaultCredentials();
    auto stub = std::make_unique<Subscriber::Stub>(
        grpc::CreateChannel("pubsub.googleapis.com", creds));

    PullRequest request;
    request.set_subscription(
        "projects/pubsub-cpp-api-1504713535863/subscriptions/testing");
    request.set_max_messages(50);
    request.set_return_immediately(false);

    PullResponse response;
    ClientContext ctx;

    auto status = stub->Pull(&ctx, request, &response);
    if (!status.ok()) {
        // ...
    }

    // Do something with "response".
}

作为最后的选择,您可以使用 Push 订阅,要求您在客户端上实现HTTP终结点.但是,通常不建议这样做,除非您从多个订阅中散布,或者在客户端无法发出传出请求的情况下.

As a last resort, you could use a Push subscription, which would only require you to implement an HTTP endpoint on your client. However, this is not usually recommended unless you are fanning in from multiple subscriptions, or for cases where your client cannot make outgoing requests.

这篇关于C ++中Google Pub/Sub的消费者示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆