作者 | 元毅,阿里云容器平台高级开发工程师,负责阿里云容器平台 Knative 相关工作。

导读:在实际的开发中我们经常会遇到将一条数据需要经过多次处理的场景,称为 Pipeline。那么在 Knative 中是否也提供这样的能力呢?其实从 Knative Eventing 0.7 版本开始,就提供了 Sequence CRD 资源,用于事件处理 Pipeline。本篇文章中,阿里云容器平台高级开发工程师元毅将为大家详细介绍 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 个部分:

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

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

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

直接访问 Service 场景

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

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

示例结果

面向事件处理场景

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

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

示例结果

级联 Sequence 场景

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

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

示例结果

Broker/Trigger 场景

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

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

示例结果

小结

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

欢迎加入 Knative 交流群

Knative 基本功能深入剖析:Knative Eventing 之 Sequence 介绍的更多相关文章

  1. Knative 基本功能深入剖析:Knative Serving 自动扩缩容 Autoscaler

    Knative Serving 默认情况下,提供了开箱即用的快速.基于请求的自动扩缩容功能 - Knative Pod Autoscaler(KPA).下面带你体验如何在 Knative 中玩转 Au ...

  2. Knative 基本功能深入剖析:Knative Serving 之服务路由管理

    导读:本文主要围绕 Knative Service 域名展开,介绍了 Knative Service 的路由管理.文章首先介绍了如何修改默认主域名,紧接着深入一层介绍了如何添加自定义域名以及如何根据 ...

  3. Knative 基本功能深入剖析:Knative Serving 的流量灰度和版本管理

    作者|冬岛 阿里云技术专家 本篇主要介绍 Knative Serving 的流量灰度,通过一个 rest-api 的例子演示如何创建不同的 Revision.如何在不同的 Revision 之间按照流 ...

  4. AndroidStudio制作登录和注册功能的实现,界面的布局介绍

    前言 大家好,给大家带来AndroidStudio制作登录和注册功能的实现,界面的布局介绍的概述,希望你们喜欢 每日一句: Success is connecting with the world a ...

  5. Knative 实战:如何在 Knative 中配置自定义域名及路由规则

    作者 | 元毅 阿里云智能事业群高级开发工程师 当前 Knative 中默认支持是基于域名的转发,可以通过域名模板配置后缀,但目前对于用户来说并不能指定全域名设置.另外一个问题就是基于 Path 和 ...

  6. ftp功能深度剖析 + 线程 031

    一 打印进度条 import time for i in range(20): # \r 回到行首打印内容 如果有同一行内容,那么就被抹掉了 n = '>'* i print('\r%s'%n, ...

  7. ActiveReports 9 新功能:可视化查询设计器(VQD)介绍

    在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍可视化数据查询设计器,无需手动编写任何SQL语句,主要内容如 ...

  8. 学习Struts--Chap02:Struts2框架各个功能模块和程序执行流程的介绍

    1.Struts2的系统架构: 2.架构中不同Key的作用介绍: servlet Filters:过滤器链,client的全部请求都要经过Filter链的处理. Struts Core:Struts2 ...

  9. 云原生生态周报 Vol. 13 | Forrester 发布企业级容器平台报告

    业界要闻 近日,全球知名市场调研机构 Forrester 发布首个企业级公共云容器平台报告.其中,阿里云容器服务的市场表现全球前三.中国第一,同时创造中国企业最好成绩,进入强劲表现者象限.报告显示,阿 ...

随机推荐

  1. 斗鱼刷弹幕js代码

    对于一个网络喷子(like me)来说,喷人必须高效. var script=document.createElement("script"); script.type=" ...

  2. AOD.NET实现数据库事物Transaction

    在开始介绍文章主要内容前先简单说一下事务 1.事务介绍 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库 ...

  3. 英语cartialgenous鹿茸cartialgenous单词

    鹿茸cartialgenous是雄鹿未骨化密生茸毛的幼角,主要从梅花鹿和马鹿头上采收,前者叫花鹿茸.黄毛茸,后者叫青毛茸.雄鹿到了一定年纪头上就会长角,初发时嫩如春笋,其表面上有一层纤细茸毛的嫩角就是 ...

  4. access truncate

    access int access(const char *pathname, int mode); 确定文件或文件夹的访问权限 //unistd.h #define R_OK 4 /* Test f ...

  5. Properties类按顺序输出加载内容

    Properties类按顺序输出加载内容 最近手写工厂的时候,遇到了加载配置文件时不按照properties文件中的数据的顺序来加载. 一.问题代码 import java.io.IOExceptio ...

  6. [TCP/IP] TCP流和UDP数据报之间的区别

    TCP流和UDP数据报之间的区别 1.TCP本身是面向连接的协议,S和C之间要使用TCP,必须先建立连接,数据就在该连接上流动,可以是双向的,没有边界.所以叫数据流 ,占系统资源多 2.UDP不是面向 ...

  7. yum 安装apache php mysql

    安装: yum install -y httpd php 查看版本:. rpm -qa httpd php httpd-2.2.15-54.el6.centos.x86_64 php-5.3.3-48 ...

  8. Pinctrl子系统之一了解基础概念【转】

    转自:https://blog.csdn.net/u012830148/article/details/80609337 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请 ...

  9. CF704D Captain America(上下界网络流)

    传送门 题意: 二维平面给出\(n\)个点,现在可以给每个点进行染色,染红色的代价为\(r\),染蓝色的代价为\(b\). 之后会有\(m\)个限制,形式如:\(t_i\ l_i\ d_i\),当\( ...

  10. VC 静态库与动态库(四)动态库创建与使用_显示调用

    在第三章的基础上,接着添加一个显示调用项目 显示调用项目创建: 1.给解决方案添加一个新的控制台项目DisplayCall用于测试动态库,创建完成后设置为启动项目 2.DisplayCall.cpp添 ...