第 5 章 自定义现有的 Kamelet 目录
本文档包括自定义现有 kamelet 的信息。它涉及以下阶段:
- 创建一个简单的 kamelet。
- 自定义现有 kamelet 目录
红帽不支持从头开始实施您自己的 kamelet。
5.1. 创建一个简单的 Kamelet
Apache Camel 提供超过 300 个组件,可以从一个已经可用组件中创建 Kamelet 开始。官方目录中提供的大多数 Kamelets 都是一个简单的,仅包含 Kamelet 属性重新映射到 Camel 端点参数。
请考虑以下示例,要提供一个 Kamelet,供您在 TTY 上搜索数据,从而提供有关给定关键字的信息流。要创建这种 Kamelet,您可以使用 "camel-twitter" 组件的选项,而无需任何重新调整。
要编写简单的 Kamelet,请使用 kamel CLI 开始构建一个新的 Kamelet 资源:
kamel init twitter-search-source.kamelet.yaml
这会生成类似如下的 YAML 文件:
twitter-search-source.kamelet.yaml
apiVersion: camel.apache.org/v1alpha1 kind: Kamelet metadata: name: twitter-search-source labels: camel.apache.org/kamelet.type: "source" spec: definition: title: "Timer" description: "Produces periodic events with a custom payload" required: - message properties: period: title: Period description: The time interval between two events type: integer default: 1000 message: title: Message description: The message to generate type: string types: out: mediaType: text/plain template: from: uri: timer:tick parameters: period: "{{period}}" steps: - set-body: constant: "{{message}}" - to: "kamelet:sink"
更改该文件,以创建路由,它在 Twitter 中搜索一个特定的关键字。
初始构建(timer-to-log)中提供的路由不准确,因此将其改为以下内容:
twitter-search-source.kamelet.yaml
apiVersion: camel.apache.org/v1alpha1 kind: Kamelet # ... spec: # ... template: from: uri: "twitter-search:{{keywords}}" (1) parameters: accessToken: "{{accessToken}}" (2) accessTokenSecret: "{{accessTokenSecret}}" consumerKey: "{{apiKey}}" (3) consumerSecret: "{{apiKeySecret}}" steps: - marshal: (4) json: {} - to: "kamelet:sink" (5)
- 关键字 是 Camel Twitter-search 中的路径参数。有些端点参数仅映射 1-1。Camel 组件 consumerKey 名为 apiKey,以反映 Twitter 开发人员门户中的实际名称。
- Camel Twitter 组件生成 Java 对象,您必须将它们放入 JSON。Source Kamelet 将数据发送到特殊端点 "kamelet:sink",该端点由不同的目标在运行时替代。
上面的 YAML 路由模板使用 twitter-search 组件在 Twitter 中搜索。在 JSON 中添加了 marshaling 步骤,因为 Kamelet 的输出必须具有通过线传输的值。要完成 Kamelet,您必须以 JSON 模式格式记录参数。它在 spec
定义 部分中指定: twitter-search-source.kamelet.yaml
apiVersion: camel.apache.org/v1alpha1 kind: Kamelet metadata: name: twitter-search-source # ... spec: definition: title: "Twitter Search Source" (1) description: |- Allows to get all tweets on particular keywords from Twitter. It requires tokens that can be obtained by creating an application in the Twitter developer portal: https://developer.twitter.com/. required: (2) - keywords - apiKey - apiKeySecret - accessToken - accessTokenSecret properties: keywords: (3) title: Keywords description: The keywords to use in the Twitter search (Supports Twitter standard operators) type: string example: "Apache Camel" apiKey: title: API Key description: The API Key from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password (4) apiKeySecret: title: API Key Secret description: The API Key Secret from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password accessToken: title: Access Token description: The Access Token from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password accessTokenSecret: title: Access Token Secret description: The Access Token Secret from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password # ...
有关 textual 格式的 Kamelet 的常规信息:
- 创建 Kamelet 所需的参数列表。
- 每个参数的规格(flat 结构,不允许嵌套选项)
特定 UI (OpenShift 控制台)的可选图形自定义
最后的 Kamelet 类似如下:
twitter-search-source.kamelet.yaml
apiVersion: camel.apache.org/v1alpha1 kind: Kamelet metadata: name: twitter-search-source annotations: camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,..." # Truncated camel.apache.org/provider: "Apache Software Foundation" labels: camel.apache.org/kamelet.type: "source" camel.apache.org/kamelet.group: "Twitter" spec: definition: title: "Twitter Search Source" description: |- Allows to get all tweets on particular keywords from Twitter. It requires tokens that can be obtained by creating an application in the Twitter developer portal: https://developer.twitter.com/. required: - keywords - apiKey - apiKeySecret - accessToken - accessTokenSecret properties: keywords: title: Keywords description: The keywords to use in the Twitter search (Supports Twitter standard operators) type: string example: "Apache Camel" apiKey: title: API Key description: The API Key from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password apiKeySecret: title: API Key Secret description: The API Key Secret from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password accessToken: title: Access Token description: The Access Token from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password accessTokenSecret: title: Access Token Secret description: The Access Token Secret from the Twitter application in the developer portal type: string format: password x-descriptors: - urn:alm:descriptor:com.tectonic.ui:password types: out: mediaType: application/json template: from: uri: "twitter-search:{{keywords}}" parameters: accessToken: "{{accessToken}}" accessTokenSecret: "{{accessTokenSecret}}" consumerKey: "{{apiKey}}" consumerSecret: "{{apiKeySecret}}" steps: - marshal: json: {} - to: "kamelet:sink"
您可以使用 Catalog 上共享的 Kamelet,并在 OpenShift 集群上创建。
例如:
与简单的绑定一起应用到集群。
- 您必须启用 Openshift 集群,并连接到一个命名空间,以便 camel K operator 可以正常工作。
创建 Kamelet。
kubectl apply -f twitter-search-source.kamelet.yaml
如以下示例所示,创建一个绑定:
twitter-search-source-binding.yaml`
apiVersion: camel.apache.org/v1alpha1 kind: KameletBinding metadata: name: twitter-search-source-binding spec: source: ref: kind: Kamelet apiVersion: camel.apache.org/v1alpha1 name: twitter-search-source properties: keywords: "Apache Camel" apiKey: "your own" apiKeySecret: "your own" accessToken: "your own" accessTokenSecret: "your own" sink: uri: "log:info"
这可以使用以下方法创建:
kubectl apply -f twitter-search-source-binding.yaml
创建后,您可以使用以下方法查看绑定的日志:
kamel logs twitter-search-source-binding
- 执行上述步骤后,在创建集成后,必须在日志中得到一些调整。
- 有关如何在不同上下文(如 Knative、Kafka 等)中使用它的更多信息,请参阅 Kamelets 用户指南。