Camel K 入门


Red Hat Integration 2023.q1

开发并运行您的第一个 Camel K 应用程序

摘要

如何安装 Camel K、设置您的开发环境并运行示例应用程序。

前言

使开源包含更多

红帽承诺替换我们的代码、文档和网页属性中存在问题的语言。我们从这四个术语开始: master、slave、blacklist 和 whitelist。这些更改将在即将发行的几个发行本中逐渐实施。有关更多详情,请参阅我们的首席技术官 Chris Wright 提供的消息

第 1 章 Camel K 简介

本章介绍了 Red Hat Integration - Camel K 提供的概念、功能和云原生架构:

1.1. Camel K 概述

Red Hat Integration - Camel K 是一个从 Apache Camel K 构建的轻量级集成框架,它在 OpenShift 的云中原生运行。Camel K 是专为无服务器和微服务架构而设计的。您可以使用 Camel K 立即在 OpenShift 上运行使用 Camel 域特定语言(DSL)编写的集成代码。Camel K 是 Apache Camel 开源社区的子项目 :https://github.com/apache/camel-k。

Camel K 采用 Go 编程语言实施,并使用 Kubernetes Operator SDK 在云中自动部署集成。例如,这包括在 OpenShift 上自动创建服务和路由。这在在云中部署和重新部署集成时(如几秒钟或更短的时间),这可以更快地进行解决。

Camel K 运行时提供了显著的性能优化。Quarkus 云原生 Java 框架默认启用,以更快地启动时间,并降低内存和 CPU 占用。在开发人员模式下运行 Camel K 时,您可以对集成 DSL 进行实时更新,并在 OpenShift 的云中立即查看结果,而无需等待您的集成重新部署。

将 Camel K 与 OpenShift Serverless 和 Knative Serving 搭配使用,仅根据需要创建容器,并在负载下自动缩放为零。这通过删除服务器配置和维护开销来降低成本,并可让您专注于应用程序开发。

将 Camel K 与 OpenShift Serverless 和 Knative Eventing 一起使用,您可以管理系统中的组件如何在无服务器应用程序的事件驱动的架构中进行通信。这通过使用发布订阅或事件流模型在事件制作者和消费者间分离的关系提供了灵活性。

1.2. Camel K 功能

Camel K 包括以下主要平台和特性:

1.2.1. 平台和组件版本

  • OpenShift Container Platform 4.6, 4.9, 4.10, 4.11
  • OpenShift Serverless 1.27.0
  • Red Hat build of Quarkus 2.7.6
  • Red Hat Camel Extensions for Quarkus 2.7.1
  • Apache Camel K 1.8.0
  • Apache Camel 3.14.2
  • OpenJDK 11

1.2.2. Camel K 功能

  • 用于自动扩展和缩减至零的 Knative Serving
  • 用于事件驱动的架构的 Knative Eventing
  • 默认使用 Quarkus 运行时进行性能优化
  • Camel 集成使用 Java 或 YAML DSL 编写
  • 使用 Visual Studio Code 的开发工具
  • 使用 OpenShift 中的 Prometheus 监控集成
  • Quickstart 教程
  • 对外部系统的kamelet 目录,如 AWS、JIRA 和 Salesforce

下图显示了 Camel K 云原生架构的简化视图:

Camel K 架构

其他资源

1.2.3. Kamelets

kamelets 隐藏了连接到简单界面后面的外部系统的复杂性,其中包含实例化它们所需的所有信息,即使用户没有熟悉 Camel。

kamelet 作为自定义资源实施,您可以在 OpenShift 集群上安装并在 Camel K 集成中使用。kamelets 是路由模板,它使用 Camel 组件来连接外部系统,而无需深入了解组件。Kamelets 对连接到外部系统的详细信息进行了抽象。您还可以组合 Kamelets 来创建复杂的 Camel 集成,就像使用标准 Camel 组件一样。

1.3. Camel K 开发工具

Camel K 为 Visual Studio (VS)代码、Red Hat CodeReady WorkSpaces 和 Eclipse Che 提供开发工具扩展。基于 Camel 的工具扩展包括自动完成 Camel DSL 代码、Camel K 模式配置和 Camel K traits 等功能。虽然 Didact 教程工具扩展提供了 Camel K 快速启动指南命令的自动执行。

可用的 VS Code 开发工具扩展如下:

  • 红帽 Apache Camel 的 VS Code 扩展包

    • Apache Camel K 扩展的工具
    • Apache Camel 扩展的语言支持
    • Apache Camel K 的调试适配器
    • OpenShift、Java 等的额外扩展
    • VS Code 扩展的 Didact 教程工具

有关如何为 Camel K 设置这些 VS Code 扩展的详情,请参考 设置 Camel K 开发环境

重要

注意: Camel K VS Code 扩展是社区功能。

Eclipse Che 还使用 vscode-camelk 插件提供这些功能。

有关 开发支持范围的更多信息,请参阅开发支持覆盖范围

1.4. Camel K 发行版

表 1.1. Red Hat Integration - Camel K distribution
分发描述位置

Operator 镜像

Red Hat Integration 的容器镜像 - Camel K Operator: integration/camel-k-rhel8-operator

Maven 存储库

Red Hat Integration 的 Maven 工件 - Camel K

红帽提供了 Maven 存储库,用于托管我们随我们的产品附带的内容。这些软件仓库可从软件下载页面下载。

对于 Red Hat Integration - Camel K 需要以下软件仓库:

  • rhi-common
  • rhi-camel-quarkus
  • rhi-camel-k

红帽集成安装 - 本发行版本中不支持以离线模式的 Camel K。

Red Hat Integration 的软件下载

源代码

Red Hat Integration 的源代码 - Camel K

Red Hat Integration 的软件下载

Quickstarts

快速入门指南:

  • 基本 Java 集成
  • 事件流集成
  • JDBC 集成
  • JMS 集成
  • Kafka 集成
  • Knative 集成
  • SaaS 集成
  • Serverless API 集成
  • 转换集成

https://github.com/openshift-integration

注意

您必须有一个 Red Hat Integration 订阅,并登录到红帽客户门户网站来访问 Red Hat Integration - Camel K 发行版本。

第 2 章 准备 OpenShift 集群

本章解释了如何在 OpenShift 上安装 Red Hat Integration - Camel K 和 OpenShift Serverless,以及如何在开发环境中安装所需的 Camel K 和 OpenShift Serverless 命令行工具。

2.1. 安装 Camel K

您可以从 OperatorHub 在 OpenShift 集群上安装 Red Hat Integration - Camel K Operator。OperatorHub 可从 OpenShift Container Platform Web 控制台获得,集群管理员提供了一个界面来发现和安装 Operator。

安装 Camel K Operator 后,您可以安装 Camel K CLI 工具以便命令行访问所有 Camel K 功能。

先决条件

  • 您可以使用正确的访问级别访问 OpenShift 4.6 (或更新版本)集群、创建项目并安装 Operator 的功能,以及在本地系统上安装 CLI 工具。

    注意

    从 OpenShift OperatorHub 安装 Camel K 时,您不需要创建 pull secret。Camel K Operator 会自动重复使用 OpenShift 集群级别的身份验证,以从 registry.redhat.io 中拉取 Camel K 镜像。

  • 已安装 OpenShift CLI 工具(oc),以便您可以在命令行中与 OpenShift 集群交互。有关如何安装 OpenShift CLI 的详细信息,请参阅安装 OpenShift CLI

流程

  1. 在 OpenShift Container Platform Web 控制台中,使用具有集群管理员权限的账户登录。
  2. 创建新 OpenShift 项目:

    1. 在左侧导航菜单中,点击 Home > Project > Create Project
    2. 输入项目名称,如 my-camel-k-project,然后单击 Create
  3. 在左侧导航菜单中,点 Operators > OperatorHub
  4. Filter by keyword 文本框中,输入 Camel K,然后点 Red Hat Integration - Camel K Operator 卡。
  5. 阅读 Operator 的信息,然后单击 Install。此时会打开 Operator 安装页面。
  6. 选择以下订阅设置:

    • 更新频道 & gt; latest
    • Installation Mode > A specific namespace on the cluster > my-camel-k-project
    • Approval Strategy > Automatic

      注意

      如果您的环境需要,也可以使用 Installation mode > All namespaces on the clusterApproval Strategy > Manual 设置。

  7. Install,然后等待几分钟,直到 Camel K Operator 准备就绪。
  8. 下载并安装 Camel K CLI 工具:

    1. 在 OpenShift Web 控制台顶部的 Help 菜单(?)中选择 Command line tools
    2. 向下滚动到 kamel - Red Hat Integration - Camel K - 命令行界面 部分。
    3. 单击链接,以下载本地操作系统的二进制文件(Linux、Mac、Windows)。
    4. 在您的系统路径中解压并安装 CLI。
    5. 要验证您可以访问 Kamel K CLI,请打开命令窗口,然后键入以下内容:

      kamel --help

      此命令显示有关 Camel K CLI 命令的信息。

后续步骤

(可选) 指定 Camel K 资源限值

2.1.1. 指定 Camel K 资源限值

安装 Camel K 时,Camel K 的 OpenShift pod 没有为 CPU 和内存(RAM)资源设置任何限制。如果要为 Camel K 定义资源限值,您必须编辑在安装过程中创建的 Camel K 订阅资源。

前提条件

  • 您对安装 Camel K Operator 的 OpenShift 项目具有集群管理员访问权限,如 安装 Camel K 所述。
  • 您知道您要应用到 Camel K 订阅的资源限值。有关资源限值的更多信息,请参阅以下文档:

流程

  1. 登录 OpenShift Web 控制台。
  2. 选择 Operators > Installed Operators > Operator Details > Subscription
  3. 选择 Actions > Edit Subscription

    订阅的文件会在 YAML 编辑器中打开。

  4. spec 部分下,添加一个 config.resources 部分,并为 memory 和 cpu 提供值,如下例所示:

    Copy to Clipboard Toggle word wrap
    spec:
      channel: default
      config:
        resources:
          limits:
            memory: 512Mi
            cpu: 500m
          requests:
            cpu: 200m
            memory: 128Mi
  5. 保存您的更改。

OpenShift 更新订阅并应用您指定的资源限值。

2.2. 安装 OpenShift Serverless

您可以从 OperatorHub 在 OpenShift 集群上安装 OpenShift Serverless Operator。OperatorHub 可从 OpenShift Container Platform Web 控制台获得,集群管理员提供了一个界面来发现和安装 Operator。

OpenShift Serverless Operator 支持 Knative Serving 和 Knative Eventing 功能。如需了解更多详细信息,请参阅安装 OpenShift Serverless Operator

先决条件

  • 具有安装 Camel K Operator 的 OpenShift 项目的集群管理员访问权限。
  • 已安装 OpenShift CLI 工具(oc),以便您可以在命令行中与 OpenShift 集群交互。有关如何安装 OpenShift CLI 的详细信息,请参阅安装 OpenShift CLI

流程

  1. 在 OpenShift Container Platform Web 控制台中,使用具有集群管理员权限的账户登录。
  2. 在左侧导航菜单中,点 Operators > OperatorHub
  3. Filter by keyword 文本框中,输入 Serverless 来查找 OpenShift Serverless Operator
  4. 阅读 Operator 的信息,然后点 Install 以显示 Operator 订阅页面。
  5. 选择默认订阅设置:

    • 更新频道 > 选择与您的 OpenShift 版本匹配的频道,如 4.12
    • Installation Mode > All namespaces on the cluster
    • Approval Strategy > Automatic

      注意

      如果您的环境需要,也可以使用 Approval Strategy > Manual 设置。

  6. Install,然后等待几分钟,直到 Operator 准备就绪。
  7. 按照 OpenShift 文档中的步骤安装所需的 Knative 组件:

  8. (可选)下载并安装 OpenShift Serverless CLI 工具:

    1. 在 OpenShift Web 控制台顶部的 Help 菜单(?)中选择 Command line tools
    2. 向下滚动到 kn - OpenShift Serverless - 命令行界面 部分。
    3. 单击链接,以下载本地操作系统的二进制文件(Linux、Mac、Windows)
    4. 在您的系统路径中解压并安装 CLI。
    5. 要验证您可以访问 kn CLI,请打开命令窗口,然后键入以下内容:

      kn --help

      此命令显示有关 OpenShift Serverless CLI 命令的信息。

      如需了解更多详细信息,请参阅 OpenShift Serverless CLI 文档

2.3. 为 Camel K 配置 Maven 存储库

对于 Camel K operator,您可以在 ConfigMap 或 secret 中提供 Maven 设置。

流程

  1. 要从文件创建 ConfigMap,请运行以下命令:

    Copy to Clipboard Toggle word wrap
    oc create configmap maven-settings --from-file=settings.xml

    然后,在 IntegrationPlatform 资源中可以从 spec.build.maven.settings 字段引用创建的 ConfigMap

    示例

    Copy to Clipboard Toggle word wrap
    apiVersion: camel.apache.org/v1
    kind: IntegrationPlatform
    metadata:
      name: camel-k
    spec:
      build:
         maven:
            settings:
               configMapKeyRef:
                    key: settings.xml
                    name: maven-settings

    或者,您可以直接编辑 IntegrationPlatform 资源来引用包含 Maven 设置的 ConfigMap:

    Copy to Clipboard Toggle word wrap
    oc edit ip camel-k

为远程 Maven 存储库配置 CA 证书

您可以在 Secret 中提供 Maven 命令用来连接到远程 Maven 存储库的 CA 证书。

流程

  1. 使用以下命令,从文件创建 Secret:

    Copy to Clipboard Toggle word wrap
    oc create secret generic maven-ca-certs --from-file=ca.crt
  2. spec.build.maven.caSecret 字段引用 IntegrationPlatform 资源中创建的 Secret,如下所示。

    Copy to Clipboard Toggle word wrap
    apiVersion: camel.apache.org/v1
    kind: IntegrationPlatform
    metadata:
      name: camel-k
    spec:
      build:
          maven:
              caSecret:
                 key: tls.crt
                 name: tls-secret

第 3 章 开发并运行 Camel K 集成

本章介绍了如何设置开发环境以及如何开发和部署使用 Java 和 YAML 编写的简单 Camel K 集成。它还介绍了如何在运行时使用 kamel 命令行来管理 Camel K 集成。例如,这包括运行、描述、日志记录和删除集成。

3.1. 设置 Camel K 开发环境

在自动部署 Camel K 快速启动指南前,您必须使用推荐的开发工具设置您的环境。本节介绍如何安装推荐的 Visual Studio (VS)代码 IDE 及其为 Camel K 提供的扩展。

注意
  • Camel K VS Code 扩展是社区功能。
  • 建议使用 VS Code,以及 Camel K 的最佳开发人员体验。这包括自动完成 Camel DSL 代码和 Camel K traits,以及自动执行教程命令。但是,您可以使用您选择的 IDE 手动输入代码和教程命令,而不是 VS Code。

先决条件

流程

  1. 在您的开发平台上安装 VS Code。例如,在 Red Hat Enterprise Linux 中:

    1. 安装所需的密钥和存储库:

      Copy to Clipboard Toggle word wrap
      $ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
      $ sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
    2. 更新缓存并安装 VS Code 软件包:

      Copy to Clipboard Toggle word wrap
      $ yum check-update
      $ sudo yum install code

      有关在其他平台上安装的详情,请查看 VS Code 安装文档

  2. 输入 代码 命令以启动 VS Code 编辑器。如需了解更多详细信息,请参阅 VS Code 命令行文档
  3. 安装 VS Code Camel 扩展包,其中包括 Camel K 所需的扩展。例如,在 VS Code 中:

    1. 在左侧导航栏中,单击 Extensions
    2. 在搜索框中,输入 Apache Camel
    3. 选择 由红帽为 Apache Camel 的扩展 包,然后单击 Install

      选择红帽 Apache Camel 的 VS Code 扩展包

      如需了解更多详细信息,请参阅红帽 Apache Camel 的扩展包 的说明。

  4. 安装 VS Code Didact 扩展,您可以通过单击教程中的链接来自动运行快速启动指南命令。例如,在 VS Code 中:

    1. 在左侧导航栏中,单击 Extensions
    2. 在搜索框中,输入 Didact
    3. 选择扩展,然后单击 Install

      如需了解更多详细信息,请参阅 Did act 扩展 的说明。

3.2. 在 Java 中开发 Camel K 集成

本节介绍如何在 Java DSL 中开发简单的 Camel K 集成。在 Java 中编写集成以使用 Camel K 部署,与在 Camel 中定义路由规则相同。但是,在使用 Camel K 时,您不需要构建并将集成打包为 JAR。

您可以在集成路由中直接使用任何 Camel 组件。Camel K 会自动处理依赖项管理,并使用代码检查从 Camel 目录中导入所有必需的库。

流程

  1. 输入 kamel init 命令来生成简单的 Java 集成文件。例如:

    Copy to Clipboard Toggle word wrap
    $ kamel init HelloCamelK.java
  2. 在 IDE 中打开生成的集成文件,并根据需要编辑。例如,HelloCamelK.java 集成自动包含 Camel 计时器 和日志 组件,以帮助您开始:

    Copy to Clipboard Toggle word wrap
    // camel-k: language=java
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class HelloCamelK extends RouteBuilder {
      @Override
      public void configure() throws Exception {
    
          // Write your routes here, for example:
          from("timer:java?period=1s")
            .routeId("java")
            .setBody()
              .simple("Hello Camel K from ${routeId}")
            .to("log:info");
    
      }
    }

后续步骤

3.3. 在 YAML 中开发 Camel K 集成

本节介绍如何在 YAML DSL 中开发简单的 Camel K 集成。在 YAML 中编写集成以使用 Camel K 部署,与在 Camel 中定义路由规则相同。

您可以在集成路由中直接使用任何 Camel 组件。Camel K 会自动处理依赖项管理,并使用代码检查从 Camel 目录中导入所有必需的库。

流程

  1. 输入 kamel init 命令来生成简单的 YAML 集成文件。例如:

    Copy to Clipboard Toggle word wrap
    $ kamel init hello.camelk.yaml
  2. 在 IDE 中打开生成的集成文件,并根据需要编辑。例如,hello.camelk.yaml 集成会自动包含 Camel 计时器 和日志 组件,以帮助您开始:

    Copy to Clipboard Toggle word wrap
    # Write your routes here, for example:
    - from:
        uri: "timer:yaml"
        parameters:
          period: "1s"
        steps:
          - set-body:
              constant: "Hello Camel K from yaml"
          - to: "log:info"

3.4. 运行 Camel K 集成

您可以使用 kamel run 命令从命令行运行 OpenShift 集群的云中 Camel K 集成。

先决条件

流程

  1. 使用 oc 客户端工具登录到 OpenShift 集群,例如:

    Copy to Clipboard Toggle word wrap
    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. 确保 Camel K Operator 正在运行,例如:

    Copy to Clipboard Toggle word wrap
    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. 输入 kamel run 命令,以在 OpenShift 的云中运行您的集成。例如:

    Java 示例

    Copy to Clipboard Toggle word wrap
    $ kamel run HelloCamelK.java
    integration "hello-camel-k" created

    YAML 示例

    Copy to Clipboard Toggle word wrap
    $ kamel run hello.camelk.yaml
    integration "hello" created

  4. 输入 kamel get 命令检查集成的状态:

    Copy to Clipboard Toggle word wrap
    $ kamel get
    NAME       PHASE           KIT
    hello      Building Kit    myproject/kit-bq666mjej725sk8sn12g

    当集成第一次运行时,Camel K 会为容器镜像构建集成工具包,它会下载所有所需的 Camel 模块并将其添加到镜像 classpath 中。

  5. 再次输入 kamel get 来验证集成是否正在运行:

    Copy to Clipboard Toggle word wrap
    $ kamel get
    NAME       PHASE   KIT
    hello      Running myproject/kit-bq666mjej725sk8sn12g
  6. 输入 kamel log 命令将日志输出到 stdout

    Copy to Clipboard Toggle word wrap
    $ kamel log hello
    [1] 2021-08-11 17:58:40,573 INFO  [org.apa.cam.k.Runtime] (main) Apache Camel K Runtime 1.7.1.fuse-800025-redhat-00001
    [1] 2021-08-11 17:58:40,653 INFO  [org.apa.cam.qua.cor.CamelBootstrapRecorder] (main) bootstrap runtime: org.apache.camel.quarkus.main.CamelMainRuntime
    [1] 2021-08-11 17:58:40,844 INFO  [org.apa.cam.k.lis.SourcesConfigurer] (main) Loading routes from: SourceDefinition{name='camel-k-embedded-flow', language='yaml', location='file:/etc/camel/sources/camel-k-embedded-flow.yaml', }
    [1] 2021-08-11 17:58:41,216 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Routes startup summary (total:1 started:1)
    [1] 2021-08-11 17:58:41,217 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main)     Started route1 (timer://yaml)
    [1] 2021-08-11 17:58:41,217 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 3.10.0.fuse-800010-redhat-00001 (camel-1) started in 136ms (build:0ms init:100ms start:36ms)
    [1] 2021-08-11 17:58:41,268 INFO  [io.quarkus] (main) camel-k-integration 1.6.6 on JVM (powered by Quarkus 1.11.7.Final-redhat-00009) started in 2.064s.
    [1] 2021-08-11 17:58:41,269 INFO  [io.quarkus] (main) Profile prod activated.
    [1] 2021-08-11 17:58:41,269 INFO  [io.quarkus] (main) Installed features: [camel-bean, camel-core, camel-k-core, camel-k-runtime, camel-log, camel-support-common, camel-timer, camel-yaml-dsl, cdi]
    [1] 2021-08-11 17:58:42,423 INFO  [info] (Camel (camel-1) thread #0 - timer://yaml) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from yaml]
    ...
  7. Ctrl-C 终止终端中的日志记录。

其他资源

在不使用 CLI 的情况下运行集成

您可以在没有 CLI (Command Line Interface)的情况下运行 集成自定义资源,并使用配置创建一个集成自定义资源 来运行应用程序。

例如,执行以下示例路由。

Copy to Clipboard Toggle word wrap
kamel run Sample.java -o yaml

它返回预期的 Integration 自定义资源。

Copy to Clipboard Toggle word wrap
apiVersion: camel.apache.org/v1
kind: Integration
metadata:
  creationTimestamp: null
  name: my-integration
  namespace: default
spec:
  sources:
  - content: "
    import org.apache.camel.builder.RouteBuilder;
    public class Sample extends RouteBuilder {
      @Override
      public void configure()
      throws Exception {
        from(\"timer:tick\")
        .log(\"Hello Integration!\");
       }
      }"
    name: Sample.java
status: {}

将此自定义资源保存到 yaml 文件 my-integration.yaml 中。现在,使用 oc 命令行、UI 或 API 来调用 OpenShift 集群,运行包含 Integration 自定义资源的集成。在以下示例中,从命令行使用 oc CLI。

Copy to Clipboard Toggle word wrap
oc apply -f my-integration.yaml
...
integration.camel.apache.org/my-integration created

Operator 运行集成。

注意

自定义资源的 schema 更改

强大的 Trait API 会对以下 CustomResourceDefinitions 实施更改: 集成集成工具包'和"集成平台"。

spec.traits.<trait-id>.configuration 下的特征属性现在直接在 spec.traits.<trait-id> 下定义。

Copy to Clipboard Toggle word wrap
traits:
  container:
    configuration:
      enabled: true
      name: my-integration

↓↓↓

Copy to Clipboard Toggle word wrap
traits:
  container:
    enabled: true
    name: my-integration

这种实现中有可能向后兼容。为实现向后兼容,请为每个特征类型提供带有 RawMessage 类型的 Configuration 字段,以便从现有集成和资源从新的 Camel K 版本读取。

当读取旧的集成和资源时,每个特征(若有)中的旧配置将迁移到新的 Trait API 字段。如果在新的 API 字段中预定义了值,它们会在旧的 API 字段前面。

Copy to Clipboard Toggle word wrap
type Trait struct {
    // Can be used to enable or disable a trait. All traits share this common property.
    Enabled *bool `property:"enabled" json:"enabled,omitempty"`

    // Legacy trait configuration parameters.
    // Deprecated: for backward compatibility.
    Configuration *Configuration `json:"configuration,omitempty"`
}

// Deprecated: for backward compatibility.
type Configuration struct {
    RawMessage `json:",inline"`
}

3.5. 在开发模式下运行 Camel K 集成

您可以从命令行在 OpenShift 集群上以开发模式运行 Camel K 集成。使用开发模式,您可以快速迭代开发中的集成,并快速获得代码反馈。

当您使用 --dev 选项指定 kamel run 命令时,这将立即在云中部署集成,并在终端中显示集成日志。然后,您可以更改代码,并查看立即对 OpenShift 上的远程集成 Pod 应用的更改。终端会自动显示云中远程集成的所有重新部署。

注意

Camel K 在开发模式中生成的工件与您生产环境中运行的工件相同。开发模式的目的是加快开发速度。

先决条件

流程

  1. 使用 oc 客户端工具登录到 OpenShift 集群,例如:

    Copy to Clipboard Toggle word wrap
    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. 确保 Camel K Operator 正在运行,例如:

    Copy to Clipboard Toggle word wrap
    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. 使用 --dev 进入 kamel run 命令,以便在云的 OpenShift 上以开发模式运行集成。下面显示了一个简单的 Java 示例:

    Copy to Clipboard Toggle word wrap
    $ kamel run HelloCamelK.java --dev
    Condition "IntegrationPlatformAvailable" is "True" for Integration hello-camel-k: test/camel-k
    Integration hello-camel-k in phase "Initialization"
    Integration hello-camel-k in phase "Building Kit"
    Condition "IntegrationKitAvailable" is "True" for Integration hello-camel-k: kit-c49sqn4apkb4qgn55ak0
    Integration hello-camel-k in phase "Deploying"
    Progress: integration "hello-camel-k" in phase Initialization
    Progress: integration "hello-camel-k" in phase Building Kit
    Progress: integration "hello-camel-k" in phase Deploying
    Integration hello-camel-k in phase "Running"
    Condition "DeploymentAvailable" is "True" for Integration hello-camel-k: deployment name is hello-camel-k
    Progress: integration "hello-camel-k" in phase Running
    Condition "CronJobAvailable" is "False" for Integration hello-camel-k: different controller strategy used (deployment)
    Condition "KnativeServiceAvailable" is "False" for Integration hello-camel-k: different controller strategy used (deployment)
    Condition "Ready" is "False" for Integration hello-camel-k
    Condition "Ready" is "True" for Integration hello-camel-k
    [1] Monitoring pod hello-camel-k-7f85df47b8-js7cb
    ...
    ...
    [1] 2021-08-11 18:34:44,069 INFO  [org.apa.cam.k.Runtime] (main) Apache Camel K Runtime 1.7.1.fuse-800025-redhat-00001
    [1] 2021-08-11 18:34:44,167 INFO  [org.apa.cam.qua.cor.CamelBootstrapRecorder] (main) bootstrap runtime: org.apache.camel.quarkus.main.CamelMainRuntime
    [1] 2021-08-11 18:34:44,362 INFO  [org.apa.cam.k.lis.SourcesConfigurer] (main) Loading routes from: SourceDefinition{name='HelloCamelK', language='java', location='file:/etc/camel/sources/HelloCamelK.java', }
    [1] 2021-08-11 18:34:46,180 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Routes startup summary (total:1 started:1)
    [1] 2021-08-11 18:34:46,180 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main)     Started java (timer://java)
    [1] 2021-08-11 18:34:46,180 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 3.10.0.fuse-800010-redhat-00001 (camel-1) started in 243ms (build:0ms init:213ms start:30ms)
    [1] 2021-08-11 18:34:46,190 INFO  [io.quarkus] (main) camel-k-integration 1.6.6 on JVM (powered by Quarkus 1.11.7.Final-redhat-00009) started in 3.457s.
    [1] 2021-08-11 18:34:46,190 INFO  [io.quarkus] (main) Profile prod activated.
    [1] 2021-08-11 18:34:46,191 INFO  [io.quarkus] (main) Installed features: [camel-bean, camel-core, camel-java-joor-dsl, camel-k-core, camel-k-runtime, camel-log, camel-support-common, camel-timer, cdi]
    [1] 2021-08-11 18:34:47,200 INFO  [info] (Camel (camel-1) thread #0 - timer://java) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    [1] 2021-08-11 18:34:48,180 INFO  [info] (Camel (camel-1) thread #0 - timer://java) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    [1] 2021-08-11 18:34:49,180 INFO  [info] (Camel (camel-1) thread #0 - timer://java) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    ...
  4. 编辑集成 DSL 文件的内容,保存您的更改,并立即查看在终端中显示的更改。例如:

    Copy to Clipboard Toggle word wrap
    ...
    integration "hello-camel-k" updated
    ...
    [2] 2021-08-11 18:40:54,173 INFO  [org.apa.cam.k.Runtime] (main) Apache Camel K Runtime 1.7.1.fuse-800025-redhat-00001
    [2] 2021-08-11 18:40:54,209 INFO  [org.apa.cam.qua.cor.CamelBootstrapRecorder] (main) bootstrap runtime: org.apache.camel.quarkus.main.CamelMainRuntime
    [2] 2021-08-11 18:40:54,301 INFO  [org.apa.cam.k.lis.SourcesConfigurer] (main) Loading routes from: SourceDefinition{name='HelloCamelK', language='java', location='file:/etc/camel/sources/HelloCamelK.java', }
    [2] 2021-08-11 18:40:55,796 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Routes startup summary (total:1 started:1)
    [2] 2021-08-11 18:40:55,796 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main)     Started java (timer://java)
    [2] 2021-08-11 18:40:55,797 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 3.10.0.fuse-800010-redhat-00001 (camel-1) started in 174ms (build:0ms init:147ms start:27ms)
    [2] 2021-08-11 18:40:55,803 INFO  [io.quarkus] (main) camel-k-integration 1.6.6 on JVM (powered by Quarkus 1.11.7.Final-redhat-00009) started in 3.025s.
    [2] 2021-08-11 18:40:55,808 INFO  [io.quarkus] (main) Profile prod activated.
    [2] 2021-08-11 18:40:55,809 INFO  [io.quarkus] (main) Installed features: [camel-bean, camel-core, camel-java-joor-dsl, camel-k-core, camel-k-runtime, camel-log, camel-support-common, camel-timer, cdi]
    [2] 2021-08-11 18:40:56,810 INFO  [info] (Camel (camel-1) thread #0 - timer://java) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    [2] 2021-08-11 18:40:57,793 INFO  [info] (Camel (camel-1) thread #0 - timer://java) Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    ...
  5. Ctrl-C 终止终端中的日志记录。

其他资源

3.6. 使用 modeline 运行 Camel K 集成

您可以使用 Camel K 模式在 Camel K 集成源文件中指定多个配置选项,这些文件在运行时执行。这通过保存重新输入多个命令行选项的时间来创建时间,并有助于防止输入错误。

以下示例显示了 Java 集成文件的 modeline 条目,它启用了 3scale 并限制集成容器内存。

先决条件

流程

  1. 在您的集成文件中添加 Camel K modeline 条目。例如:

    ThreeScaleRest.java

    Copy to Clipboard Toggle word wrap
    // camel-k: trait=3scale.enabled=true trait=container.limit-memory=256Mi 
    1
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class ThreeScaleRest extends RouteBuilder {
    
      @Override
      public void configure() throws Exception {
          rest().get("/")
            .route()
            .setBody().constant("Hello");
      }
    }

    1
    启用容器和 3scale 特征,以通过 3scale 公开路由,并限制容器内存。
  2. 运行集成,例如:

    Copy to Clipboard Toggle word wrap
    kamel run ThreeScaleRest.java

    kamel run 命令输出集成中指定的任何 mode 命令行选项,例如:

    Copy to Clipboard Toggle word wrap
    Modeline options have been loaded from source files
    Full command: kamel run ThreeScaleRest.java --trait=3scale.enabled=true --trait=container.limit-memory=256Mi

其他资源

第 4 章 升级 Camel K

您可以自动升级已安装的 Camel K operator,但不自动升级 Camel K 集成。您必须手动为 Camel K 集成触发升级。本章解释了如何升级 Camel K operator 和 Camel K 集成。

4.1. 升级 Camel K operator

安装的 Camel K operator 订阅指定一个更新频道,如 1.6.0 频道,用于跟踪和接收 Operator 的更新。要升级 Operator 以开始跟踪并从更新频道接受更新,您可以更改订阅中的更新频道。如需有关更改 已安装 Operator 的更新频道的更多信息,请参阅升级已安装的 Operator。

注意
  • 安装的 Operator 无法变为比当前频道旧的频道。

如果订阅中的批准策略被设置为 Automatic,升级过程会在所选频道中提供新的 Operator 版本时立即启动。如果批准策略被设置为 Manual,则必须手动批准待处理的升级。

先决条件

  • Camel K operator 使用 Operator Lifecycle Manager (OLM)安装。

流程

  1. 在 OpenShift Container Platform web 控制台的 Administrator 视角中,进入 OperatorsInstalled Operators
  2. Camel K Operator
  3. Subscription 标签页。
  4. Channel 中的更新频道的名称。
  5. 点击您要更改为的更新频道。例如,latest。点击 Save。这将开始升级到最新的 Camel K 版本。

对于带有自动批准策略的订阅,升级会自动开始。返回到 OperatorsInstalled Operators 页面,以监控升级的进度。完成后,状态将变为 Succeeded 和 Up to date。

对于采用手动批准策略的订阅,您可以从 Subscription 选项卡中手动批准升级。

4.2. 升级 Camel K 集成

当您触发 Camel K operator 的升级时,Operator 会准备要升级的集成,但不会为每个 Operator 触发升级,以避免服务中断。在升级 Operator 时,集成自定义资源不会自动升级到较新的版本,例如,Operator 可能处于 1.6.0 版本,而集成会报告自定义资源的 status.version 字段。

先决条件

Camel K operator 使用 Operator Lifecycle Manager (OLM)安装和升级。

流程

  • 打开终端,并运行以下命令来升级 Camel K 间的操作。
Copy to Clipboard Toggle word wrap
kamel rebuild myintegration

这将清除集成资源的状态,Operator 将使用升级版本的工件(如 1.6.0 )启动集成部署。

4.3. 降级 Camel K

您可以通过安装以前的 Operator 版本来降级到 Camel K operator 的旧版本。这需要使用 OC CLI 手动触发。有关使用 CLI 安装 Operator 特定版本的更多信息 ,请参阅安装 Operator 的特定版本

重要

您必须删除现有 Camel K operator,然后安装 Operator 的 specifc 版本,因为 OLM 不支持降级。

安装旧版本的 Operator 后,请使用 kamel rebuild 命令降级与 Operator 版本的集成。例如,

Copy to Clipboard Toggle word wrap
kamel rebuild myintegration

第 5 章 Camel K 快速启动开发人员指南

Red Hat Integration - Camel K 提供基于 https://github.com/openshift-integration 提供的集成用例的快速入门开发人员教程。本章详细介绍了如何设置和部署以下教程:

5.1. 部署基本 Camel K Java 集成

本教程介绍了如何在 OpenShift 的云中运行简单的 Java 集成,应用配置和路由到集成,并以 Kubernetes CronJob 的形式运行集成。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-basic.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-basic
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。例如:

    启动 Camel K Quickstart 指南

    这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。

  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-basic 中的命令。

5.2. 部署 Camel K Serverless 与 Knative 集成

本教程演示了如何在事件驱动的架构中部署 Camel K 与 OpenShift Serverless 集成。本教程使用 Knative Eventing 代理在 Bitcoin 演示中使用事件发布订阅模式进行通信。

本教程还介绍了如何使用 Camel K 集成来与多个外部系统连接到 Knative 事件网格。Camel K 集成也使用 Knative Serving 来根据需要自动缩放和缩减为零。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-knative.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-knative
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-knative 中的命令。

5.3. 部署 Camel K 转换集成

本教程介绍了如何在 OpenShift 上运行 Camel K Java 集成,该集成将数据转换为 JSON,并将其存储在 PostgreSQL 等数据库中。

教程示例使用 CSV 文件查询 XML API,并使用收集的数据来构建有效的 GeoJSON 文件,该文件存储在 PostgreSQL 数据库中。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-transformations.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-transformations
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-transformations 中的命令。

5.4. 部署 Camel K Serverless 事件流集成

本教程演示了在 Knative Eventing 中使用 Camel K 和 OpenShift Serverless 进行事件驱动的架构。

本教程演示了如何在带有 AMQ Broker 集群的 AMQ Streams 集群中安装带有 Knative 的 Camel K 和 Serverless,以及如何部署事件流项目来运行全局 hazard 警报演示应用程序。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-event-streaming.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-event-streaming
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-event-streaming 中的命令。

5.5. 部署基于 Camel K Serverless API 的集成

本教程演示了将 Camel K 和 OpenShift Serverless 与 Knative Serving 用于基于 API 的集成,并管理在 OpenShift 上使用 3scale API 管理的 API。

教程演示了如何配置基于 Amazon S3 的存储、设计 OpenAPI 定义,并运行调用演示 API 端点的集成。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-api.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-api
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-api 中的命令。

5.6. 部署 Camel K SaaS 集成

本教程介绍了如何在 OpenShift 上运行 Camel K Java 集成,该集成连接了两个广泛使用的软件作为服务(SaaS)供应商。

教程示例演示了如何使用基于 REST 的 Camel 组件集成 Salesforce 和 ServiceNow SaaS 供应商。在这个简单示例中,每个新的 Salesforce 问题单都会被复制到包括 Salesforce 问题单号的对应 ServiceNow Incident 中。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-saas.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-saas
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-saas 中的命令。

5.7. 部署 Camel K JDBC 集成

本教程介绍了如何通过 JDBC 驱动程序使用 Camel K 和 SQL 数据库。本教程介绍了如何将生成数据的集成设置为 Postgres 数据库(您可以使用您选择的任何相关数据库)以及如何从同一数据库读取数据。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-jdbc.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-jdbc
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-jdbc 中的命令。

5.8. 部署 Camel K JMS 集成

本教程介绍了如何使用 JMS 连接到消息代理,以便使用和生成消息。有两个示例:

  • JMS Sink:本教程演示了如何向 JMS 代理生成消息。
  • JMS Source:本教程演示了如何使用来自 JMS 代理的消息。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-jms.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-jms
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-jms 中的命令。

其他资源

5.9. 部署 Camel K Kafka 集成

本教程介绍了如何在 Apache Kafka 中使用 Camel K。本教程介绍了如何通过 Red Hat OpenShift Streams for Apache Kafka 设置 Kafka 主题,并将其与 Camel K 一起使用。

先决条件

流程

  1. 克隆教程 Git 存储库:

    Copy to Clipboard Toggle word wrap
    $ git clone git@github.com:openshift-integration/camel-k-example-kafka.git
  2. 在 VS Code 中,选择 FileOpen Foldercamel-k-example-kafka
  3. 在 VS Code 导航树中,右键单击 readme.didact.md 文件,然后从文件中选择 Didact: Start Didact 教程。这会在 VS Code 中打开一个新的 Didact 选项卡,以显示教程说明。
  4. 按照教程说明,点提供的链接自动运行命令。

    或者,如果您没有安装带有 Didact 扩展名的 VS Code,您可以手动输入 https://github.com/openshift-integration/camel-k-example-kafka 中的命令。

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat, Inc.