使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 gcloud CLI 在 Pub/Sub 中发布和接收消息
本页面介绍如何使用 Google Cloud CLI 在 Pub/Sub 中执行以下操作:
- 创建主题和订阅
- 将消息发布到主题。
- 接收来自订阅的消息。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
操作演示
准备工作
-
设置 Google Cloud 控制台项目。
设置项目
点击即可执行以下操作:
- 创建或选择项目。
-
为该项目启用 Pub/Sub API。
您可以随时在 Google Cloud 控制台中查看和管理这些资源。
-
Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
创建主题
创建 ID 为 my-topic
的主题:
gcloud pubsub topics create my-topic
创建订阅
创建 ID 为 my-sub
的订阅,并将其附加到 my-topic
:
gcloud pubsub subscriptions create my-sub --topic=my-topic
发布消息
将消息发布到 my-topic
:
gcloud pubsub topics publish my-topic --message="hello"
接收消息
接收来自 my-sub
的消息:
gcloud pubsub subscriptions pull my-sub --auto-ack
gcloud CLI 会将消息输出到命令行。
结果怎么样?
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-07-28。
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻译问题"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"易于理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"解决了我的问题"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]