Memublikasikan peristiwa secara langsung

Anda dapat memublikasikan peristiwa CloudEvents langsung ke bus Eventarc Advanced dalam format yang didukung menggunakan Google Cloud CLI atau dengan mengirim permintaan ke Eventarc Publishing REST API. Anda juga dapat menggunakan library klien Eventarc untuk mengakses API Eventarc dari bahasa yang didukung.

Sebelum memulai

Sebelum memulai, pastikan Anda telah menyelesaikan langkah-langkah berikut:

  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. Publikasikan secara langsung

    Pesan peristiwa yang Anda publikasikan secara langsung harus sesuai dengan spesifikasi CloudEvents.

    gcloud

    1. Buka terminal.

    2. Anda dapat memublikasikan peristiwa ke bus menggunakan perintah gcloud eventarc message-buses publish. Contoh:

      gcloud eventarc message-buses publish BUS_NAME \
          --avro-message=AVRO_MESSAGE
      Atau:
      gcloud eventarc message-buses publish BUS_NAME \
          --json-message=JSON_MESSAGE
      Atau:
      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

      Ganti kode berikut:

      • BUS_NAME: ID atau ID yang memenuhi syarat sepenuhnya dari bus tempat memublikasikan peristiwa.

      Anda hanya boleh menggunakan salah satu opsi berikut:

      • AVRO_MESSAGE: pesan peristiwa dalam format Avro sesuai dengan spesifikasi ini.
      • JSON_MESSAGE: pesan peristiwa dalam format JSON sesuai dengan spesifikasi ini.
      • DATA_PAYLOAD: data peristiwa yang dipublikasikan.

      Jika menggunakan flag --event-data, Anda juga harus menggunakan yang berikut:

      • EVENT_ID: ID peristiwa. Produser acara harus memastikan bahwa source + id bersifat unik untuk setiap acara yang berbeda.
      • EVENT_SOURCE: sumber peristiwa dari peristiwa yang dipublikasikan.
      • EVENT_TYPE: jenis peristiwa yang terkait dengan kejadian awal.

      Jika menggunakan flag --event-data, Anda dapat menggunakan opsi berikut:

      • EVENT_ATTRIBUTE: atribut peristiwa yang dipublikasikan. Anda dapat mengulangi tanda --event-attributes untuk menambahkan lebih banyak atribut.

        Perhatikan bahwa peristiwa dapat mencakup sejumlah atribut CloudEvents kustom tambahan dengan nama yang berbeda (juga dikenal sebagai atribut ekstensi).

    Contoh:

    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"}
    }'

    REST API

    Untuk memublikasikan peristiwa ke bus, gunakan metode projects.locations.messageBuses.publish.

    Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

    • Nama lengkap resource bus dalam format projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME

      Ganti kode berikut:

      • PROJECT_ID: Google Cloud project ID untuk project bus.
      • LOCATION: region tempat bus di-deploy—misalnya, us-central1.
      • BUS_NAME: nama bus tempat memublikasikan peristiwa.
    • SPEC_VERSION: versi spesifikasi CloudEvents yang digunakan peristiwa—misalnya, 1.0.
    • EVENT_TYPE: jenis peristiwa yang terkait dengan kemunculan asal.
    • EVENT_SOURCE: sumber peristiwa dari peristiwa yang dipublikasikan.
    • EVENT_ID: ID peristiwa. Produser harus memastikan bahwa source + id bersifat unik untuk setiap acara yang berbeda.
    • CONTENT_TYPE (opsional): jenis konten nilai data. Jika peristiwa format JSON tidak memiliki atribut datacontenttype, maka diasumsikan bahwa data adalah nilai JSON yang sesuai dengan jenis media application/json.
    • DATA_PAYLOAD (opsional): payload peristiwa yang dienkode ke dalam format media yang ditentukan oleh datacontenttype dan mematuhi dataschema jika atribut tersebut ada.

    Meminta isi JSON:

    {
    "jsonMessage":
      "{\"specversion\":\"SPEC_VERSION\",
      \"type\":\"EVENT_TYPE\",
      \"source\":\"EVENT_SOURCE\",
      \"id\":\"EVENT_ID\",
      \"datacontenttype\":\"CONTENT_TYPE\",
      \"data\":\"DATA_PAYLOAD\"}"
    }
    

    Untuk mengirim permintaan Anda, perluas salah satu opsi berikut:

    Jika berhasil, server akan menampilkan kode status HTTP 200 OK dan isi respons kosong dalam format JSON:

    200 OK
    
    {}
    

    Langkah berikutnya