gcloud CLI を使用して Pub/Sub でメッセージをパブリッシュし、受信する

このページでは、Google Cloud CLI を使用して Pub/Sub で次の操作を行う方法について説明します。

  • トピックとサブスクリプションを作成する。
  • トピックにメッセージをパブリッシュする。
  • サブスクリプションからメッセージを受信する。

このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。

ガイドを表示


始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Pub/Sub API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  3. Install the Google Cloud CLI.

  4. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  5. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  6. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Pub/Sub API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  7. Install the Google Cloud CLI.

  8. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  9. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  10. トピックの作成

    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 は、コマンドラインにメッセージを出力します。

    いかがでしたか

    次のステップ