Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Sequence in Knative Eventing and its four usage scenarios

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze Sequence in Knative Eventing and its four usage scenarios, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Introduction: in actual development, we often encounter scenarios where a piece of data needs to be processed many times, called Pipeline. So is this capability also available in Knative? In fact, since Knative Eventing version 0.7, Sequence CRD resources have been provided for event handling Pipeline. The following is a detailed introduction to the definition of Sequence and the four usage scenarios provided in Knative Eventing.

Sequence definition

Let's first take a look at the Sequence Spec definition:

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: testspec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: InMemoryChannel steps:-ref: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: test reply: kind: Broker apiVersion: eventing.knative.dev/v1alpha1 name: test

Sequence Spec consists of three parts:

Steps: services executed sequentially are defined in step, and a Subscription is created for each service.

ChannelTemplate: specifies which Channel to use

Reply: (optional) defines the target service to which the last step service result is forwarded.

What specific application scenarios are Sequence suitable for? We also mentioned Pipeline for event handling above. So what kind of form is it reflected in the actual scene application? Now let's reveal the following four usage scenarios provided by Sequence in Knative Eventing:

Direct access to Service

Event-oriented processing

Cascaded Sequence

For Broker/Trigger.

Direct access to Service scenarios

The events generated by the event source are sent directly to the Sequence service. After receiving the event, the Sequence calls the Service service sequentially to handle the event:

Create Knative Service

Here we create three Knative Service for event handling. After each Service receives the event, it prints the current event handling information.

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: firstspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "0"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: secondspec: template: spec: containers:- Image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "1"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: thirdspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:- Name: STEP value: "2"-create Sequence

Create a Sequence that calls first- > second- > third Service sequentially.

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: sequencespec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: 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 create data source

Create a CronJobSource data source and send an event message {"message": "Hello world!"} to the Sequence service every 1 minute.

ApiVersion: sources.eventing.knative.dev/v1alpha1kind: CronJobSourcemetadata: name: cronjob-sourcespec: schedule: "* / 1 *" data:'{"message": "Hello world!"} 'sink: apiVersion: messaging.knative.dev/v1alpha1 kind: Sequence name: sequence sample result

Event-oriented processing scenario

The event generated by the event source is sent directly to the Sequence service. After receiving the event, the Sequence calls the Service service sequentially to handle the event, and the final result is displayed by calling event-display Service:

Create Knative Service

Create 3 Knative Service for event handling as above:

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: firstspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "0"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: secondspec: template: spec: containers:- Image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "1"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: thirdspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:- Name: STEP value: "2"-create Sequence

Create the Sequence that calls first- > second- > third Service sequentially, and send the processing result to event-display through reply:

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: sequencespec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: 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 creation results display Service

Create an event-display Service to receive the final result information.

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: event-displayspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d create data source

Create a CronJobSource data source and send an event message {"message": "Hello world!"} to the Sequence service every 1 minute.

ApiVersion: sources.eventing.knative.dev/v1alpha1kind: CronJobSourcemetadata: name: cronjob-sourcespec: schedule: "* / 1 *" data:'{"message": "Hello world!"} 'sink: apiVersion: messaging.knative.dev/v1alpha1 kind: Sequence name: sequence sample result

Cascading Sequence scenarios

The more advanced aspect of Sequence is that it supports cascading processing: Sequence By Sequence, which allows multiple Sequence processing to meet the needs of complex event processing scenarios.

Create Knative Service

Create six Knative Service for event handling, the first three for the first Sequence and the last three for the second Sequence.

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: firstspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "0"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: secondspec: template: spec: containers:- Image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "1"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: thirdspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:- Name: STEP value: "2"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: fourthspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "3"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: fifthspec : template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "4"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: sixthspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/ Probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "5"-create the 1st Sequence

Use first- > second- > third Service for the first Sequence call processing, sending the execution result to the second Sequence.

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: first-sequencespec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: 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 create the second Sequence

Use fourth- > fifth- > sixth Service for the second Sequence call processing, sending the execution result to event-display.

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: second-sequencespec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: 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: ServiceapiVersion: serving.knative.dev/v1alpha1 name: event-display creation result shows that ServiceapiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: event-displayspec: template: spec: containerers:-image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d creates a data source pointing to the 1st SequenceapiVersion: sources.eventing.knative.dev/v1alpha1kind: CronJobSourcemetadata: Name: cronjob-sourcespec: schedule: "* / 1 *" data:'{"message": "Hello world!"} 'sink: apiVersion: messaging.knative.dev/v1alpha1 kind: Sequence name: first-sequence sample result

Broker/Trigger scene

The event source cronjobsource sends events to the Broker and sends them through Trigger to the Sequence called by the three Service. After Sequence has finished processing, the resulting event is sent to Broker, and finally another Trigger is sent to event-display Service to display the event result:

Create Knative Service

Create 3 Knative Service as above for service processing in Sequence.

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: firstspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "0"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: secondspec: template: spec: containers:-image: registry.cn-hangzhou .aliyuncs.com / knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "1"-apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: thirdspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/probable-summer:2656f39a7fcb6afd9fc79e7a4e215d14d651dc674f38020d1d18c6f04b220700 env:-name: STEP value: "2"-- create Sequence

Create a Sequence, where the three services first- > second- > third are executed sequentially. The result of the final processing is sent to broker-test.

ApiVersion: messaging.knative.dev/v1alpha1kind: Sequencemetadata: name: sequencespec: channelTemplate: apiVersion: messaging.knative.dev/v1alpha1kind: 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 create event source pointing to Broker

Create a CronjobSource that sends a {"message": "Hello world!"} message to broker-test every 1 minute.

ApiVersion: sources.eventing.knative.dev/v1alpha1kind: CronJobSourcemetadata: name: cronjob-sourcespec: schedule: "* / 1 *" data:'{"message": "Hello world!"} 'sink: apiVersion: eventing.knative.dev/v1alpha1kind: Broker name: default create Broker

Create a default Broker:

Kubectl label namespace default knative-eventing-injection=enabled creates Trigger to point to Sequence

Create a Trigger with a subscription event type of dev.knative.cronjob.event, which is used by Sequence for consumption processing.

ApiVersion: eventing.knative.dev/v1alpha1kind: Triggermetadata: name: sequence-triggerspec: filter: sourceAndType: type: dev.knative.cronjob.event subscriber: ref: apiVersion: messaging.knative.dev/v1alpha1 kind: Sequence name: sequence create result subscription Trigger

Create an event type Trigger that subscribes to samples.http.mod3, and send the results executed by Sequence to event-display Service for display.

ApiVersion: eventing.knative.dev/v1alpha1kind: Triggermetadata: name: display-triggerspec: filter: sourceAndType: type: samples.http.mod3 subscriber: ref: apiVersion: serving.knative.dev/v1alpha1kind: Service name: event-display---apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: event-displayspec: template: spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-release/event_display:bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d

Sample result

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report