Skip to content

Latest commit

 

History

History
 
 

kafka

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Kafka example : A Camel Quarkus example

{cq-description}

Tip
Check the Camel Quarkus User guide for prerequisites and other general information.

Prerequisites

The example application requires a Kafka instance.

You do not need to provide the Kafka instance yourself as long as you play with the example code in dev mode (a.k.a. mvn quarkus:dev - read more here or as long as you only run the supplied tests (mvn test). In those situations, Quarkus tooling starts a Strimzi image for you via Quarkus Dev Services and it also configures the application so that you do not need touch anything in application.properties.

Start in Development mode

Run the application in development mode.

Tip
If you want to use another running instance, in dev mode. Uncomment the corresponding Kafka configuration section in src/main/resources/application.properties and change %prod profile to %dev.
$ mvn clean compile quarkus:dev

The above command compiles the project, starts the application and lets the Quarkus tooling watch for changes in your workspace. Any modifications in your project will automatically take effect in the running application.

Tip
Please refer to the Development mode section of Camel Quarkus User guide for more details.

You should start to see some log messages appearing on the console.

Every 10 seconds the timer component triggers the generation of random Message and send it to the Kafka topic Test.

[FromTimer2Kafka] (Camel (camel-1) thread #2 - KafkaProducer[test]) Message sent correctly sent to the topic! : "Message #1"

Next a Kafka consumer reads the messages and put them in a seda queue.

[FromKafka2Seda] (Camel (camel-1) thread #0 - KafkaConsumer[test]) Received : "Message #1"

Next pull a message from the queue :

$ curl -X GET http://0.0.0.0:8080/example

Configure Kafka client, package and run the application

Once you are done with developing you may want to configure your kafka client, package and run the application.

Tip
Find more details about the JVM mode and Native mode in the Package and run section of Camel Quarkus User guide

Configure kafka client

Uncomment the corresponding commented section in src/main/resources/application.properties.

  • The section Kafka instance without Authentication if no Authentication required.

  • The section Kafka instance with SASL Plain if using SASL.

  • The section Kafka instance with SASL Oauth Bearer if using Oauth Bearer.

You need to set the corresponding environment variables: - Without Authentication

$ export brokers=<YOUR_KAFKA_BROKERS_URL>
  • SASL Plain

$ export brokers=<YOUR_KAFKA_BROKERS_URL>
$ export id=<YOUR_KAFKA_SASL_CLIENT_ID>
$ export secret=<YOUR_KAFKA_SASL_CLIENT_SECRET>

-SASL Oauth Bearer

$ export brokers=<YOUR_KAFKA_BROKERS_URL>
$ export id=<YOUR_KAFKA_SASL_CLIENT_ID>
$ export secret=<YOUR_KAFKA_SASL_CLIENT_SECRET>
$ export token=<YOUR_KAFKA_SASL_OAUTHBEARER_TOKEN_URL>

If you want to deploy on Kubernetes or Openshift, you’d need to define the above environment variables in a secret named camel-kafka. Set the needed values in the kubefiles/secret-example.yml, then add the secret :

$ kubectl apply -f kubefiles/secret-example.yml

JVM mode

$ mvn clean package -DskipTests
$ java -jar target/quarkus-app/quarkus-run.jar

Native mode

Important
Native mode requires having GraalVM and other tools installed. Please check the Prerequisites section of Camel Quarkus User guide.

To prepare a native executable using GraalVM, run the following command:

$ mvn clean package -DskipTests -Pnative
$ ./target/*-runner

Deploying to OpenShift

Uncomment the creating container with openshift and secrets, in the Openshift specific section in src/main/resources/application.properties.

$ mvn clean package -DskipTests -Dquarkus.kubernetes.deploy=true -Dopenshift

The openshift profile uses quarkus openshift and openshift-container extensions, as described in the pom.xml.

<dependencies>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-openshift</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-container-image-openshift</artifactId>
    </dependency>
</dependencies>

You can check the pod status and tail logs using the commands mentioned above in the Kubernetes section. Use the oc binary instead of kubectl if preferred.

Feedback

Please report bugs and propose improvements via GitHub issues of Camel Quarkus project.