Nachrichten in Pub/Sub mit der gcloud CLI veröffentlichen und empfangen

Auf dieser Seite wird erläutert, wie Sie mit der Google Cloud CLI die folgenden Vorgänge in Pub/Sub ausführen:

  • Thema und Abo erstellen
  • Veröffentlichen Sie Nachrichten zum Thema:
  • Nachrichten aus dem Abo empfangen

Eine detaillierte Anleitung dazu finden Sie direkt in der Google Cloud Console. Klicken Sie dazu einfach auf Anleitung:

Anleitung


Hinweise

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Pub/Sub API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  3. Install the Google Cloud CLI.

  4. Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.

  5. Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren:

    gcloud init
  6. Set up a Google Cloud console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Pub/Sub API for that project.

    You can view and manage these resources at any time in the Google Cloud console.

  7. Install the Google Cloud CLI.

  8. Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.

  9. Führen Sie folgenden Befehl aus, um die gcloud CLI zu initialisieren:

    gcloud init
  10. Thema erstellen

    Erstellen Sie ein Thema mit der ID my-topic:

    gcloud pubsub topics create my-topic

    Abo erstellen

    Erstellen Sie ein Abo mit der ID my-sub und hängen Sie es an my-topic an:

    gcloud pubsub subscriptions create my-sub --topic=my-topic

    Nachrichten veröffentlichen

    Veröffentlichen Sie eine Nachricht in my-topic:

    gcloud pubsub topics publish my-topic --message="hello"

    Nachrichten empfangen

    Empfangen Sie die Nachricht von my-sub:

    gcloud pubsub subscriptions pull my-sub --auto-ack

    Die gcloud CLI gibt die Nachricht in der Befehlszeile aus.

    Wie ist es gelaufen?

    Nächste Schritte