Pubblicare gli eventi direttamente

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:

  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. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project.

  13. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  14. Pubblicare direttamente

    Il messaggio evento che pubblichi direttamente deve essere conforme alla specifica CloudEvents.

    gcloud

    1. Apri un terminale.

    2. Puoi pubblicare eventi in un bus utilizzando il comando gcloud eventarc message-buses publish. Ad esempio:

      gcloud eventarc message-buses publish BUS_NAME \
          --avro-message=AVRO_MESSAGE
      Oppure:
      gcloud eventarc message-buses publish BUS_NAME \
          --json-message=JSON_MESSAGE
      Oppure:
      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 che source + id sia 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-attributes per 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).

    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:

    • Il nome completo della risorsa del bus nel formato projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME

      Sostituisci 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 esempio us-central1.
      • BUS_NAME: il nome del bus a cui pubblicare l'evento.
    • SPEC_VERSION: la versione della specifica CloudEvents utilizzata dall'evento, ad esempio 1.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 che source + id sia univoco per ogni evento distinto.
    • CONTENT_TYPE (facoltativo): il tipo di contenuti del valore data. Se un evento in formato JSON non ha l'attributo datacontenttype, si presume che i dati siano un valore JSON conforme al tipo di media application/json.
    • DATA_PAYLOAD (facoltativo): il payload dell'evento codificato nel formato multimediale specificato da datacontenttype e conforme a dataschema quando questi attributi sono presenti.

    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
    
    {}
    

    Passaggi successivi