The quickstart can run with any Kafka instance. However we want to focus how to connect a Red Hat OpenShift Streams for Apache Kafka to Camel. In order to use it, you must login to the Red Hat Cloud (Beta). Please, consider the due limitations of this offering at the time of writing this tutorial.
In order to setup an instance and your first topic, you can follow up the UI quickstart or use the rhoas CLI.
Note: we assume in this tutorial that you are creating a topic named
test
. You can use any other name, just make sure to reflect the name chosen in theapplication.properties
configuration file.
Once you’ve setup your first topic, you must create a set of credentials that you will be using during this quickstart:
At this stage you should have the following credentials: a
kafka bootstrap URL
, a service account id
and a
service account secret
.
This example demonstrates using the camel-kafka component with Red Hat Fuse on Spring Boot to produce and consume kafka messages.
In this example, a Camel route produce at a fixed rate of 5s, String messages that are sent to kafka. A second route is subscribed to the kafka topic and print out the messages received.
-
Maven
-
Configured kafka instance, topic and credentials on RHOSAK
-
A java version that support TLS 1.3 protocol, eg. from Oracle JDK 8u261-b12 or AdoptOpenJDK 8u272-b10 or java 11 (
fuse-java-openshift-jdk11-rhel8
image is used to build in java 11).
To run the example
-
Change your working directory to
resources
directory. -
edit
application.properties
with parameters from RHOSAK, in particular:## Modify value of <FILL_ME> before running application kafka.brokers = <FILL_ME> # Service Account info sa.client.id=<FILL_ME> sa.client.secret=<FILL_ME>
-
Build the project
mvn clean package
-
Start the fat jar
mvn spring-boot:run
In the logs, you should see somethig like this:
2021-06-04 12:27:04.807 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate 2021-06-04 12:27:09.825 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate 2021-06-04 12:27:14.764 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate 2021-06-04 12:27:19.848 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate 2021-06-04 12:27:24.765 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate 2021-06-04 12:27:29.762 INFO 1106408 --- [aConsumer[test]] kafka-consumer : Greetings at fixed rate
Important
|
You need to run this example on Container Development Kit 3.3 or OpenShift 3.7. Both of these products have suitable Fuse images pre-installed. If you run it in an environment where those images are not preinstalled follow the steps described in Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images. |
To deploy this quickstart to a running OpenShift cluster:
-
Download the project and extract the archive on your local filesystem.
-
Log in to your OpenShift cluster:
$ oc login -u developer -p developer
-
Create a new OpenShift project for the quickstart:
$ oc new-project MY_PROJECT_NAME
-
Change the directory to the folder that contains the extracted quickstart application (for example,
my_openshift/spring-boot-camel-rhosak
) :
$ cd my_openshift/spring-boot-camel-rhosak
-
Build and deploy the project to the OpenShift cluster:
$ mvn clean -DskipTests oc:deploy -Popenshift
-
List all the running pods:
$ oc get pods
-
Find the name of the pod that runs this quickstart. Output the logs from the running pods and see the messages sent by Camel:
$ oc logs <name of pod>
A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.
If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your quickstart there.
-
Log in to your OpenShift cluster:
$ oc login -u developer -p developer
-
Create a new OpenShift project for the quickstart:
$ oc new-project MY_PROJECT_NAME
-
Import base images in your newly created project (MY_PROJECT_NAME) according to documentation.
-
Change the directory to the folder that contains the extracted quickstart application (for example,
my_openshift/spring-boot-camel-rhosak
) :$ cd my_openshift/spring-boot-camel-rhosak
-
Build and deploy the project to the OpenShift cluster:
$ mvn clean -DskipTests oc:deploy -Popenshift -Djkube.generator.fromMode=istag -Djkube.generator.from=MY_PROJECT_NAME/fuse7-java-openshift:1.8
-
In your browser, navigate to the
MY_PROJECT_NAME
project in the OpenShift console. Wait until you can see that the pod for thespring-boot-camel-rhosak
has started up. -
On the project’s
Overview
page, navigate to the details page deployment of thespring-boot-camel-rhosak
application:https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/pods/spring-boot-camel-rhosak-NUMBER_OF_DEPLOYMENT?tab=details
. -
Switch to tab
Logs
and then see the messages sent by Camel.