Using Quarkus applications with Kafka instances in Red Hat OpenShift Streams for Apache Kafka

Guide
  • Red Hat OpenShift Streams for Apache Kafka 1
  • Updated 19 October 2021
  • Published 13 April 2021

Using Quarkus applications with Kafka instances in Red Hat OpenShift Streams for Apache Kafka

Guide
Red Hat OpenShift Streams for Apache Kafka 1
  • Updated 19 October 2021
  • Published 13 April 2021

Red Hat OpenShift Streams for Apache Kafka is currently available for Development Preview. Development Preview releases provide early access to a limited set of features that might not be fully tested and that might change in the final GA version. Users should not use Development Preview software in production or for business-critical workloads. Limited documentation is available for Development Preview releases and is typically focused on fundamental user goals.

As a developer of applications and services, you can connect Quarkus applications to Kafka instances in Red Hat OpenShift Streams for Apache Kafka. Quarkus is a Kubernetes-native Java framework made for Java virtual machines (JVMs) and native compilation, and optimized for serverless, cloud, and Kubernetes environments. Quarkus is designed to work with popular Java standards, frameworks, and libraries like Eclipse MicroProfile and Spring, as well as Apache Kafka, RESTEasy (JAX-RS), Hibernate ORM (JPA), Infinispan, Camel, and many more.

Prerequisites
  • You have a Red Hat account.

  • You have a running Kafka instance in OpenShift Streams for Apache Kafka.

  • Git is installed.

  • You have an IDE such as IntelliJ IDEA, Eclipse, or VSCode.

  • JDK 11 or later is installed.

  • Apache Maven 3.6.2 or later is installed.

  • For Windows, the latest version of Oracle JDK is installed.

Importing the Quarkus sample code

For this quick start, you’ll use the Quarkus sample code from the OpenShift Streams for Apache Kafka Guides and Samples repository in GitHub. After you understand the concepts and tasks in this quick start, you can use your own Quarkus applications with OpenShift Streams for Apache Kafka in the same way.

Procedure
  1. On the command line, clone the OpenShift Streams for Apache Kafka Guides and Samples repository from GitHub.

    Cloning the guides and samples repository
    git clone https://github.com/redhat-developer/app-services-guides app-services-guides
  2. In your IDE, open the code-examples/quarkus-kafka-quickstart directory from the repository that you cloned.

Configuring the Quarkus example application to connect to a Kafka instance

To enable your Quarkus application to access a Kafka instance, configure the connection using the bootstrap server endpoint, the generated credentials for your OpenShift Streams for Apache Kafka service account, and the SASL/OAUTHBEARER token endpoint for the Kafka instance. For Quarkus, you can configure connection information by using the application.properties configuration file. The example in this task sets environment variables and then references them in the application.properties file.

Quarkus applications use MicroProfile Reactive Messaging to produce messages to and consume messages from your Kafka instances in OpenShift Streams for Apache Kafka. For more information about Quarkus configuration options for Kafka and Reactive Messaging, see Using Apache Kafka with Reactive Messaging in the Quarkus documentation.

Prerequisites
  • You have the bootstrap server endpoint and the SASL/OAUTHBEARER token endpoint for the Kafka instance. To relocate the server endpoint and the SASL/OAUTHBEARER token endpoint, select your Kafka instance in the OpenShift Streams for Apache Kafka web console, select the options menu (three vertical dots), and click Connection.

  • You have the generated credentials for your service account. To regenerate the credentials, use the Service Accounts page in the OpenShift Streams for Apache Kafka web console to find your service account and update the credentials.

  • You’ve set the permissions for your service account to access the Kafka instance resources. To verify the current permissions, select your Kafka instance in the OpenShift Streams for Apache Kafka web console and use the Access page to find your service account permission settings.

Procedure
  1. On the command line, set the Kafka instance bootstrap server and client credentials as environment variables to be used by Quarkus or other applications. Replace the values with your own server and credential information.

    Setting environment variables for server and credentials
    $ export BOOTSTRAP_SERVER=<bootstrap_server>
    $ export CLIENT_ID=<client_id>
    $ export CLIENT_SECRET=<client_secret>
    $ export OAUTH_TOKEN_ENDPOINT_URI=<oauth_token_endpoint_uri>
  2. In the Quarkus example application, review the src/main/resources/application.properties file to understand how the environment variables you set in the previous step are used in your application. This example uses the dev configuration profile in the application.properties file.

Creating the prices Kafka topic in OpenShift Streams for Apache Kafka

For this quick start, the Kafka topic that the Quarkus example application references is called prices. You need to create this topic in OpenShift Streams for Apache Kafka so that the Quarkus application can interact with it.

Prerequisites
  • You’ve created a Kafka instance in OpenShift Streams for Apache Kafka and the instance is in Ready state.

Procedure
  1. In the OpenShift Streams for Apache Kafka web console, go to Streams for Apache Kafka > Kafka Instances and click the name of the Kafka instance that you want to add a topic to.

  2. Select the Topics tab, click Create topic, and follow the guided steps to define the topic details. Click Next to complete each step and click Finish to complete the setup.

    Image of wizard to create a topic
    Figure 1. Guided steps to define topic details
    • Topic name: Enter prices as the topic name.

    • Partitions: Set the number of partitions for this topic. This example sets the partition to 1 for a single partition. Partitions are distinct lists of messages within a topic and enable parts of a topic to be distributed over multiple brokers in the cluster. A topic can contain one or more partitions, enabling producer and consumer loads to be scaled.

    • Message retention: Set the message retention time and size to the relevant value and increment. This example sets the retention time to A week and the retention size to Unlimited. Message retention time is the amount of time that messages are retained in a topic before they are deleted or compacted, depending on the cleanup policy. Retention size is the maximum total size of all log segments in a partition before they are deleted or compacted.

    • Replicas: For this release of OpenShift Streams for Apache Kafka, the replicas are preconfigured. The number of partition replicas for the topic is set to 3 and the minimum number of follower replicas that must be in sync with a partition leader is set to 2. Replicas are copies of partitions in a topic. Partition replicas are distributed over multiple brokers in the cluster to ensure topic availability if a broker fails. When a follower replica is in sync with a partition leader, the follower replica can become the new partition leader if needed.

      After you complete the topic setup, the new Kafka topic is listed in the topics table. You can now run the Quarkus application to start producing and consuming messages to and from this topic.

Verification
  • Verify that the new Kafka topic prices is listed in the topics table.

Running the Quarkus example application

After you configure your Quarkus application to connect to a Kafka instance and you create the Kafka topic, you can run the Quarkus application to start producing and consuming messages to and from the topic.

The Quarkus example application in this quick start has three application-scoped Java classes:

  • One class generates a random number between 0 and 100 and produces it to a Kafka topic.

  • Another class consumes the number from the Kafka topic.

  • A final class exposes the number as a REST UI (using Server Sent events).

Prerequisites
  • You’ve configured the Quarkus example application to connect to the Kafka instance.

  • You’ve created the prices example Kafka topic.

Procedure
  1. On the command line, navigate to the code-examples/quarkus-kafka-quickstart directory that you imported and run the Quarkus example application in developer mode.

    Running the Quarkus example application
    $ cd ~/code-examples/quarkus-kafka-quickstart
    $ ./mvnw quarkus:dev
  2. After the application is running, in a web browser, go to http://localhost:8080/prices.html and verify that the Last price is updated.

    If the Quarkus application fails to run, review the error log in the terminal and address any problems. Also review the steps in this quick start to ensure that the Quarkus application and Kafka topic are configured correctly.