Memublikasikan dan menerima pesan di Pub/Sub menggunakan Google Cloud konsol
Halaman ini menunjukkan cara melakukan tugas dasar di Pub/Sub menggunakan konsolGoogle Cloud .
Untuk mengikuti panduan langkah demi langkah untuk tugas ini langsung di Google Cloud konsol, klik Pandu saya:
Sebelum memulai
- Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
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.
-
Membuat topik
Di konsol Google Cloud , buka halaman Pub/Sub.
Di halaman Topics, klik
Buat topik .Di jendela yang terbuka, masukkan
MyTopicdi kolom ID Topik.Biarkan nilai default untuk opsi lainnya, lalu klik Create.
Anda akan melihat pesan keberhasilan:
A new topic and a new subscription have been successfully created.Anda baru saja membuat topik bernama
MyTopicdan langganan default terkaitMyTopic-sub.
Menambahkan langganan kedua
Untuk menambahkan langganan kedua ke topik yang baru saja Anda buat, selesaikan langkah-langkah berikut:
Di konsol Google Cloud , buka halaman Pub/Sub subscriptions.
Di halaman Subscriptions, klik Create subscription.
Masukkan
MySubdi kolomSubscription ID .Untuk Select a Cloud Pub/Sub topic, pilih topik
MyTopicdari menu drop-down.Biarkan nilai default untuk opsi lainnya.
Klik
Buat .Anda akan melihat pesan keberhasilan:
Subscription successfully added.Langganan
MySubkini terpasang ke topikMyTopic. Pub/Sub mengirimkan semua pesan yang dikirim keMyTopicke langgananMySubdanMyTopic-sub.
Memublikasikan pesan ke topik
Di konsol Google Cloud , buka halaman Pub/Sub topics.
Klik topik
MyTopic.Klik halaman
Topik lalu klikMyTopic.Di halaman
MyTopic, klik tabPesan .Klik
Publikasikan pesan .Di jendela
Isi pesan , masukkanHello World.Klik
Publikasikan . Pesan "Message published" akan muncul di bagian bawah halaman jika publikasi berhasil.
Mengambil pesan dari langganan
Di konsol Google Cloud , buka halaman Pub/Sub subscriptions.
Di tab Messages, klik Pull.
Anda akan melihat pesan yang baru saja Anda publikasikan. Pesan memiliki
data, Hello World, dan waktu saat pesan dipublikasikan.
Saat menggunakan konsol Google Cloud , penarikan individual untuk volume pesan yang rendah sering kali menampilkan nol pesan. Jika Anda tidak melihat pesan, klik Tarik beberapa kali untuk mengirimkan beberapa permintaan penarikan. Ini bukan masalah pada Library Klien Pub/Sub.
Pembersihan
Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.
Jika tidak lagi memerlukan topik dan langganan yang Anda buat, Anda dapat menghapusnya.
Untuk menghapus topik dan langganan, selesaikan langkah-langkah berikut:
Buka halaman
Topik .Centang kotak di samping
MyTopic, lalu klikHapus .Selesaikan langkah-langkah di jendela konfirmasi untuk menghapus topik secara permanen.
Atau, Anda dapat menghapus project yang Anda buat.
Langkah berikutnya
Pelajari lebih lanjut konsep Pub/Sub yang dibahas di halaman ini.
Pelajari contoh end-to-end sistem Pub/Sub.
Coba panduan memulai Pub/Sub lain yang menggunakan library klien atau Google Cloud CLI.
Pelajari lebih lanjut Pub/Sub API.