Important
|
Application Services 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 Application Services. 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.
-
You have a running Kafka instance in App Services.
-
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.
For this quick start, you’ll use the Quarkus sample code from the App Services 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 App Services in the same way.
-
On the command line, clone the App Services Guides and Samples repository from GitHub.
Cloning the guides and samples repositorygit clone https://github.com/redhat-developer/app-services-guides app-services-guides
-
In your IDE, open the
code-examples/quarkus-kafka-quickstart
directory from the repository that you cloned.
To enable your Quarkus application to access a Kafka instance, configure the connection using the bootstrap server endpoint, the generated credentials for your App Services 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 App Services. For more information about Quarkus configuration options for Kafka and Reactive Messaging, see Using Apache Kafka with Reactive Messaging in the Quarkus documentation.
-
You have the bootstrap server endpoint, the service account credentials, and the SASL/OAUTHBEARER token endpoint for the Kafka instance. You copied this information previously for the Kafka instance in App Services by selecting the options menu (three vertical dots) and clicking Connection.
-
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>
-
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 thedev
configuration profile in theapplication.properties
file.
For this quick start, the Kafka topic that the Quarkus example application references is called prices
. You need to create this topic in App Services so that the Quarkus application can interact with it.
-
You’ve created a Kafka instance in App Services and the instance is in Ready state.
-
In the App Services 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.
-
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.
-
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 toUnlimited
. 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 App Services, 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 to2
. 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.
-
-
Verify that the new Kafka topic
prices
is listed in the topics table.
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).
-
You’ve configured the Quarkus example application to connect to the Kafka instance.
-
You’ve created the
prices
example Kafka topic.
-
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
-
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.