Publicar e receber mensagens no Pub/Sub usando a CLI gcloud

Nesta página, mostramos como realizar as seguintes operações no Pub/Sub usando a Google Cloud CLI:

  • Criar um tópico e uma inscrição.
  • Publicar mensagens no tópico.
  • Receber mensagens da assinatura.

Para seguir as instruções passo a passo desta tarefa diretamente no console do Google Cloud, clique em Orientação:

Orientações


Antes de começar

  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

Crie um tópico

Crie um tópico com o ID my-topic:

gcloud pubsub topics create my-topic

Crie uma assinatura

Crie uma assinatura com o ID my-sub e anexe-a ao my-topic:

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

Publicar mensagens

Publique uma mensagem em my-topic:

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

Receber mensagens

Receba a mensagem de my-sub:

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

A CLI gcloud imprime a mensagem na linha de comando.

Como foi?

A seguir