Google Cloud コンソールを使用して Pub/Sub でメッセージをパブリッシュして受信する
このページでは、Google Cloud コンソールを使用して Pub/Sub の基本的なタスクを行う方法について説明します。
このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。
始める前に
- Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. 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.enablepermission. 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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
-
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. 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.enablepermission. 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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
トピックの作成
Google Cloud コンソールで、[Pub/Sub] ページに移動します。
[トピック] ページで、[
トピックを作成 ] をクリックします。表示されたウィンドウで、[トピック ID] フィールドに「
MyTopic」と入力します。その他の項目はデフォルト値のままにして、[作成] をクリックします。
成功メッセージが表示されます:
A new topic and a new subscription have been successfully created.これで
MyTopicというトピックと、それに関連付けられたデフォルトのサブスクリプションMyTopic-subが作成されました。
2 つ目のサブスクリプションを追加する
作成したトピックに 2 つ目のサブスクリプションを追加する手順は、次のとおりです。
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」と入力します。[
公開 ] をクリックします。パブリッシュが正常に行われた場合は、ページの下部に「メッセージを公開しました」というメッセージが表示されます。
サブスクリプションからメッセージを pull する
Google Cloud コンソールで、Pub/Sub の [サブスクリプション] ページに移動します。
[メッセージ] タブで、[Pull] をクリックします。
先ほど公開したメッセージが表示されます。メッセージには、のデータ、Hello World、メッセージの公開時刻が含まれます。
Google Cloud コンソールを使用する場合、メッセージ量が少ない個々の pull では、メッセージが返されないことがよくあります。メッセージが表示されない場合は、[Pull] を複数回クリックして、複数の pull リクエストを発行します。これは、Pub/Sub クライアント ライブラリの問題ではありません。
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。
作成したトピックとサブスクリプションが不要になった場合は、削除できます。
トピックとサブスクリプションを削除するには、次の手順を行います。
[
トピック ] ページに移動します。MyTopicの横にあるチェックボックスをオンにして、[削除 ] をクリックします。確認ウィンドウの手順を完了して、トピックを完全に削除します。
または、作成したプロジェクトを削除することもできます。
次のステップ
このページで説明する Pub/Sub のコンセプトについてさらに学習する。
Pub/Sub サービスの基本を確認する。
Pub/Sub システムのエンドツーエンドの例に取り組む。
クライアント ライブラリまたは Google Cloud CLI を使用する別の Pub/Sub クイックスタートを試します。
Pub/Sub API について学習します。