跳至內容

進階事件篩選

Image

我們將學習哪些 Knative 功能?

  • 觸發器 (Trigger) 和 Broker
  • Knative 如何神奇地將所有事物連結在一起

最終交付成果是什麼樣子?

Image

  • 「分析過的評論」將被送回 Broker(使用觸發器),並儲存到資料庫中。
  • 不包含不雅文字的評論將會顯示在 UI 中,並且情緒將以表情符號顯示。

實作

步驟 1:建立資料庫插入的觸發器

Image

將以下觸發器配置附加到現有的 node-server/config/200-broker.yaml 檔案,然後套用

附加node-server/config/200-broker.yaml
---
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
  name: db-insert-trigger
spec:
  broker: bookstore-broker
  filter:
    attributes: # Trigger will filter events based on BOTH the type and badwordfilter attribute
      type: moderated-comment # This is the filter that will be applied to the event, only events with the ce-type moderated-comment will be processed
      badwordfilter: good # This is the filter that will be applied to the event, only events with the ce-extension badwordfilter: good will be processed
  subscriber:
    ref:
      apiVersion: v1
      kind: Service
      name: node-server-svc
    uri: /insert # This is the path where the event will be sent to the subscriber, see /insert in node-server code: index.js
kubectl apply -f node-server/config/200-broker.yaml

套用配置後,您應該會看到

broker.eventing.knative.dev/bookstore-broker unchanged
trigger.eventing.knative.dev/db-insert-trigger created

到目前為止,您在叢集中建立的觸發器應該如下所示

Image

驗證

執行以下命令以檢查觸發器是否已成功建立

kubectl get triggers

您應該會看到觸發器處於就緒狀態

NAME                BROKER             SUBSCRIBER_URI                                                       AGE     READY   REASON
db-insert-trigger   bookstore-broker   http://node-server-svc.default.svc.cluster.local/insert              5h32m   True      
sequence-trigger    bookstore-broker   http://sequence-kn-sequence-0-kn-channel.default.svc.cluster.local   5h30m   True    
log-trigger        bookstore-broker   http://event-display.default.svc.cluster.local                       5h32m   True    

驗證

Image

現在,是神奇的時刻了。所有事物都自動連接起來。嘗試與 UI 互動!

  • 一般評論:當您發送不包含任何「不雅文字」的一般評論時,它將會正確顯示在評論區中。
  • 「不雅文字」評論:包含冒犯性或仇恨言論的評論將會被過濾掉,並最終被重新導向至 Slack(我們將在下一節中介紹)。

很簡單,不是嗎?這就是為什麼 Knative 事件如此有幫助!您只需要專注於開發每個元件,而 Knative 事件會處理服務之間的連接和通訊。每個服務都可以專注於其職責,而無需擔心訊息傳遞。

下一步

Image

您已經建立了事件驅動架構。現在是將其連接到外部服務以進一步增強您的書店應用程式的時候了。在下一節中,我們將讓書店能夠向您的 Slack 工作區發送通知!

前往使用 Camel K 連接 Slack

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