פרסום וקבלת הודעות ב-Pub/Sub באמצעות מסוף Google Cloud

בדף הזה מוסבר איך לבצע משימות בסיסיות ב-Pub/Sub באמצעות מסוףGoogle Cloud .


לחצו על תראו לי איך כדי לקרוא הסבר מפורט על המשימה ישירות במסוף Google Cloud :

תראו לי איך


לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  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 role (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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    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.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  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 role (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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    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.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

יצירת נושא

  1. נכנסים לדף Pub/Sub במסוף Google Cloud .

    כניסה ל-Pub/Sub

  2. בדף Topics, לוחצים על Create topic.

  3. בחלון שנפתח, מזינים MyTopic בשדה Topic ID.

    משאירים את ערכי ברירת המחדל בשאר האפשרויות ולוחצים על Create.

    מוצגת הודעת האישור: A new topic and a new subscription have been successfully created.

    יצרתם נושא בשם MyTopic ומינוי משויך שמוגדר כברירת מחדל בשם MyTopic-sub.

הוספת מינוי שני

כדי להוסיף מינוי שני לנושא שיצרתם:

  1. נכנסים לדף Pub/Sub subscriptions במסוף Google Cloud .

    מעבר אל "מינויים"

  2. בדף Subscriptions, לוחצים על Create subscription.

  3. בשדה Subscription ID מזינים את הערך MySub.

  4. בקטע Select a Cloud Pub/Sub topic, בוחרים את הנושא MyTopic מהתפריט הנפתח.

  5. משאירים את ערכי ברירת המחדל בשאר האפשרויות.

  6. לוחצים על יצירה.

    מוצגת הודעת האישור: Subscription successfully added.

    המינוי MySub מצורף עכשיו לנושא MyTopic. ‫Pub/Sub מעביר את כל ההודעות שנשלחות אל MyTopic למינויים MySub ו-MyTopic-sub.

פרסום הודעה בנושא

  1. נכנסים לדף Pub/Sub topics במסוף Google Cloud .

    מעבר אל "נושאים"

  2. לוחצים על הנושא MyTopic.

  3. לוחצים על הדף נושאים ואז על MyTopic.

  4. בדף MyTopic, לוחצים על הכרטיסייה הודעות.

  5. לוחצים על Publish message.

  6. בחלון גוף ההודעה, כותבים Hello World.

  7. לוחצים על פרסום. אם ההודעה פורסמה בהצלחה, בתחתית הדף תופיע ההודעה "ההודעה פורסמה".

שליפת ההודעות מהמינוי

  1. נכנסים לדף Pub/Sub subscriptions במסוף Google Cloud .

    מעבר אל "מינויים"

  2. בכרטיסייה Messages, לוחצים על Pull.

ההודעה שפרסמתם אמורה להופיע. ההודעה מכילה את הנתונים, Hello World, ואת השעה שבה ההודעה פורסמה.

כשמשתמשים במסוף Google Cloud , משיכה בודדת של נפח הודעות נמוך יכולה לעיתים קרובות להחזיר אפס הודעות. אם לא מופיעות הודעות, לוחצים על Pull כמה פעמים כדי לשלוח כמה בקשות משיכה. זו לא בעיה בספריות הלקוח של Pub/Sub.

הסרת המשאבים

כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.

אם אתם לא צריכים יותר את הנושא והמינוי שיצרתם, אתם יכולים למחוק אותם.

כדי למחוק את הנושא והמינוי, מבצעים את השלבים הבאים:

  1. נכנסים לדף Topics.

  2. מסמנים את התיבה לצד MyTopic ולוחצים על Delete.

  3. מבצעים את השלבים שבחלון האישור כדי למחוק את הנושא באופן סופי.

אפשר גם למחוק את הפרויקט שיצרתם.

המאמרים הבאים