Pubblica e ricevi messaggi in Pub/Sub utilizzando gcloud CLI

Questa pagina mostra come eseguire le seguenti operazioni in Pub/Sub utilizzando Google Cloud CLI:

  • Crea un argomento e una sottoscrizione.
  • Pubblicare messaggi nell'argomento.
  • Ricevi i messaggi dalla sottoscrizione.

Per seguire le indicazioni dettagliate per questa attività direttamente nella Google Cloud console, fai clic su Procedura guidata:

Procedura guidata


Prima di iniziare

  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. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  5. Per inizializzare gcloud CLI, esegui questo comando:

    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. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  9. Per inizializzare gcloud CLI, esegui questo comando:

    gcloud init
  10. Crea un argomento

    Crea un argomento con ID my-topic:

    gcloud pubsub topics create my-topic

    Creare una sottoscrizione

    Crea una sottoscrizione con l'ID my-sub e collegala a my-topic:

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

    pubblica dei messaggi

    Pubblica un messaggio in my-topic:

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

    Ricevere messaggi

    Ricevi il messaggio da my-sub:

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

    gcloud CLI stampa il messaggio nella riga di comando.

    Com'è andata?

    Passaggi successivi