Google Cloud コンソールを使用して Pub/Sub でメッセージをパブリッシュして受信する
このページでは、Google Cloud コンソールを使用して Pub/Sub の基本的なタスクを行う方法について説明します。
このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。
始める前に
- 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.
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theserviceusage.services.enable
permission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Pub/Sub Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
IAM に移動 - プロジェクトを選択します。
- [ アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
-
-
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 theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
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 theserviceusage.services.enable
permission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Pub/Sub Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
IAM に移動 - プロジェクトを選択します。
- [ アクセスを許可] をクリックします。
-
[新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。
- [ロールを選択] リストでロールを選択します。
- 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
- [保存] をクリックします。
-
Google Cloud コンソールで、[Pub/Sub] ページに移動します。
[トピック] ページで、[
トピックを作成 ] をクリックします。表示されたウィンドウで、[トピック ID] フィールドに「
MyTopic
」と入力します。その他の項目はデフォルト値のままにして、[作成] をクリックします。
成功メッセージが表示されます:
A new topic and a new subscription have been successfully created.
これで
MyTopic
というトピックと、それに関連付けられたデフォルトのサブスクリプションMyTopic-sub
が作成されました。Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。
[サブスクリプション] ページで、[サブスクリプションを作成] をクリックします。
[
サブスクリプション ID ] フィールドに、「MySub
」と入力します。[Cloud Pub/Sub トピックを選択してください] で、プルダウン メニューから
MyTopic
トピックを選択します。残りのオプションはデフォルト値のままにします。
[
作成 ] をクリックします。成功メッセージが表示されます:
Subscription successfully added.
これで
MySub
サブスクリプションがトピックMyTopic
にアタッチされました。Pub/Sub は、MyTopic
に送信されたすべてのメッセージをMySub
サブスクリプションとMyTopic-sub
サブスクリプションに配信します。Google Cloud コンソールで、[Pub/Sub トピック] ページに移動します。
MyTopic
トピックをクリックします。[
トピック ] ページをクリックし、[MyTopic
] をクリックします。[
MyTopic
] ページで、[メッセージ ] タブをクリックします。[
メッセージをパブリッシュ ] をクリックします。[
メッセージ本文 ] ウィンドウに「Hello World
」と入力します。[
公開 ] をクリックします。パブリッシュが正常に行われた場合は、ページの下部に「メッセージを公開しました」というメッセージが表示されます。Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。
[メッセージ] タブで、[Pull] をクリックします。
[
トピック ] ページに移動します。MyTopic
の横にあるチェックボックスをオンにして、[削除 ] をクリックします。確認ウィンドウの手順を完了して、トピックを完全に削除します。
このページで説明する Pub/Sub のコンセプトについてさらに学習する。
Pub/Sub サービスの基本を確認する。
Pub/Sub システムのエンドツーエンドの例に取り組む。
クライアント ライブラリまたは Google Cloud CLI を使用する別の Pub/Sub クイックスタートを試します。
Pub/Sub API について学習します。
トピックの作成
2 つ目のサブスクリプションを追加する
作成したトピックに 2 つ目のサブスクリプションを追加する手順は、次のとおりです。
トピックへのメッセージのパブリッシュ
サブスクリプションからメッセージを pull する
先ほど公開したメッセージが表示されます。メッセージには、のデータ、Hello World
、メッセージの公開時刻が含まれます。
Google Cloud コンソールを使用する場合、メッセージ量が少ない個々の pull では、メッセージが返されないことがよくあります。メッセージが表示されない場合は、[Pull] を複数回クリックして、複数の pull リクエストを発行します。これは、Pub/Sub クライアント ライブラリの問題ではありません。
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。
作成したトピックとサブスクリプションが不要になった場合は、削除できます。
トピックとサブスクリプションを削除するには、次の手順を行います。
または、作成したプロジェクトを削除することもできます。