直接發布事件

您可以使用 Google Cloud CLI,或向 Eventarc Publishing REST API 發出要求,以支援的格式將 CloudEvents 事件直接發布至 Eventarc Advanced 匯流排。您也可以使用Eventarc 用戶端程式庫,透過支援的語言存取 Eventarc API。

事前準備

開始之前,請確認您已完成下列步驟:

  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. 直接發布

    您直接發布的事件訊息必須符合 CloudEvents 規格

    gcloud

    1. 開啟終端機。

    2. 您可以使用 gcloud eventarc message-buses publish 指令,將事件發布至匯流排。例如:

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

      更改下列內容:

      • BUS_NAME:要發布事件的匯流排 ID 或完整 ID。

      您只能使用下列其中一種方法:

      • AVRO_MESSAGE:Avro 格式的事件訊息,請參閱這份規格
      • JSON_MESSAGE:JSON 格式的事件訊息,請參閱這份規格
      • DATA_PAYLOAD:已發布事件的資料。

      如果使用 --event-data 旗標,也必須使用下列項目:

      • EVENT_ID:事件 ID。事件產生者必須確保每個不同的事件都有不重複的 source + id
      • EVENT_SOURCE:已發布事件的事件來源。
      • EVENT_TYPE:與原始事件相關的事件類型。

      使用 --event-data 標記時,您可以視需要使用下列項目:

      • EVENT_ATTRIBUTE:已發布事件的屬性。 如要新增更多屬性,可以重複使用 --event-attributes 標記。

        請注意,事件可能包含任意數量的其他自訂 CloudEvents 屬性,且名稱各不相同 (也稱為擴充屬性)。

    範例:

    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

    如要將事件發布至匯流排,請使用 projects.locations.messageBuses.publish 方法。

    使用任何要求資料之前,請先替換以下項目:

    • 匯流排的完整資源名稱,格式為 projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME

      更改下列內容:

      • PROJECT_ID:公車專案的 Google Cloud 專案 ID。
      • LOCATION:部署匯流排的區域,例如 us-central1
      • BUS_NAME:要發布事件的匯流排名稱。
    • SPEC_VERSION:事件使用的 CloudEvents 規格版本,例如 1.0
    • EVENT_TYPE:與原始事件相關的事件類型。
    • EVENT_SOURCE:已發布事件的事件來源。
    • EVENT_ID:事件 ID。 製作者必須確保每個不同活動的 source + id 都是不重複的。
    • CONTENT_TYPE (選用):data 值的內容類型。如果 JSON 格式的事件沒有 datacontenttype 屬性,系統會假設資料是符合 application/json 媒體類型的 JSON 值。
    • DATA_PAYLOAD (選用):事件酬載,已編碼為 datacontenttype 指定的媒體格式,並遵守 dataschema (如有這些屬性)。

    JSON 要求主體:

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

    請展開以下其中一個選項,以傳送要求:

    如果成功,伺服器會傳回 HTTP 200 OK 狀態碼,以及 JSON 格式的空白回應主體:

    200 OK
    
    {}
    

    後續步驟