Sequeue 解析

本文涉及的产品
应用实时监控服务ARMS - 应用监控,每月50GB免费额度
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
函数计算FC,每月15万CU 3个月
简介: 在实际的开发中我们会经常遇到将一条数据需要经过多次处理的场景,称为 Pipeline。那么在 Knative 中是否也提供这样的能力呢?其实从 Knative Eventing 0.7 版本开始,提供了 Sequence CRD 资源,用于事件处理 Pipeline。下面我们介绍一下 Sequence。

本文选自《Knative 云原生应用开发指南》
knative海报.png
更多云原生技术资讯可关注阿里巴巴云原生技术圈

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个部分:

  1. steps: 在 step 中定义了按照顺序执行的服务,每个服务会对应创建 Subscription
  2. channelTemplate:指定了使用具体的那个 Channel
  3. reply:(可选)定义了将最后一个 step 服务结果转发到的目标服务

Sequence 都是适合哪些具体应用场景呢?我们上面也提到了事件处理的 Pipeline。那么在实际场景应用中究竟以什么样的形式体现呢? 现在我们揭晓一下 Sequence 在 Knative Eventing 中提供的如下 4 种使用场景:

  • 直接访问 Service
  • 面向事件处理
  • 级联 Sequence
  • 面向 Broker/Trigger

直接访问 Service 场景

事件源产生的事件直接发送给 Sequence 服务, Sequence 接收到事件之后顺序调用 Service 服务对事件进行处理:
20.png

创建 Knative Service

这里我们创建 3 个 Knative Service 用于事件处理。每个 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"
---

创建 Sequence

创建顺序调用 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

创建数据源

创建 CronJobSource 数据源,每隔 1 分钟发送一条事件消息{"message": "Hello world!"}到 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: sequence

示例结果

21.png22.png
23.png
24.png

面向事件处理场景

事件源产生的事件直接发送给 Sequence 服务, Sequence 接收到事件之后顺序调用 Service 服务对事件进行处理,处理之后的最终结果会调用event-display Service 显示:
25.png

创建 Knative Service

同上创建 3 个 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"
---

创建 Sequence

创建顺序调用 first->second->third Service 的 Sequence,将处理结果通过reply发送给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

创建结果显示 Service

创建event-display Service, 用于接收最终的结果信息。

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

创建数据源

创建 CronJobSource 数据源,每隔 1 分钟发送一条事件消息{"message": "Hello world!"}到 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: sequence

示例结果

26.png
27.png

级联 Sequence 场景

Sequence 更高级的地方还在于支持级联处理: Sequence By Sequence,这样可以进行多次 Sequence 处理,满足复杂事件处理场景需求:
28.png

创建 Knative Service

创建 6 个 Knative Service 用于事件处理, 前 3 个用于第 1 个 Sequence,后 3 个用于第 2 个 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"
---

创建第 1 个 Sequence

使用 first->second->third Service 用于第 1 个 Sequence 调用处理,将执行结果发送给第 2 个 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

创建第 2 个 Sequence

使用 fourth->fifth->sixth Service 用于第 2 个 Sequence 调用处理,将执行结果发送给 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

创建结果显示 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

创建数据源指向第 1 个 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

示例结果

29.png
30.png

Broker/Trigger 场景

事件源 cronjobsource 向 Broker 发送事件,通过Trigger 将这些事件发送到由 3 个 Service 调用的 Sequence 中。Sequence 处理完之后将结果事件发送给 Broker,并最终由另一个Trigger发送给 event-display Service 显示事件结果:
31.png

创建 Knative Service

同上创建 3 个 Knative Service,用于 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"

---

创建 Sequence

创建 Sequence,这里依次顺序执行first->second->third这3个服务。将最终处理的结果发送到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

创建事件源指向 Broker

创建 CronjobSource,它将每隔 1 分钟发送一条{"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

创建 Broker

创建默认 Broker

kubectl label namespace default knative-eventing-injection=enabled

创建 Trigger  指向 Sequence

创建订阅事件类型为dev.knative.cronjob.event的 Trigger, 用于 Sequence 进行消费处理。

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

创建结果订阅 Trigger

创建订阅 samples.http.mod3 的事件类型 Trigger,将 Sequence 执行的结果发送给event-display Service 进行显示。

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
---

示例结果

32.png
33.png

总结

以上介绍了什么是 Sequence, 以及基于 Sequence 的 4 种使用场景,我们可以根据实际需求选择不同的使用场景,从而实现事件处理 Pipeline。这对于需要多步骤处理事件的场景尤为适合。

阿里巴巴云原生关注微服务、Serverless、容器、Service Mesh 等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,做最懂云原生开发者的技术圈。”

相关文章
|
2月前
|
Java
XPath解析(二)
XPath解析(二)
|
2月前
|
XML 数据格式
XPath解析(一)
XPath解析(一)
|
6月前
|
算法 Java 大数据
【揭秘】RecursiveAction全面解析
RecursiveAction是Java中一个强大的工具,它允许将复杂任务分解为更小的子任务,这些子任务可以并行执行,从而提高整体性能,其主要优点在于能够有效地利用多核处理器,减少任务执行时间,并简化并行编程的复杂性。
【揭秘】RecursiveAction全面解析
|
6月前
|
并行计算 算法 Java
【揭秘】ForkJoinTask全面解析
ForkJoinTask的显著优点在于其高效的并行处理能力,它能够将复杂任务拆分成多个子任务,并利用多核处理器同时执行,从而显著提升计算性能,此外,ForkJoinTask还提供了简洁的API和强大的任务管理机制,使得开发者能够更轻松地编写并行化代码,高效地利用系统资源。
【揭秘】ForkJoinTask全面解析
|
11月前
VIN解析
VIN解析
100 1
|
存储 算法
Manacher算法解析
Manacher算法解析
87 0
|
编解码
QLED与UHD有何不同?全面解析
QLED和UHD电视几乎是一样的。作为一项先进技术,QLED 已被修改为 4k 分辨率及更高分辨率,因此它们显示为 UHD 显示器。因此,UHD 和 QLED 并不是相互竞争的术语。你应该为你的家买什么取决于你的预算和喜好。
|
自然语言处理 JavaScript 前端开发
这个this的解析
首先得理解this是在函数被调用的时候绑定的,完全取决于函数的调用位置。这与静态作用域相反,反而有点类似动态作用域(由运行时决定)。this在运行时才进行对象绑定。 其次,就是我们常见的this指向问题,即this的绑定。
171 0
|
存储 数据安全/隐私保护
GPUImage 解析
GPUImage解析(一) —— 基本概览(一)GPUImage解析(二) —— 基本概览(二)GPUImage解析(三) —— 基本概览(三)GPUImage解析(四) —— 安装方法及框架介绍GPUImage解析(五) —— 框架中的几个基类GPU...
1036 0