Nachrichten über die gcloud CLI in Pub/Sub veröffentlichen und empfangen

Auf dieser Seite wird beschrieben, 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

Klicken Sie auf Anleitung, um eine detaillierte Anleitung für diese Aufgabe direkt in der Google Cloud Console aufzurufen.

Anleitung


Hinweise

  1. Melden Sie sich bei Ihrem Google Cloud-Konto an. Wenn Sie mit Google Cloud noch nicht vertraut sind, erstellen Sie ein Konto, um die Leistungsfähigkeit unserer Produkte in der Praxis sehen und bewerten zu können. Neukunden erhalten außerdem ein Guthaben von 300 $, um Arbeitslasten auszuführen, zu testen und bereitzustellen.
  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. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 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.

  6. Install the Google Cloud CLI.
  7. To initialize the gcloud CLI, run the following command:

    gcloud init

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