פרסום וקבלת הודעות ב-Pub/Sub באמצעות מסוף Google Cloud
בדף הזה מוסבר איך לבצע משימות בסיסיות ב-Pub/Sub באמצעות מסוףGoogle Cloud .
לחצו על תראו לי איך כדי לקרוא הסבר מפורט על המשימה ישירות במסוף Google Cloud :
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 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, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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.
-
יצירת נושא
נכנסים לדף Pub/Sub במסוף Google Cloud .
בדף Topics, לוחצים על
Create topic .בחלון שנפתח, מזינים
MyTopicבשדה Topic ID.משאירים את ערכי ברירת המחדל בשאר האפשרויות ולוחצים על Create.
מוצגת הודעת האישור:
A new topic and a new subscription have been successfully created.יצרתם נושא בשם
MyTopicומינוי משויך שמוגדר כברירת מחדל בשםMyTopic-sub.
הוספת מינוי שני
כדי להוסיף מינוי שני לנושא שיצרתם:
נכנסים לדף Pub/Sub subscriptions במסוף Google Cloud .
בדף Subscriptions, לוחצים על Create subscription.
בשדה
Subscription ID מזינים את הערךMySub.בקטע Select a Cloud Pub/Sub topic, בוחרים את הנושא
MyTopicמהתפריט הנפתח.משאירים את ערכי ברירת המחדל בשאר האפשרויות.
לוחצים על
יצירה .מוצגת הודעת האישור:
Subscription successfully added.המינוי
MySubמצורף עכשיו לנושאMyTopic. Pub/Sub מעביר את כל ההודעות שנשלחות אלMyTopicלמינוייםMySubו-MyTopic-sub.
פרסום הודעה בנושא
נכנסים לדף Pub/Sub topics במסוף Google Cloud .
לוחצים על הנושא
MyTopic.לוחצים על הדף
נושאים ואז עלMyTopic.בדף
MyTopic, לוחצים על הכרטיסייההודעות .לוחצים על
Publish message .בחלון
גוף ההודעה , כותביםHello World.לוחצים על
פרסום . אם ההודעה פורסמה בהצלחה, בתחתית הדף תופיע ההודעה "ההודעה פורסמה".
שליפת ההודעות מהמינוי
נכנסים לדף Pub/Sub subscriptions במסוף Google Cloud .
בכרטיסייה Messages, לוחצים על Pull.
ההודעה שפרסמתם אמורה להופיע. ההודעה מכילה את הנתונים, Hello World, ואת השעה שבה ההודעה פורסמה.
כשמשתמשים במסוף Google Cloud , משיכה בודדת של נפח הודעות נמוך יכולה לעיתים קרובות להחזיר אפס הודעות. אם לא מופיעות הודעות, לוחצים על Pull כמה פעמים כדי לשלוח כמה בקשות משיכה. זו לא בעיה בספריות הלקוח של Pub/Sub.
הסרת המשאבים
כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.
אם אתם לא צריכים יותר את הנושא והמינוי שיצרתם, אתם יכולים למחוק אותם.
כדי למחוק את הנושא והמינוי, מבצעים את השלבים הבאים:
נכנסים לדף
Topics .מסמנים את התיבה לצד
MyTopicולוחצים עלDelete .מבצעים את השלבים שבחלון האישור כדי למחוק את הנושא באופן סופי.
אפשר גם למחוק את הפרויקט שיצרתם.
המאמרים הבאים
דוגמה מקצה לקצה של מערכת Pub/Sub.
נסו מדריך אחר למתחילים של Pub/Sub שמשתמש בספריות לקוח או ב-Google Cloud CLI.