Publica y recibe mensajes en Pub/Sub con gcloud CLI

En esta página, se muestra cómo realizar las siguientes operaciones en Pub/Sub con Google Cloud CLI:

  • Crea un tema y una suscripción.
  • Publica mensajes en el tema.
  • Recibe mensajes de la suscripción.

Antes de comenzar

  1. Set up a Trusted 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 Trusted Cloud console.

  2. Install the Google Cloud CLI.

  3. Configura gcloud CLI para usar tu identidad federada.

    Para obtener más información, consulta Accede a gcloud CLI con tu identidad federada.

  4. Para inicializar la CLI de gcloud, ejecuta el siguiente comando:

    gcloud init

    Crea un tema

    Crea un tema con el ID my-topic:

    gcloud pubsub topics create my-topic

    Cree una suscripción

    Crea una suscripción con el ID my-sub y adjúntala a my-topic:

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

    Publique mensajes

    Publica un mensaje en my-topic:

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

    Recibir mensajes

    Recibe el mensaje de my-sub:

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

    Gcloud CLI imprime el mensaje en la línea de comandos.

    ¿Cómo fue?

    ¿Qué sigue?