פרסום וקבלת הודעות ב-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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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. בדף נושאים, לוחצים על יצירת נושא.

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

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

    מוצגת הודעת ההצלחה: A new topic and a new subscription have been successfully created.

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

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

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

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

    לדף "מינויים"

  2. בדף מינויים, לוחצים על יצירת מינוי.

  3. מזינים MySub בשדה Subscription ID (מזהה המינוי).

  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 .

    לדף Topics

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

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

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

  5. לוחצים על פרסום ההודעה.

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

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

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

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

    לדף "מינויים"

  2. בכרטיסייה Messages (הודעות), לוחצים על Pull (משיכה).

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

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

הסרת המשאבים

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

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

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

  1. עוברים לדף נושאים.

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

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

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

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