重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如何分析Knative Eventing中的Sequence及其4 种使用场景,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联主营和平网站建设的网络公司,主营网站建设方案,APP应用开发,和平h5成都微信小程序搭建,和平网站营销推广欢迎和平等地区企业咨询
导读:在实际的开发中我们经常会遇到将一条数据需要经过多次处理的场景,称为 Pipeline。那么在 Knative 中是否也提供这样的能力呢?其实从 Knative Eventing 0.7 版本开始,就提供了 Sequence CRD 资源,用于事件处理 Pipeline。下面将为大家详细介绍 Sequence 的定义及在 Knative Eventing 中提供的 4 种使用场景。
首先来看一下 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 个部分:
steps: 在 step 中定义了按照顺序执行的服务,每个服务会对应创建 Subscription;
channelTemplate:指定了使用具体的那个 Channel;
reply:(可选)定义了将最后一个 step 服务结果转发到的目标服务。
Sequence 都是适合哪些具体应用场景呢?我们上面也提到了事件处理的 Pipeline。那么在实际场景应用中究竟以什么样的形式体现呢? 现在我们揭晓一下 Sequence 在 Knative Eventing 中提供的如下 4 种使用场景:
直接访问 Service;
面向事件处理;
级联 Sequence;
面向 Broker/Trigger。
事件源产生的事件直接发送给 Sequence 服务, Sequence 接收到事件之后顺序调用 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" ---
创建顺序调用 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 显示:
同上创建 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" ---
创建顺序调用 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
创建 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 By Sequence,这样可以进行多次 Sequence 处理,满足复杂事件处理场景需求。
创建 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" ---
使用 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
使用 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
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
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
事件源 cronjobsource 向 Broker 发送事件,通过 Trigger 将这些事件发送到由 3 个 Service 调用的 Sequence 中。Sequence 处理完之后将结果事件发送给 Broker,并最终由另一个 Trigger 发送给 event-display
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,这里依次顺序执行 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
创建 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:
kubectl label namespace default knative-eventing-injection=enabled
创建订阅事件类型为 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
创建订阅 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 ---
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。