Getting Started with Quarkus on OpenShift (Part 1)
Introduction
Quarkus is an open source, Kubernetes-native Java (TM) framework that allows developers to optimize container-native applications for peak performance. Quarkus offers:
- Fast startup times
- Low memory consumption
- Reduced overall host costs
- Improved Java app performance
- Increased developer productivity
This article shows you how to get started with Quarkus by building a microservice and deploying it on the Red Hat OpenShift Container Platform.
Prerequisites
As prerequisites for Quarkus development you will need to download:
- Maven version 3.6.2 or newer
- Java JDK 11
- Access to an OpenShift cluster (you can use CodeReady Containers)
oc
orkubectl
curl
The commands below were run on Linux using the bash
shell. The commands will work on Windows if the line continuation characters is changed to a dash (-).
Using Maven to Create a Quarkus Project
We can use Maven to create our first Quarkus microservice project. Run the following command:
mvn io.quarkus:quarkus-maven-plugin:1.12.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-DclassName=”org.acme.getting.started.GreetingResource” \
-Dpath=”/hello”
Maven will download a number of dependencies and then create the project. When Maven has finished, change into the project’s directory:
cd getting-started
Optionally import the directory into your favorite IDE and review the directories and files. The directory includes a sample Java REST application, test cases and resources as well as Dockefiles that will allow you to create a container image of your application.
Building and Running Our First Application
In order to build and run/debug our first application run the following command:
./mvnw compile quarkus:dev
In another terminal window, perform a REST invocation of the service by running:
curl http://localhost:8080/hello
The output should be:
Hello RESTEasy
Hot Code Swapping
Now that we’ve seen how easy it is to build and run a Quarkus application, we will see how debugging a program is fast and easy thanks to a cool feature called hot code swapping. Leave the program running. In the file named GreetingResource.java
change the greeting message from Hello RestEasy
to Hello Debug Easy
and save the file. Now run the curl
command again. Even without recompiling or repackaging the application, the output should now be:
Hello Debug Easy
The code hot swap greatly reduces cycle time of the edit/compile/debug loop.
Restore the greeting to its original value Hello RestEasy
in order to pass the built-in test suite in the next section.
Running on OpenShift
In order to configure a Maven targets for OpenShift, we first need to add the OpenShift extension to the project by running the following:
./mvnw quarkus:add-extension -Dextensions="openshift"
Create a project on OpenShift for the application by running:
oc new-project quarkus-getting-started
We will now, build and deploy the application, and create an OpenShift route
to access the application from outside of OpenShift by running:
./mvnw package -Dquarkus.kubernetes.deploy=true \
-Dquarkus.openshift.expose=true
Watch the output of oc get pods
and wait until the build
and deploy
pods have completed and a getting-started-1-xxxxx
pod is running. For example:
$ oc get pods
NAME READY STATUS RESTARTS AGE
getting-started-1-build 0/1 Completed 0 6m54s
getting-started-1-deploy 0/1 Completed 0 54s
getting-started-1-mcdgf 1/1 Running 0 44s
Get the route that was created by running:
oc get route getting-started -o jsonpath='{.spec.host}{"\n"}'
Test the application by perform a REST invocation from your terminal window to the service running on OpenShift:
curl http://$(oc get route getting-started -o jsonpath='{.spec.host}')/hello
The output should be:
Hello RESTEasy
We now have a working Quarkus microservice running on OpenShift.