En este documento, se muestra cómo publicar mensajes en un tema con un esquema.
Antes de comenzar
Antes de configurar el flujo de trabajo de publicación, asegúrate de haber completado los siguientes pasos: tareas:
- Comprender cómo funcionan los esquemas de Pub/Sub
- Asocia un esquema a un tema.
Roles obligatorios
Para obtener los permisos que necesitas para publicar mensajes en un tema,
pídele a tu administrador que te otorgue el rol de IAM de
Publicador de Pub/Sub (roles/pubsub.publisher
) en el tema.
Para obtener más información sobre cómo otorgar roles, consulta Administra el acceso a proyectos, carpetas y organizaciones.
También puedes obtener los permisos necesarios mediante roles personalizados o cualquier otro rol predefinido.
Necesitas permisos adicionales para crear o actualizar temas y suscripciones.Publica mensajes con un esquema
Puedes publicar mensajes en un tema asociado con un esquema.
Debes codificar los mensajes en el esquema y el formato que hayas especificado
cuando creaste el tema. Cuando un mensaje no
coinciden con el esquema asociado al tema, Pub/Sub devuelve un
Se produjo un error INVALID_ARGUMENT
en la solicitud de publicación.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Publica un mensaje de muestra con el gcloud pubsub topics publish.
gcloud pubsub topics publish TOPIC_ID \ --message=MESSAGE
Reemplaza lo siguiente:
TOPIC_ID: Es el nombre del tema que ya creaste.
MESSAGE: Es el mensaje publicado en el tema. Un mensaje de muestra puede ser
{"name": "Alaska", "post_abbr": "AK"}
.
C++
Antes de probar esta muestra, sigue las instrucciones de configuración de C++ en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C++.
Avro ProtoC#
Antes de probar esta muestra, sigue las instrucciones de configuración de C# en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C#.
Avro ProtoGo
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Avro ProtoJava
Antes de probar esta muestra, sigue las instrucciones de configuración de Java en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Java.
Avro ProtoNode.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Avro Búfer de protocoloNode.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Avro Búfer de protocoloPHP
Antes de probar esta muestra, sigue las instrucciones de configuración de PHP en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para PHP.
Avro Búfer de protocoloPython
Antes de probar esta muestra, sigue las instrucciones de configuración de Python en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Python.
Avro Búfer de protocoloRuby
Antes de probar esta muestra, sigue las instrucciones de configuración de Ruby en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Ruby.
Avro Búfer de protocolo¿Qué sigue?
Para restringir las ubicaciones en las que Pub/Sub almacena los datos de los mensajes, consulta Restringe las ubicaciones de recursos de Pub/Sub.
Para obtener más información sobre la recepción de mensajes, consulta Elige un tipo de suscripción.