此内容没有您所选择的语言版本。
Chapter 3. Getting started with Camel K
This chapter explains how to set up your development environment and how to develop and deploy simple Camel K integrations written in Java, XML, 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,
- Section 3.1, “Setting up your Camel K development environment”
- Section 3.2, “Developing Camel K integrations in Java”
- Section 3.3, “Developing Camel K integrations in XML”
- Section 3.4, “Developing Camel K integrations in YAML”
- Section 3.5, “Running Camel K integrations”
- Section 3.6, “Running Camel K integrations in development mode”
- Section 3.7, “Running Camel K integrations using modeline”
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.
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
You must have access to an OpenShift cluster on which the Camel K Operator and OpenShift Serverless Operator are installed:
- Section 2.3, “Installing the Camel K and OpenShift command line tools”
Procedure
Install VS Code on your development platform. For example, on Red Hat Enterprise Linux:
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'
$ 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'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the cache and install the VS Code package:
yum check-update sudo yum install code
$ yum check-update $ sudo yum install code
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For details on installing on other platforms, see the VS Code installation documentation.
-
Enter the
code
command to launch the VS Code editor. For more details, see the VS Code command line documentation. Install the VS Code Camel Extension Pack, which includes the extensions required for Camel K. For example, in VS Code:
- In the left navigation bar, click Extensions.
- In the search box, enter Apache Camel.
Select the Extension Pack for Apache Camel by Red Hat, and click Install.
For more details, see the instructions for the Extension Pack for Apache Camel by Red Hat.
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:
- In the left navigation bar, click Extensions.
- In the search box, enter Didact.
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
Enter the
kamel init
command to generate a simple Java integration file. For example:kamel init HelloCamelK.java
$ kamel init HelloCamelK.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open the generated integration file in your IDE and edit as appropriate. For example, the
HelloCamelK.java
integration automatically includes the Cameltimer
andlog
components to help you get started:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
3.3. Developing Camel K integrations in XML 复制链接链接已复制到粘贴板!
This section explains how to develop a simple Camel K integration in classic XML DSL. Writing an integration in XML 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
Enter the
kamel init
command to generate a simple XML integration file. For example:kamel init hello-camel-k.xml
$ kamel init hello-camel-k.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open the generated integration file in your IDE and edit as appropriate. For example, the
hello-camel-k.xml
integration automatically includes the Cameltimer
andlog
components to help you get started:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
3.4. 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
Enter the
kamel init
command to generate a simple XML integration file. For example:kamel init hello.camelk.yaml
$ kamel init hello.camelk.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Open the generated integration file in your IDE and edit as appropriate. For example, the
hello.camelk.yaml
integration automatically includes the Cameltimer
andlog
components to help you get started:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIntegrations written in YAML must have a file name with the pattern
*.camelk.yaml
or a first line of# camel-k: language=yaml
.
Additional resources
3.5. 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
- Section 3.1, “Setting up your Camel K development environment”.
- You must already have a Camel integration written in Java, XML, or YAML DSL.
Procedure
Log into your OpenShift cluster using the
oc
client tool, for example:oc login --token=my-token --server=https://my-cluster.example.com:6443
$ oc login --token=my-token --server=https://my-cluster.example.com:6443
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that the Camel K Operator is running, for example:
oc get pod
$ oc get pod NAME READY STATUS RESTARTS AGE camel-k-operator-86b8d94b4-pk7d6 1/1 Running 0 6m28s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the
kamel run
command to run your integration in the cloud on OpenShift. For example:Java example
kamel run HelloCamelK.java
$ kamel run HelloCamelK.java integration "hello-camel-k" created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow XML example
kamel run hello-camel-k.xml
$ kamel run hello-camel-k.xml integration "hello-camel-k" created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow YAML example
kamel run hello.camelk.yaml
$ kamel run hello.camelk.yaml integration "hello" created
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the
kamel get
command to check the status of the integration:kamel get
$ kamel get NAME PHASE KIT hello Building Kit kit-bq666mjej725sk8sn12g
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
Enter
kamel get
again to verify that the integration is running:kamel get
$ kamel get NAME PHASE KIT hello Running kit-bq666mjej725sk8sn12g
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the
kamel log
command to print the log tostdout
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Press
Ctrl-C
to terminate logging in the terminal.
Additional resources
-
For more details on the
kamel run
command, enterkamel run --help
- For faster deployment turnaround times, see Section 3.6, “Running Camel K integrations in development mode”
- For details of development tools to run integrations, see VS Code Tooling for Apache Camel K by Red Hat
- See also Section 5.1, “Managing Camel K integrations”
3.6. 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.
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
- Section 3.1, “Setting up your Camel K development environment”.
- You must already have a Camel integration written in Java, XML, or YAML DSL.
Procedure
Log into your OpenShift cluster using the
oc
client tool, for example:oc login --token=my-token --server=https://my-cluster.example.com:6443
$ oc login --token=my-token --server=https://my-cluster.example.com:6443
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that the Camel K Operator is running, for example:
oc get pod
$ oc get pod NAME READY STATUS RESTARTS AGE camel-k-operator-86b8d94b4-pk7d6 1/1 Running 0 6m28s
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the content of your integration DSL file, save your changes, and see the changes displayed instantly in the terminal. For example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Press
Ctrl-C
to terminate logging in the terminal.
Additional resources
-
For more details on the
kamel run
command, enterkamel run --help
- For details of development tools to run integrations, see VS Code Tooling for Apache Camel K by Red Hat
- Section 5.1, “Managing Camel K integrations”
- Section 7.6, “Configuring Camel K integration dependencies”
3.7. 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 configures traits for Prometheus monitoring and 3scale API Management, and includes a dependency on an external Maven library:
// camel-k: language=java trait=prometheus.enabled=true trait=3scale.enabled=true dependency=mvn:org.my/app:1.0
// camel-k: language=java trait=prometheus.enabled=true trait=3scale.enabled=true dependency=mvn:org.my/app:1.0
Prerequisites
- Section 3.1, “Setting up your Camel K development environment”.
- You must already have a Camel integration written in Java, XML, or YAML DSL.
Procedure
Add a Camel K modeline entry to your integration file. For example:
Hello.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command to run the integration:
kamel run Hello.java
$ kamel run Hello.java Modeline options have been loaded from source files Full command: kamel run Hello.java --trait=prometheus.enabled=true --dependency mvn:org.my/application:1.0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
kamel run
command outputs any modeline options specified in the integration.
Additional resources
- Section 9.2, “Camel K modeline options”
- For details of development tools to run modeline integrations, see Introducing IDE support for Apache Camel K Modeline.