直接发布事件

您可以使用 Google Cloud CLI 或通过向 Eventarc Publishing REST API 发送请求,以支持的格式将 CloudEvents 事件直接发布到 Eventarc 高级总线。您还可以使用 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 或完全限定标识符。

      您只能使用以下其中一项

      • AVRO_MESSAGE:根据此规范,采用 Avro 格式的事件消息。
      • JSON_MESSAGE:根据此规范,采用 JSON 格式的事件消息。
      • DATA_PAYLOAD:已发布事件的数据。

      如果使用 --event-data 标志,您还必须使用以下标志:

      • EVENT_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:事件标识符。 提供方必须确保 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
    
    {}
    

    后续步骤