3.3. 将数据源连接到 Kamelet Binding 中的 Knative 目的地
要将数据源连接到 Knative 目标(频道或代理),您可以创建一个 Kamelet Binding,如图 3.2 所示。
图 3.2 将数据源连接到 Knative 目的地
Knative 目的地可以是 Knative 频道或 Knative 代理。
当您向频道发送数据时,该频道只有一个事件类型。您不需要在 Kamelet Binding 中为频道指定任何属性值。
将数据发送到代理时,因为代理可以处理多个事件类型,您必须在 Kamelet Binding 中引用代理时为 type 属性指定一个值。
先决条件
您知道您要将事件发送到的 Knative 频道或代理的名称和类型。
此流程中的示例使用名为
mychannel
的InMemoryChannel
频道或名为default
的代理。对于代理示例,对于 coffee 事件,type
属性值为coffee
。您知道您要添加到 Camel 集成中的 Kamelet 和所需的实例参数。
此流程的 Kamelet 示例是
coffee-source
Kamelet。它有一个可选参数,
用于指定发送每个事件的频率。您可以将代码从 Example source Kamelet 复制到名为coffee-source.kamelet.yaml
文件的文件,然后运行以下命令将其作为资源添加到命名空间中:oc apply -f coffee-source.kamelet.yaml
流程
要将数据源连接到 Knative 目的地,请创建一个 Kamelet Binding:
在您选择的编辑器中,使用以下基本结构创建一个 YAML 文件:
apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: spec: source: sink:
为 Kamelet Binding 添加名称。在本例中,名称是
coffees-to-knative
,因为绑定将coffee-source
Kamelet 连接到 Knative 目的地。apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: coffees-to-knative spec: source: sink:
对于 Kamelet Binding 的源,指定一个数据源 Kamelet (例如,
coffee-source
Kamelet)生成事件,其中包含有关 coffee 的数据,并为 Kamelet 配置任何参数。apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: coffees-to-knative spec: source: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 name: coffee-source properties: period: 5000 sink:
对于 Kamelet Binding 的 sink,请指定 Knative 频道或代理以及所需的参数。
这个示例将 Knative 频道指定为 sink:
apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: coffees-to-knative spec: source: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 name: coffee-source properties: period: 5000 sink: ref: apiVersion: messaging.knative.dev/v1 kind: InMemoryChannel name: mychannel
本例将 Knative 代理指定为接收器:
apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: coffees-to-knative spec: source: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 name: coffee-source properties: period: 5000 sink: ref: kind: Broker apiVersion: eventing.knative.dev/v1 name: default properties: type: coffee
-
保存 YAML 文件(例如,
coffees-to-knative.yaml
)。 - 登录您的 OpenShift 项目。
将 Kamelet Binding 作为资源添加到 OpenShift 命名空间中:
oc apply -f <kamelet binding filename>
例如:
oc apply -f coffees-to-knative.yaml
Camel K operator 使用
KameletBinding
资源生成并运行 Camel K 集成。构建可能需要几分钟时间。查看
KameletBinding
的状态:oc get kameletbindings
查看其集成的状态:
oc get integrations
查看集成的日志:
kamel logs <integration> -n <project>
例如:
kamel logs coffees-to-knative -n my-camel-knative
另请参阅