问题
I am trying to play around Google Pub/Sub and I need to integrate it in C++ code-base.
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
.
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?
回答1:
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);
}
}
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.
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".
}
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.
回答2:
There is c++ library called pubsuber
for accessing google pubsub.
For more information look here.
来源:https://stackoverflow.com/questions/56260017/consumer-example-for-google-pub-sub-in-c