

在本文档中,您将了解如何使用 Google Cloud Pub/Sub 向主题发送消息以及从订阅中接收消息。
先决条件
- Google Cloud 帐号
- 已启用 Pub/Sub API 的 Google Cloud 项目
创建主题和订阅
- 访问 Cloud Pub/Sub 控制台 。
- 单击 创建主题 按钮。
- 输入主题名称,然后单击 创建 按钮。
- 单击 创建订阅 按钮。
- 输入订阅名称,然后单击 创建 按钮。
发布消息
您可以使用 Google Cloud Pub/Sub 客户端库或通过 HTTP API 来发布消息。使用客户端库发布消息
以下是一个使用 Google Cloud Pub/Sub 客户端库来发布消息的示例:```javaimport com.google.cloud.pubsub.v1.Publisher;import com.google.pubsub.v1.PubsubMessage;import com.google.pubsub.v1.TOPicName;public class PublishMessage {public static void main(String... args) throws Exception {// TODO(developer): Replace these variables before running the sample.String projectId = "your-project-id";String topicId = "your-topic-id";String message = "your-message";TopicName topicName = TopicName.of(projectId, topicId);Publisher publisher = null;try {// Create a publisher instance with default settings bound to the topicpublisher = Publisher.newBuilder(topicName).build();// Create a message and publish itbyte[] data = message.getBytes("UTF-8");PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom(data)).build();publisher.publish(pubsubMessage).get();System.out.println("Message published successfully.");} finally {if (publisher != null) {// When finished with the publisher, shutdown to free up resources.publisher.shutdown();publisher.awaitTermination(1, TimeUnit.MINUTES);}}}}```通过 HTTP API 发布消息
以下是一个通过 HTTP API 来发布消息的示例:```curl -X POST \-H "Authorization: Bearer $(gcloud auth Application-default print-access-token)" \-H "Content-Type: application/json" \-d '{"message": {"data": "your-message"}}' \"https://pubsub.googleapis.com/v1/projects/$PROJECT_ID/topics/$TOPIC_ID:publish"```拉取消息
您可以使用 Google Cloud Pub/Sub 客户端库或通过 HTTP API 来拉取消息。使用客户端库拉取消息
以下是一个使用 Google Cloud Pub/Sub 客户端库来拉取消息的示例:```javaimport com.google.cloud.pubsub.v1.AckReplyConsumer;import com.google.cloud.pubsub.v1.MessageReceiver;import com.google.cloud.pubsub.v1.Subscriber;import com.google.pubsub.v1.ProjectSubscriptionName;import com.google.pubsub.v1.PubsubMessage;public class SubscribeExample {public static void main(String... args) throws Exception {// TODO(developer): Replace these variables before running the sample.String projectId = "your-project-id";String subscriptionId = "your-subscription-id";ProjectSubscriptionName subscriptionName =ProjectSubscriptionName.of(projectId, subscriptionId);// Instantiate an asynchronous message receiverMessageReceiver receiver =(PubsubMessage message, AckReplyConsumer consumer) -> {// Handle incoming message, then ack the message.System.out.println("Id: " + message.getMessageId());System.out.println("Data: " + message.getData().toStringUtf8());consumer.ack();};Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();subscriber.startAsync();// Allow the subscriber to run for 30s unless an unrecoverable error occurs.subscriber.awaitTerminated(30, TimeUnit.SECONDS);}}```通过 HTTP API 拉取消息
以下是一个通过 HTTP API 来拉取消息的示例:```curl -X POST \-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \-H "Content-Type: application/json" \-d '{"subscription": "projects/$PROJECT_ID/subscriptions/$SUBSCRIPTION_ID"}' \"https://pubsub.googleapis.com/v1/projects/$PROJECT_ID/subscriptions/$SUBSCRIPTION_ID:pull"```其他资源
Google Cloud Pub/Sub 文档 Google Cloud Pub/Sub Java 客户端库 Google Cloud Pub/Sub Python 客户端库我们的服务
选择我们的理由
多平台兼容
安卓/苹果/电脑/平板/OS均可使用
自助服务
24小时自助下单,安全快捷稳定
智能算法
发送或接收消息采用自研独家协议算法
前沿技术
发送或接收消息提供专业的快速网红变现速成方法,是个人和电商带货的首选平台!诚信经营,不执行就退款,24小时助力网红自助下单平台。
专人维护
发送或接收消息每个业务单独技术人员维护
专属客服
发送或接收消息专业客服365天在线解答