Google Cloud コンソールを使用して Pub/Sub でメッセージをパブリッシュして受信する

このページでは、Google Cloud コンソールを使用して 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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Pub/Sub API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Pub/Sub Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Pub/Sub API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Pub/Sub Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  10. トピックの作成

    1. Google Cloud コンソールで、[Pub/Sub] ページに移動します。

      [Pub/Sub] に移動

    2. [トピック] ページで、[トピックを作成] をクリックします。

    3. 表示されたウィンドウで、[トピック ID] フィールドに「MyTopic」と入力します。

      その他の項目はデフォルト値のままにして、[作成] をクリックします。

      成功メッセージが表示されます: A new topic and a new subscription have been successfully created.

      これで MyTopic というトピックと、それに関連付けられたデフォルトのサブスクリプション MyTopic-sub が作成されました。

    2 つ目のサブスクリプションを追加する

    作成したトピックに 2 つ目のサブスクリプションを追加する手順は、次のとおりです。

    1. Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。

      [サブスクリプション] に移動

    2. [サブスクリプション] ページで、[サブスクリプションを作成] をクリックします。

    3. [サブスクリプション ID] フィールドに、「MySub」と入力します。

    4. [Cloud Pub/Sub トピックを選択してください] で、プルダウン メニューから MyTopic トピックを選択します。

    5. 残りのオプションはデフォルト値のままにします。

    6. [作成] をクリックします。

      成功メッセージが表示されます: Subscription successfully added.

      これで MySub サブスクリプションがトピック MyTopic にアタッチされました。Pub/Sub は、MyTopic に送信されたすべてのメッセージを MySub サブスクリプションと MyTopic-sub サブスクリプションに配信します。

    トピックへのメッセージのパブリッシュ

    1. Google Cloud コンソールで、[Pub/Sub トピック] ページに移動します。

      [トピック] に移動

    2. MyTopic トピックをクリックします。

    3. [トピック] ページをクリックし、[MyTopic] をクリックします。

    4. [MyTopic] ページで、[メッセージ] タブをクリックします。

    5. [メッセージをパブリッシュ] をクリックします。

    6. [メッセージ本文] ウィンドウに「Hello World」と入力します。

    7. [公開] をクリックします。パブリッシュが正常に行われた場合は、ページの下部に「メッセージを公開しました」というメッセージが表示されます。

    サブスクリプションからメッセージを pull する

    1. Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。

      [サブスクリプション] に移動

    2. [メッセージ] タブで、[Pull] をクリックします。

    先ほど公開したメッセージが表示されます。メッセージには、のデータ、Hello World、メッセージの公開時刻が含まれます。

    Google Cloud コンソールを使用する場合、メッセージ量が少ない個々の pull では、メッセージが返されないことがよくあります。メッセージが表示されない場合は、[Pull] を複数回クリックして、複数の pull リクエストを発行します。これは、Pub/Sub クライアント ライブラリの問題ではありません。

    クリーンアップ

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。

    作成したトピックとサブスクリプションが不要になった場合は、削除できます。

    トピックとサブスクリプションを削除するには、次の手順を行います。

    1. [トピック] ページに移動します。

    2. MyTopic の横にあるチェックボックスをオンにして、[削除] をクリックします。

    3. 確認ウィンドウの手順を完了して、トピックを完全に削除します。

    または、作成したプロジェクトを削除することもできます。

    次のステップ