Puoi pubblicare un evento CloudEvents direttamente in un bus Eventarc Advanced in un formato supportato utilizzando Google Cloud CLI o inviando una richiesta all'API REST di pubblicazione di Eventarc. Puoi anche utilizzare le librerie client Eventarc per accedere alle API Eventarc da un linguaggio supportato.
Prima di iniziare
Prima di iniziare, assicurati di aver completato i seguenti passaggi:
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com Apri un terminale.
Puoi pubblicare eventi in un bus utilizzando il comando
gcloud eventarc message-buses publish. Ad esempio:Oppure:gcloud eventarc message-buses publish BUS_NAME \ --avro-message=AVRO_MESSAGE
Oppure:gcloud eventarc message-buses publish BUS_NAME \ --json-message=JSON_MESSAGE
gcloud eventarc message-buses publish BUS_NAME \ --event-data=DATA_PAYLOAD \ --event-id=EVENT_ID \ --event-source=EVENT_SOURCE \ --event-type=EVENT_TYPE \ --event-attributes=EVENT_ATTRIBUTE
Sostituisci quanto segue:
BUS_NAME: l'ID o l'identificatore completo del bus a cui pubblicare l'evento.
Devi utilizzare solo uno dei seguenti documenti:
AVRO_MESSAGE: il messaggio dell'evento in formato Avro secondo questa specifica.JSON_MESSAGE: il messaggio dell'evento in formato JSON secondo questa specifica.DATA_PAYLOAD: i dati di un evento pubblicato.
Se utilizzi il flag
--event-data, devi utilizzare anche quanto segue:EVENT_ID: l'identificatore dell'evento. I produttori di eventi devono assicurarsi chesource+idsia univoco per ogni evento distinto.EVENT_SOURCE: l'origine evento di un evento pubblicato.EVENT_TYPE: il tipo di evento correlato all'occorrenza di origine.
Se utilizzi il flag
--event-data, puoi facoltativamente utilizzare quanto segue:EVENT_ATTRIBUTE: gli attributi di un evento pubblicato. Puoi ripetere il flag--event-attributesper aggiungere altri attributi.Tieni presente che gli eventi possono includere un numero qualsiasi di attributi CloudEvents personalizzati aggiuntivi con nomi distinti (noti anche come attributi di estensione).
- Il nome completo della risorsa del bus nel formato
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAMESostituisci quanto segue:
PROJECT_ID: l'ID progetto Google Cloud per il progetto bus.LOCATION: la regione in cui viene eseguito il deployment del bus, ad esempious-central1.BUS_NAME: il nome del bus a cui pubblicare l'evento.
SPEC_VERSION: la versione della specifica CloudEvents utilizzata dall'evento, ad esempio1.0.EVENT_TYPE: il tipo di evento correlato all'occorrenza di origine.EVENT_SOURCE: l'origine evento di un evento pubblicato.EVENT_ID: l'identificatore dell'evento. I produttori devono assicurarsi chesource+idsia univoco per ogni evento distinto.CONTENT_TYPE(facoltativo): il tipo di contenuti del valoredata. Se un evento in formato JSON non ha l'attributodatacontenttype, si presume che i dati siano un valore JSON conforme al tipo di mediaapplication/json.DATA_PAYLOAD(facoltativo): il payload dell'evento codificato nel formato multimediale specificato dadatacontenttypee conforme adataschemaquando questi attributi sono presenti.
Pubblicare direttamente
Il messaggio evento che pubblichi direttamente deve essere conforme alla specifica CloudEvents.
gcloud
Esempi:
gcloud eventarc message-buses publish my-bus \ --event-data='{"key": "hello-world-data"}' \ --event-id=hello-world-id-1234 \ --event-source=hello-world-source \ --event-type=hello-world-type \ --event-attributes="datacontenttype=application/json"
gcloud eventarc message-buses publish my-bus --json-message='{ "specversion" : "1.0", "type" :"com.example.someevent", "source" : "google.cloud.storage.object.v1.finalized", "id" : "A234-1234-1234", "time" : "2024-04-05T17:31:00Z", "bucket" : "bucketName", "datacontenttype" : "application/json", "data":{"key": "value"} }'
API REST
Per pubblicare un evento in un bus, utilizza il
metodo
projects.locations.messageBuses.publish.
Prima di utilizzare i dati della richiesta, apporta le sostituzioni seguenti:
Corpo JSON della richiesta:
{
"jsonMessage":
"{\"specversion\":\"SPEC_VERSION\",
\"type\":\"EVENT_TYPE\",
\"source\":\"EVENT_SOURCE\",
\"id\":\"EVENT_ID\",
\"datacontenttype\":\"CONTENT_TYPE\",
\"data\":\"DATA_PAYLOAD\"}"
}
Per inviare la richiesta, espandi una di queste opzioni:
Se l'operazione va a buon fine, il server restituisce un codice di stato HTTP 200 OK e la risposta con un testo vuoto in formato JSON:
200 OK
{}