이벤트 직접 게시

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 또는 정규화된 식별자입니다.

      다음 중 하나만 사용해야 합니다.

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

    다음 단계