目录
序言
1.Parallel介绍
1.1 Parallel Spec
编辑
2.Sequence
2.1.Sequence Spec
2.2适用场景
2.3 Broker/Trigger
2.4 代码示例
3.投票
序言
三言两语,不如细心探索。
今天整理了一下Eventing 相关知识点
- Parallel
- Sequence
希望此文,能帮助读者对Knative Eventing 的这两个知识点有一个初步的了解
文章标记颜色说明:
- 黄色:重要标题
- 红色:用来标记结论
- 绿色:用来标记一级论点
- 蓝色:用来标记二级论点
1.Parallel介绍
学习Parallel 的时候,看了很多资料,其中觉得比较全面的是:
Flows - Parallel - 《Knative v1.8 Documentation》
Parallel CRD provides a way to easily define a list of branches,
each receiving the same CloudEvent sent to the Parallel ingress channel.
Typically, each branch consists of a filter function guarding the execution of the branch.
Parallel creates
Channel
s andSubscription
s under the hood.
总结下来,就是支持根据不同的过滤条件对事件进行选择处理
1.1 Parallel Spec
Parallel 资源定义,典型的 Parallel Spec描述如下:
apiVersion: messaging.knative.dev/v1alpha1
kind: Parallel
metadata:
name: me-odd-even-parallel
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1alpha1
kind: InMemoryChannel
cases:
- filter:
uri: "http://me-even-odd-switcher.default.svc.cluster.local/0"
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: me-even-transformer
- filter:
uri: "http://me-even-odd-switcher.default.svc.cluster.local/1"
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: me-odd-transformer
reply:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: me-event-display
主要包括3个部分:
channelTemplate:
定义了当前 Parallel 中使用的Channel类型
cases
:定义了一系列 filter 和 subscriber。对于每个条件分支:
filter:
首先判断filter
, 当返回事件时,调用 subscriber。filter和subscriber要求都是可访问的。- subscriber: 执行返回的事件会发生到 reply。如果 reply 为空,则发送到
spec.reply
reply
:定义了全局响应的目标函数。为可选配置其架构流程如下图
2.Sequence
主要学习了该文章,感谢作者大大:
Knative Eventing 之 Sequence 介绍
Sequence 是Knative Eventing中提供了一个资源模型,主要是为了支持对一个事件进行分步骤多次处理的场景。简单说,用于事件Pipeline处理。概括下来,有以下2点:
- 一种资源模型
- 事件Pipeline处理
2.1.Sequence Spec
Sequence Spec 的demo示例
apiVersion: messaging.knative.dev/v1alpha1
kind: Sequence
metadata:
name: sequencetest
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1alpha1
kind: InMemoryChannel
steps:
- ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: sequencetest1
- ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: sequencetest2
- ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: sequencetest3
reply:
kind: Broker
apiVersion: eventing.knative.dev/v1alpha1
name: sequencetest4
从定义中可以看出:
Sequence Spec包括3个部分:
- channelTemplate:指定了具体使用的Channel
- steps: 定义了按照顺序执行的服务,而且每个服务会对应创建Subscription。这里定义了三个步骤。每个步骤执行不同的服务
- reply:定义了最后一个step返回结果的响应目标,这一步骤是可选
2.2适用场景
Sequence 在 Knative Eventing 中提供的如下 4 种使用场景:
Broker/Trigger
级联 Sequence
面向事件处理
直接访问 Service
2.3 Broker/Trigger
该类型是最复杂的一个场景:
事件源 cronjobsource 向 Broker 发送事件,通过 Trigger 将这些事件发送到由 多个 Service 调用的 Sequence 中。
Sequence 处理完之后将结果事件发送给 Broker,并最终由另一个 Trigger 发送给
event-display
Service 显示事件结果。如下图所示:
2.4 代码示例
主要分为以下几步骤,分别创建:
- knative Service
- Sequence
- Broker
- 创建事件源
- Trigger 指向 Sequence
- 结果订阅 Trigger
1.创建knative Service
创建 3 个 Knative Service,用于 Sequence 中服务处理。
--- 第一个service -one
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: one
spec:
template:
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
env:
- name: STEP
value: "1"
--- 第二个service - two
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: two
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: three
spec:
template:
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700
env:
- name: STEP
value: "3"
2.创建 Sequence
创建 Sequence,依次顺序执行
one->two->three
这 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: one
- ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: two
- ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: three
reply:
kind: Broker
apiVersion: eventing.knative.dev/v1alpha1
name: broker-test
3.创建broker
创建默认的broker
kubectl label namespace default knative-eventing-injection=enabled
4.创建事件源
创建事件源指向 Broker
创建 CronjobSource,它将每隔 1 分钟发送一条
{"message": "Hello Knative Sequence!"}
消息到 broker-test 中。
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
name: cronjob-source
spec:
schedule: "*/1 * * * *"
data: '{"message": "Hello Knative Sequence!"}'
sink:
apiVersion: eventing.knative.dev/v1alpha1
kind: Broker
name: broker-test
5.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
6.结果订阅 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
---
参考:
Flows - Parallel - 《Knative v1.8 Documentation》
Knative Sequence 介绍