溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何分析Knative Eventing中的Sequence及其4 種使用場景

發(fā)布時(shí)間:2021-12-28 15:18:18 來源:億速云 閱讀:155 作者:柒染 欄目:云計(jì)算

如何分析Knative Eventing中的Sequence及其4 種使用場景,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

導(dǎo)讀:在實(shí)際的開發(fā)中我們經(jīng)常會遇到將一條數(shù)據(jù)需要經(jīng)過多次處理的場景,稱為 Pipeline。那么在 Knative 中是否也提供這樣的能力呢?其實(shí)從 Knative Eventing 0.7 版本開始,就提供了 Sequence CRD 資源,用于事件處理 Pipeline。下面將為大家詳細(xì)介紹 Sequence 的定義及在 Knative Eventing 中提供的 4 種使用場景。

Sequence 定義

首先來看一下 Sequence Spec 定義:

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: test
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: test
  reply:
    kind: Broker
    apiVersion: eventing.knative.dev/v1alpha1
    name: test

Sequence Spec 包括 3 個(gè)部分:

  1. steps: 在 step 中定義了按照順序執(zhí)行的服務(wù),每個(gè)服務(wù)會對應(yīng)創(chuàng)建 Subscription;

  2. channelTemplate:指定了使用具體的那個(gè) Channel;

  3. reply:(可選)定義了將最后一個(gè) step 服務(wù)結(jié)果轉(zhuǎn)發(fā)到的目標(biāo)服務(wù)。

    Sequence 都是適合哪些具體應(yīng)用場景呢?我們上面也提到了事件處理的 Pipeline。那么在實(shí)際場景應(yīng)用中究竟以什么樣的形式體現(xiàn)呢? 現(xiàn)在我們揭曉一下 Sequence 在 Knative Eventing 中提供的如下 4 種使用場景:

  • 直接訪問 Service;

  • 面向事件處理;

  • 級聯(lián) Sequence;

  • 面向 Broker/Trigger。

直接訪問 Service 場景

事件源產(chǎn)生的事件直接發(fā)送給 Sequence 服務(wù), Sequence 接收到事件之后順序調(diào)用 Service 服務(wù)對事件進(jìn)行處理:

如何分析Knative Eventing中的Sequence及其4 種使用場景

創(chuàng)建 Knative Service

這里我們創(chuàng)建 3 個(gè) Knative Service 用于事件處理。每個(gè) Service 接收到事件之后會打印當(dāng)前的事件處理信息。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: first
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "0"

---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: second
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: third
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "2"
---

創(chuàng)建 Sequence

創(chuàng)建順序調(diào)用 first->second->third Service 的 Sequence。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: first
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: second
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: third

創(chuàng)建數(shù)據(jù)源

創(chuàng)建 CronJobSource 數(shù)據(jù)源,每隔 1 分鐘發(fā)送一條事件消息 {"message": "Hello world!"} 到 Sequence 服務(wù)。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: cronjob-source
spec:
  schedule: "*/1 * * * *"
  data: '{"message": "Hello world!"}'
  sink:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: Sequence
    name: sequence

示例結(jié)果

如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景

面向事件處理場景

事件源產(chǎn)生的事件直接發(fā)送給 Sequence 服務(wù), Sequence 接收到事件之后順序調(diào)用 Service 服務(wù)對事件進(jìn)行處理,處理之后的最終結(jié)果會調(diào)用 event-display Service 顯示: 如何分析Knative Eventing中的Sequence及其4 種使用場景

創(chuàng)建 Knative Service

同上創(chuàng)建 3 個(gè) Knative Service 用于事件處理:

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: first
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "0"

---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: second
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: third
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "2"
---

創(chuàng)建 Sequence

創(chuàng)建順序調(diào)用 first->second->third Service 的 Sequence,將處理結(jié)果通過 reply 發(fā)送給 event-display

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: first
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: second
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: third
  reply:
    kind: Service
    apiVersion: serving.knative.dev/v1alpha1
    name: event-display

創(chuàng)建結(jié)果顯示 Service

創(chuàng)建 event-display Service, 用于接收最終的結(jié)果信息。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: event-display
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d

創(chuàng)建數(shù)據(jù)源

創(chuàng)建 CronJobSource 數(shù)據(jù)源,每隔 1 分鐘發(fā)送一條事件消息 {"message": "Hello world!"} 到 Sequence 服務(wù)。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: cronjob-source
spec:
  schedule: "*/1 * * * *"
  data: '{"message": "Hello world!"}'
  sink:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: Sequence
    name: sequence

示例結(jié)果

如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景

級聯(lián) Sequence 場景

Sequence 更高級的地方還在于支持級聯(lián)處理: Sequence By Sequence,這樣可以進(jìn)行多次 Sequence 處理,滿足復(fù)雜事件處理場景需求。 如何分析Knative Eventing中的Sequence及其4 種使用場景

創(chuàng)建 Knative Service

創(chuàng)建 6 個(gè) Knative Service 用于事件處理, 前 3 個(gè)用于第 1 個(gè) Sequence,后 3 個(gè)用于第 2 個(gè) Sequence。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: first
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "0"

---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: second
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: third
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "2"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: fourth
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "3"

---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: fifth
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "4"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: sixth
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
          env:
            - name: STEP
              value: "5"
---

創(chuàng)建第 1 個(gè) Sequence

使用 first->second->third Service 用于第 1 個(gè) Sequence 調(diào)用處理,將執(zhí)行結(jié)果發(fā)送給第 2 個(gè) Sequence。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: first-sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: first
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: second
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: third
  reply:
    kind: Sequence
    apiVersion: messaging.knative.dev/v1alpha1
    name: second-sequence

創(chuàng)建第 2 個(gè) Sequence

使用 fourth->fifth->sixth Service 用于第 2 個(gè) Sequence 調(diào)用處理,將執(zhí)行結(jié)果發(fā)送給 event-display

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: second-sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: fourth
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: fifth
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: sixth
  reply:
    kind: Service
    apiVersion: serving.knative.dev/v1alpha1
    name: event-display

創(chuàng)建結(jié)果顯示 Service

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: event-display
spec:
  template:
    spec:
      containerers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d

創(chuàng)建數(shù)據(jù)源指向第 1 個(gè) Sequence

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: cronjob-source
spec:
  schedule: "*/1 * * * *"
  data: '{"message": "Hello world!"}'
  sink:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: Sequence
    name: first-sequence

示例結(jié)果

如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景

Broker/Trigger 場景

事件源 cronjobsource 向 Broker 發(fā)送事件,通過 Trigger 將這些事件發(fā)送到由 3 個(gè) Service 調(diào)用的 Sequence 中。Sequence 處理完之后將結(jié)果事件發(fā)送給 Broker,并最終由另一個(gè) Trigger 發(fā)送給 event-display Service 顯示事件結(jié)果:

如何分析Knative Eventing中的Sequence及其4 種使用場景

創(chuàng)建 Knative Service

同上創(chuàng)建 3 個(gè) Knative Service,用于 Sequence 中服務(wù)處理。

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: first
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
        env:
        - name: STEP
          value: "0"

---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: second
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
        env:
        - name: STEP
          value: "1"
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: third
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
        env:
        - name: STEP
          value: "2"

---

創(chuàng)建 Sequence

創(chuàng)建 Sequence,這里依次順序執(zhí)行 first->second->third 這 3 個(gè)服務(wù)。將最終處理的結(jié)果發(fā)送到 broker-test 中。

apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
  name: sequence
spec:
  channelTemplate:
    apiVersion: messaging.knative.dev/v1alpha1
    kind: InMemoryChannel
  steps:
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: first
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: second
    - ref:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: third
  reply:
    kind: Broker
    apiVersion: eventing.knative.dev/v1alpha1
    name: default

創(chuàng)建事件源指向 Broker

創(chuàng)建 CronjobSource,它將每隔 1 分鐘發(fā)送一條 {"message": "Hello world!"} 消息到 broker-test 中。

apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: cronjob-source
spec:
  schedule: "*/1 * * * *"
  data: '{"message": "Hello world!"}'
  sink:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Broker
    name: default

創(chuàng)建 Broker

創(chuàng)建默認(rèn) Broker:

kubectl label namespace default knative-eventing-injection=enabled

創(chuàng)建 Trigger  指向 Sequence

創(chuàng)建訂閱事件類型為 dev.knative.cronjob.event 的 Trigger, 用于 Sequence 進(jìn)行消費(fèi)處理。

apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: sequence-trigger
spec:
  filter:
    sourceAndType:
      type: dev.knative.cronjob.event
  subscriber:
    ref:
      apiVersion: messaging.knative.dev/v1alpha1
      kind: Sequence
      name: sequence

創(chuàng)建結(jié)果訂閱 Trigger

創(chuàng)建訂閱 samples.http.mod3 的事件類型 Trigger,將 Sequence 執(zhí)行的結(jié)果發(fā)送給 event-display Service 進(jìn)行顯示。

apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: display-trigger
spec:
  filter:
    sourceAndType:
      type: samples.http.mod3
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: event-display
---
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: event-display
spec:
  template:
    spec:
      containers:
        - image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d
---

<a name="4ed65929-3"></a>

示例結(jié)果

如何分析Knative Eventing中的Sequence及其4 種使用場景 如何分析Knative Eventing中的Sequence及其4 種使用場景

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI