跳至內容

將 Knative 服務作為來源

在本教學課程中,您將使用 CloudEvents Player 應用程式來展示 Knative 事件處理的核心概念。在本教學課程結束時,您應該會有一個如下所示的架構

The CloudEvents Player acts as both a source and a sink for CloudEvents

以上圖片是來自Knative in Action的圖 6.6。

建立您的第一個來源

CloudEvents Player 透過接收 Broker 的名稱作為環境變數 `BROKER_NAME`,來作為 CloudEvents 的來源。如果 Broker 位於不同的命名空間,也可以設定 `BROKER_NAMESPACE` 環境變數。或者,您也可以直接使用 `BROKER_URI`。

您將透過 CloudEvents Player 應用程式將 CloudEvents 傳送到 Broker。

建立 CloudEvents Player 服務

執行命令

kn service create cloudevents-player \
--image quay.io/ruben/cloudevents-player:latest

預期輸出

Service 'cloudevents-player' created to latest revision 'cloudevents-player-00001' is available at URL:
http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io
  1. 將以下 YAML 複製到名為 `cloudevents-player.yaml` 的檔案中

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: cloudevents-player
    spec:
      template:
        metadata:
          annotations:
            autoscaling.knative.dev/min-scale: "1"
        spec:
          containers:
            - image: quay.io/ruben/cloudevents-player:latest
    

  2. 執行命令來套用 YAML 檔案

    kubectl apply -f cloudevents-player.yaml
    

    預期輸出

    service.serving.knative.dev/cloudevents-player created
    

服務現在正在執行,但它不知道 Broker 在哪裡,因此讓我們在服務和 Broker 之間建立一個 SinkBinding

執行命令

kn source binding create ce-player-binding --subject "Service:serving.knative.dev/v1:cloudevents-player" --sink broker:example-broker

預期輸出

Sink binding 'ce-player-binding' created in namespace 'default'.
  1. 將以下 YAML 複製到名為 `cloudevents-player-binding.yaml` 的檔案中

    apiVersion: sources.knative.dev/v1
    kind: SinkBinding
    metadata:
      name: ce-player-binding
    spec:
      sink:
        ref:
          apiVersion: eventing.knative.dev/v1
          kind: Broker
          name: example-broker
      subject:
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: cloudevents-player
    

  2. 執行命令來套用 YAML 檔案

    kubectl apply -f cloudevents-player-binding.yaml
    

    預期輸出

    sinkbinding.sources.knative.dev/ce-player-binding created
    

檢查 CloudEvents Player

您可以使用 CloudEvents Player 來傳送和接收 CloudEvents。 如果您在瀏覽器中開啟服務 URL,將會出現「建立事件」表單。

服務 URL 是 `http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io`,例如,`kind` 的 http://cloudevents-player.default.127.0.0.1.sslip.io

The user interface for the CloudEvents Player

這些欄位是什麼意思?
欄位 說明
事件 ID 唯一的 ID。按一下循環圖示產生一個新的 ID。
事件類型 事件類型。
事件來源 事件來源。
Specversion 標示您正在使用的 CloudEvents 規格(應始終為 1.0)。
訊息 CloudEvent 的 `data` 區段,這是載有您想要傳送的資料的酬載。

如需有關 CloudEvents 規格的更多資訊,請查看 CloudEvents 規格

傳送事件

嘗試使用 CloudEvents Player 介面傳送事件

  1. 在表單中填寫您想要的任何資料。
  2. 確保您的事件來源不包含任何空格。
  3. 按一下傳送事件

CloudEvents Player Send

按一下 將顯示 Broker 所看到的 CloudEvent。

Event Details

想改用命令列傳送事件嗎?

除了網頁表單之外,也可以使用命令列傳送/檢視事件。

要張貼事件

curl -i http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io \
    -H "Content-Type: application/json" \
    -H "Ce-Id: 123456789" \
    -H "Ce-Specversion: 1.0" \
    -H "Ce-Type: some-type" \
    -H "Ce-Source: command-line" \
    -d '{"msg":"Hello CloudEvents!"}'

要檢視事件

curl http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io/messages

「狀態」欄中的 圖示表示事件已傳送到我們的 Broker... 但事件跑到哪裡去了?嗯,現在哪裡都沒去!

Broker 只是事件的容器。為了讓您的事件傳送到任何地方,您必須建立一個觸發器來偵聽您的事件並將它們放置到某個地方。幸運的是,您將在下一頁建立您的第一個觸發器!

我們使用分析和 Cookie 來了解網站流量。關於您使用我們網站的資訊會與 Google 分享,以達到該目的。瞭解更多。