3.3. 在 Kamelet Binding 中将数据源连接到 Knative 目的地

要将数据源连接到 Knative 目标(channel 或 broker),您需要创建一个 Kamelet Binding,如图 3.2 所示

Connecting a data source to a Knative destination

图 3.2 将数据源连接到 Knative 目的地

Knative 目的地可以是 Knative 频道或 Knative 代理。

当您将数据发送到某个频道时,该频道只有一个事件类型。您不需要在 Kamelet Binding 中为频道指定任何属性值。

当您将数据发送到代理时,因为代理可以处理多个事件类型,所以您必须在 Kamelet Binding 中引用代理时为 type 属性指定一个值。

先决条件

  • 您知道要将事件发送到的 Knative 频道或代理的名称和类型。

    此流程中的示例使用名为 mychannelInMemoryChannel 频道,或名为 default 的代理。对于代理示例,c offee 事件类型 属性值是 coffee

  • 您知道,您想要添加到 Camel 集成和所需的实例参数中的 Kamelet。

    此流程中的 Kamelet 示例是 coffee-source Kamelet。它有一个可选参数 句点,用于指定发送每个事件的频率。您可以将 Example source Kamelet 中的代码复制到名为 coffee-source.kamelet.yaml 文件的文件,然后运行以下命令来将其添加为命名空间的资源:

    oc apply -f coffee-source.kamelet.yaml

流程

要将数据源连接到 Knative 目的地,请创建一个 Kamelet Binding:

  1. 在您选择的编辑器中,创建一个具有以下基本结构的 YAML 文件:

    apiVersion: camel.apache.org/v1alpha1
    kind: KameletBinding
    metadata:
      name:
    spec:
      source:
      sink:
  2. 为 Kamelet Binding 添加一个名称。在本例中,名称是 coffees-to-knative,因为绑定将 coffee-source Kamelet 连接到一个 Knative 目的地。

    apiVersion: camel.apache.org/v1alpha1
    kind: KameletBinding
    metadata:
      name: coffees-to-knative
    spec:
      source:
      sink:
  3. 对于 Kamelet Binding 的源,请指定数据源 Kamelet(例如,coff ee-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:
  4. 对于 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 代理指定为 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:
            kind: Broker
            apiVersion: eventing.knative.dev/v1
            name: default
          properties:
            type: coffee
  5. 保存 YAML 文件(例如,coffe es-to-knative.yaml)。
  6. 登录您的 OpenShift 项目。
  7. 将 Kamelet Binding 作为资源添加到 OpenShift 命名空间:

    oc apply -f <kamelet binding filename>

    例如:

    oc apply -f coffees-to-knative.yaml

    Camel K operator 使用 KameletBinding 资源生成并运行 Camel K 集成。构建可能需要几分钟时间。

  8. 查看 KameletBinding 的状态:

    oc get kameletbindings

  9. 查看其集成的状态:

    oc get integrations

  10. 查看集成的日志:

    kamel logs <integration> -n <project>

    例如:

    kamel logs coffees-to-knative -n my-camel-knative