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:
- 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 Buka terminal.
Anda dapat memublikasikan peristiwa ke bus menggunakan perintah
gcloud eventarc message-buses publish. Contoh:Atau:gcloud eventarc message-buses publish BUS_NAME \ --avro-message=AVRO_MESSAGE
Atau: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
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 bahwasource+idbersifat 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-attributesuntuk menambahkan lebih banyak atribut.Perhatikan bahwa peristiwa dapat mencakup sejumlah atribut CloudEvents kustom tambahan dengan nama yang berbeda (juga dikenal sebagai atribut ekstensi).
- Nama lengkap resource bus dalam format
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAMEGanti 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 bahwasource+idbersifat unik untuk setiap acara yang berbeda.CONTENT_TYPE(opsional): jenis konten nilaidata. Jika peristiwa format JSON tidak memiliki atributdatacontenttype, maka diasumsikan bahwa data adalah nilai JSON yang sesuai dengan jenis mediaapplication/json.DATA_PAYLOAD(opsional): payload peristiwa yang dienkode ke dalam format media yang ditentukan olehdatacontenttypedan mematuhidataschemajika atribut tersebut ada.- Memublikasikan acara dari sumber Google
- Mentransformasi peristiwa yang diterima
- Memformat peristiwa yang diterima
Publikasikan secara langsung
Pesan peristiwa yang Anda publikasikan secara langsung harus sesuai dengan spesifikasi CloudEvents.
gcloud
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:
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
{}