序列连接到另一个序列¶
我们将创建以下逻辑配置。我们创建一个 PingSource,将事件馈送到一个 Sequence,然后获取该 Sequence 的输出并将其发送到第二个 Sequence,最后显示结果输出。

这些示例中使用的函数位于 https://github.com/knative/eventing/blob/main/cmd/appender/main.go。
先决条件¶
对于此示例,我们假设您已经设置了 InMemoryChannel 以及 Knative Serving(用于我们的函数)。示例使用 default 命名空间,同样,如果您想部署到其他命名空间,您需要修改示例以反映这一点。
如果您想使用不同类型的 Channel,您将不得不修改 Sequence.Spec.ChannelTemplate 以创建适当的 Channel 资源。
设置¶
创建 Knative 服务¶
将以下命令中的 default 更改为您要创建资源的命名空间中的步骤
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: first
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 0"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: second
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 1"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: third
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 2"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: fourth
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 3"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: fifth
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 4"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: sixth
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/appender
env:
- name: MESSAGE
value: " - Handled by 5"
---
kubectl -n default create -f ./steps.yaml
创建第一个序列¶
sequence1.yaml 文件包含创建 Sequence 的规范。如果您使用不同类型的 Channel,则需要更改 spec.channelTemplate 以指向您想要的 Channel。
apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
name: first-sequence
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
steps:
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: first
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: second
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: third
reply:
ref:
kind: Sequence
apiVersion: flows.knative.dev/v1
name: second-sequence
将以下命令中的 default 更改为您要创建资源的命名空间中的 Sequence
kubectl -n default create -f ./sequence1.yaml
创建第二个序列¶
sequence2.yaml 文件包含创建 Sequence 的规范。如果您使用不同类型的 Channel,则需要更改 spec.channelTemplate 以指向您想要的 Channel。
apiVersion: flows.knative.dev/v1
kind: Sequence
metadata:
name: second-sequence
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
steps:
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: fourth
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: fifth
- ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: sixth
reply:
ref:
kind: Service
apiVersion: serving.knative.dev/v1
name: event-display
kubectl -n default create -f ./sequence2.yaml
创建显示序列创建的事件的服务¶
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display
spec:
template:
spec:
containers:
- image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
将以下命令中的 default 更改为您要创建资源的命名空间中的 Sequence
kubectl -n default create -f ./event-display.yaml
创建指向第一个序列的 PingSource¶
这将创建一个 PingSource,它将每 2 分钟发送一个 CloudEvent,数据负载为 {"message": "Hello world!"}。
apiVersion: sources.knative.dev/v1
kind: PingSource
metadata:
name: ping-source
spec:
schedule: "*/2 * * * *"
contentType: "application/json"
data: '{"message": "Hello world!"}'
sink:
ref:
apiVersion: flows.knative.dev/v1
kind: Sequence
name: first-sequence
kubectl -n default create -f ./ping-source.yaml
检查结果¶
您现在可以通过检查事件显示 Pod 的日志来查看最终输出。
kubectl -n default get pods
然后查看事件显示 Pod 的日志
kubectl -n default logs -l serving.knative.dev/service=event-display -c user-container --tail=-1
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 1.0
type: dev.knative.sources.ping
source: /apis/v1/namespaces/default/pingsources/ping-source
id: 29d531df-78d8-4d11-9ffd-ba24045241a9
time: 2020-03-02T21:18:00.0011708Z
datacontenttype: application/json
Extensions,
knativehistory: first-sequence-kn-sequence-0-kn-channel.default.svc.cluster.local; first-sequence-kn-sequence-1-kn-channel.default.svc.cluster.local; first-sequence-kn-sequence-2-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-0-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-1-kn-channel.default.svc.cluster.local; second-sequence-kn-sequence-2-kn-channel.default.svc.cluster.local
traceparent: 00-e5abc9de525a89ead80560b8f328de5c-fc12b64a6296f541-00
Data,
{
"id": 0,
"message": "Hello world! - Handled by 0 - Handled by 1 - Handled by 2 - Handled by 3 - Handled by 4 - Handled by 5"
}
您可以看到初始的 PingSource 消息 ("Hello World!") 已被 Sequence 中的每个步骤附加到其中。