검색

이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 3. Developing and running Camel K integrations

download PDF

This chapter explains how to set up your development environment and how to develop and deploy simple Camel K integrations written in Java and YAML. It also shows how to use the kamel command line to manage Camel K integrations at runtime. For example, this includes running, describing, logging, and deleting integrations.

3.1. Setting up your Camel K development environment

You must set up your environment with the recommended development tooling before you can automatically deploy the Camel K quick start tutorials. This section explains how to install the recommended Visual Studio (VS) Code IDE and the extensions that it provides for Camel K.

Note
  • The Camel K VS Code extensions are community features.
  • VS Code is recommended for ease of use and the best developer experience of Camel K. This includes automatic completion of Camel DSL code and Camel K traits, and automatic execution of tutorial commands. However, you can manually enter your code and tutorial commands using your chosen IDE instead of VS Code.

Prerequisites

Procedure

  1. Install VS Code on your development platform. For example, on Red Hat Enterprise Linux:

    1. Install the required key and repository:

      $ 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. Update the cache and install the VS Code package:

      $ yum check-update
      $ sudo yum install code

      For details on installing on other platforms, see the VS Code installation documentation.

  2. Enter the code command to launch the VS Code editor. For more details, see the VS Code command line documentation.
  3. Install the VS Code Camel Extension Pack, which includes the extensions required for Camel K. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Apache Camel.
    3. Select the Extension Pack for Apache Camel by Red Hat, and click Install.

      Selecting the VS Code Extension Pack for Apache Camel by Red Hat

      For more details, see the instructions for the Extension Pack for Apache Camel by Red Hat.

  4. Install the VS Code Didact extension, which you can use to automatically run quick start tutorial commands by clicking links in the tutorial. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Didact.
    3. Select the extension, and click Install.

      For more details, see the instructions for the Didact extension.

3.2. Developing Camel K integrations in Java

This section shows how to develop a simple Camel K integration in Java DSL. Writing an integration in Java to be deployed using Camel K is the same as defining your routing rules in Camel. However, you do not need to build and package the integration as a JAR when using Camel K.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple Java integration file. For example:

    $ kamel init HelloCamelK.java
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the HelloCamelK.java integration automatically includes the Camel timer and log components to help you get started:

    // 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. Developing Camel K integrations in YAML

This section explains how to develop a simple Camel K integration in YAML DSL. Writing an integration in YAML to be deployed using Camel K is the same as defining your routing rules in Camel.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple YAML integration file. For example:

    $ kamel init hello.camelk.yaml
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the hello.camelk.yaml integration automatically includes the Camel timer and log components to help you get started:

    # 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. Running Camel K integrations

You can run Camel K integrations in the cloud on your OpenShift cluster from the command line using the kamel run command.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command to run your integration in the cloud on OpenShift. For example:

    Java example

    $ kamel run HelloCamelK.java
    integration "hello-camel-k" created

    YAML example

    $ kamel run hello.camelk.yaml
    integration "hello" created

  4. Enter the kamel get command to check the status of the integration:

    $ kamel get
    NAME       PHASE           KIT
    hello      Building Kit    myproject/kit-bq666mjej725sk8sn12g

    When the integration runs for the first time, Camel K builds the integration kit for the container image, which downloads all the required Camel modules and adds them to the image classpath.

  5. Enter kamel get again to verify that the integration is running:

    $ kamel get
    NAME       PHASE   KIT
    hello      Running myproject/kit-bq666mjej725sk8sn12g
  6. Enter the kamel log command to print the log to stdout:

    $ 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.0 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. Press Ctrl-C to terminate logging in the terminal.

Additional resources

3.5. Running Camel K integrations in development mode

You can run Camel K integrations in development mode on your OpenShift cluster from the command line. Using development mode, you can iterate quickly on integrations in development and get fast feedback on your code.

When you specify the kamel run command with the --dev option, this deploys the integration in the cloud immediately and shows the integration logs in the terminal. You can then change the code and see the changes automatically applied instantly to the remote integration Pod on OpenShift. The terminal automatically displays all redeployments of the remote integration in the cloud.

Note

The artifacts generated by Camel K in development mode are identical to those that you run in production. The purpose of development mode is faster development.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command with --dev to run your integration in development mode on OpenShift in the cloud. The following shows a simple Java example:

    $ 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.0 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. Edit the content of your integration DSL file, save your changes, and see the changes displayed instantly in the terminal. For example:

    ...
    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.0 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. Press Ctrl-C to terminate logging in the terminal.

Additional resources

3.6. Running Camel K integrations using modeline

You can use the Camel K modeline to specify multiple configuration options in a Camel K integration source file, which are executed at runtime. This creates efficiencies by saving you the time of re-entering multiple command line options and helps to prevent input errors.

The following example shows a modeline entry from a Java integration file that enables 3scale and limits the integration container memory.

Prerequisites

Procedure

  1. Add a Camel K modeline entry to your integration file. For example:

    ThreeScaleRest.java

    // 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
    Enables both the container and 3scale traits, to expose the route through 3scale and to limit the container memory.
  2. Run the integration, for example:

    kamel run ThreeScaleRest.java

    The kamel run command outputs any modeline options specified in the integration, for example:

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

Additional resources

Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.