Spring Boot Camel Red Hat OpenShift Streams for Apache Kafka Quickstart

Preparing the Kafka instance

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.

Create an instance and a topic

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 the application.properties configuration file.

Prepare Kafka credentials

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.

Standalone

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.

Prerequisites

  • 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).

Running the example

To run the example

  1. Change your working directory to resources directory.

  2. 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>

Running the example on Red Hat Fuse Standalone

  1. Build the project

     mvn clean package
  2. 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

Running the example on Openshift

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:

  1. Download the project and extract the archive on your local filesystem.

  2. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  3. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  4. 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
  1. Build and deploy the project to the OpenShift cluster:

    $ mvn clean -DskipTests oc:deploy -Popenshift
  2. List all the running pods:

    $ oc get pods
  3. 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>

Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images

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.

  1. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  2. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  3. Import base images in your newly created project (MY_PROJECT_NAME) according to documentation.

  4. 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
  5. 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
  6. In your browser, navigate to the MY_PROJECT_NAME project in the OpenShift console. Wait until you can see that the pod for the spring-boot-camel-rhosak has started up.

  7. On the project’s Overview page, navigate to the details page deployment of the spring-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.

  8. Switch to tab Logs and then see the messages sent by Camel.