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:

  • Creare un argomento e una sottoscrizione.
  • Pubblica messaggi nell'argomento.
  • Ricevere messaggi dalla sottoscrizione.

Per seguire le indicazioni dettagliate per questa attività direttamente nella console Google Cloud, 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. 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

crea un argomento

Crea un argomento con ID my-topic:

gcloud pubsub topics create my-topic

crea 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