Quarkus

Build a stock-enquiry service with Quarkus 3.20, expose health and Prometheus metrics, and run the JVM image on OpenShift. A native build is the same Deployment with a different image.

Updated CompTech Team

The Spring Boot lab deploys an Inventory API that adjusts on-hand quantities. This lab is the service a buyer would call next: stock enquiry. It answers whether a SKU should be reordered, it tells OpenShift whether it is alive, and it counts lookups for Prometheus. Quarkus 3.20 on Java 21. The cluster is the same OpenShift 4.x you used for Inventory.

The default image is the JVM on the UBI OpenJDK runtime. The native image is a second build. Probes and the Route stay the same.

Prerequisites

  • JDK 21 and Maven
  • Podman or Docker, if you want the native image built in a container rather than with a local GraalVM
  • OpenShift 4.x and oc, able to pull registry.access.redhat.com/ubi9/openjdk-21-runtime

Step 1: Create the project

mvn io.quarkus.platform:quarkus-maven-plugin:3.20.4:create \
  -DprojectGroupId=com.comptech \
  -DprojectArtifactId=stock-enquiry \
  -Dextensions='rest-jackson,smallrye-health,micrometer-registry-prometheus' \
  -DnoCode
cd stock-enquiry

src/main/resources/application.properties:

quarkus.http.port=8080
quarkus.http.host=0.0.0.0
stock.warehouse=${STOCK_WAREHOUSE:lab}
stock.reorder-point=${STOCK_REORDER_POINT:5}
stock.api-key=${STOCK_API_KEY:dev-key}

The three stock.* values are the contract with OpenShift. A ConfigMap supplies the warehouse and the reorder point. A Secret supplies the API key. Empty defaults would be wrong here: a missing key should fail readiness, not serve anonymously.

Step 2: Enquiry, health, counter

src/main/java/com/comptech/StockResource.java:

package com.comptech;

import io.micrometer.core.instrument.MeterRegistry;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.*;
import java.util.Map;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/stock")
public class StockResource {
    private static final Map<String, Integer> ON_HAND = Map.of(
        "SRV-RACK", 12, "GPU-NODE", 2, "TOR-SW", 7);

    private final MeterRegistry registry;
    private final int reorderPoint;
    private final String apiKey;

    public StockResource(MeterRegistry registry,
                         @ConfigProperty(name = "stock.reorder-point") int reorderPoint,
                         @ConfigProperty(name = "stock.api-key") String apiKey) {
        this.registry = registry;
        this.reorderPoint = reorderPoint;
        this.apiKey = apiKey;
    }

    @GET
    @Path("/{sku}")
    @Produces(MediaType.APPLICATION_JSON)
    public Stock stock(@HeaderParam("X-Api-Key") String key, @PathParam("sku") String sku) {
        if (!apiKey.equals(key)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        registry.counter("stock.lookups", "sku", sku).increment();
        int onHand = ON_HAND.getOrDefault(sku, 0);
        return new Stock(sku, onHand, onHand <= reorderPoint);
    }

    public record Stock(String sku, int onHand, boolean reorder) {}
}

src/main/java/com/comptech/WarehouseCheck.java:

package com.comptech;

import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Readiness;

@Readiness
@ApplicationScoped
public class WarehouseCheck implements HealthCheck {
    @ConfigProperty(name = "stock.warehouse")
    String warehouse;

    @ConfigProperty(name = "stock.api-key")
    String apiKey;

    @Override
    public HealthCheckResponse call() {
        boolean ok = warehouse != null && !warehouse.isBlank()
            && apiKey != null && !apiKey.isBlank();
        return HealthCheckResponse.named("config").status(ok).build();
    }
}

@Readiness is only on /q/health/ready. Live and started stay on the process checks, so a blank API key removes the pod from the Service and does not restart it. The same split is in the Spring Boot and .NET labs. The quantities are a copy of Inventory's seed, in this process. Enquiry does not call Inventory.

Step 3: Run it locally

./mvnw quarkus:dev
curl -s localhost:8080/stock/GPU-NODE -H 'X-Api-Key: dev-key'
# {"sku":"GPU-NODE","onHand":2,"reorder":true}

curl -s -o /dev/null -w '%{http_code}\n' localhost:8080/stock/GPU-NODE
# 401

curl -s localhost:8080/q/health/live
curl -s localhost:8080/q/health/ready
curl -s localhost:8080/q/metrics | grep stock_lookups

reorder is true because 2 is at or under 5. The lookup increments stock_lookups_total. Stop dev mode with Ctrl-C before you package.

Step 4: JVM image

Quarkus writes a fast-jar layout under target/quarkus-app/. The runtime image is the same UBI OpenJDK image the Spring lab uses. src/main/docker/Dockerfile.jvm:

FROM registry.access.redhat.com/ubi9/openjdk-21-runtime
ENV LANGUAGE='en_US:en' \
    JAVA_APP_JAR=/deployments/quarkus-run.jar \
    JAVA_OPTS_APPEND='-Dquarkus.http.host=0.0.0.0'
COPY --chown=185:0 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185:0 target/quarkus-app/*.jar /deployments/
COPY --chown=185:0 target/quarkus-app/app/ /deployments/app/
COPY --chown=185:0 target/quarkus-app/quarkus/ /deployments/quarkus/
EXPOSE 8080
USER 185
./mvnw -DskipTests package
podman build -f src/main/docker/Dockerfile.jvm -t stock-enquiry:jvm .
podman run --rm -p 8080:8080 -e STOCK_API_KEY=dev-key stock-enquiry:jvm

Add target/ to .dockerignore. The image listens on 8080 as user 185, which is what OpenShift's restricted policy expects.

Step 5: Deploy

oc new-project stock
oc new-build --binary --strategy=docker --name=stock-enquiry \
  --dockerfile-path=src/main/docker/Dockerfile.jvm
oc start-build stock-enquiry --from-dir=. --follow
oc create configmap stock-config --from-literal=warehouse=dhk-1 --from-literal=reorder-point=5
oc create secret generic stock-secret --from-literal=api-key=change-me

deploy/stock.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: stock-enquiry
spec:
  replicas: 2
  selector:
    matchLabels: { app: stock-enquiry }
  template:
    metadata:
      labels: { app: stock-enquiry }
    spec:
      containers:
        - name: stock-enquiry
          image: image-registry.openshift-image-registry.svc:5000/stock/stock-enquiry:latest
          ports: [{ containerPort: 8080 }]
          env:
            - name: STOCK_WAREHOUSE
              valueFrom: { configMapKeyRef: { name: stock-config, key: warehouse } }
            - name: STOCK_REORDER_POINT
              valueFrom: { configMapKeyRef: { name: stock-config, key: reorder-point } }
            - name: STOCK_API_KEY
              valueFrom: { secretKeyRef: { name: stock-secret, key: api-key } }
          startupProbe:
            httpGet: { path: /q/health/started, port: 8080 }
            failureThreshold: 30
            periodSeconds: 2
          readinessProbe:
            httpGet: { path: /q/health/ready, port: 8080 }
            periodSeconds: 10
          livenessProbe:
            httpGet: { path: /q/health/live, port: 8080 }
            periodSeconds: 10
          resources:
            requests: { cpu: 50m, memory: 128Mi }
            limits: { memory: 384Mi }
---
apiVersion: v1
kind: Service
metadata:
  name: stock-enquiry
  labels: { app: stock-enquiry }
spec:
  selector: { app: stock-enquiry }
  ports: [{ port: 80, targetPort: 8080 }]
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: stock-enquiry
spec:
  to: { kind: Service, name: stock-enquiry }
  tls: { termination: edge, insecureEdgeTerminationPolicy: Redirect }
oc apply -f deploy/stock.yaml
oc rollout status deployment/stock-enquiry
KEY=$(oc get secret stock-secret -o jsonpath='{.data.api-key}' | base64 -d)
HOST=$(oc get route stock-enquiry -o jsonpath='{.spec.host}')
curl -sk "https://$HOST/stock/GPU-NODE" -H "X-Api-Key: $KEY"
curl -sk "https://$HOST/q/health/ready"
curl -sk "https://$HOST/q/metrics" | grep stock_lookups

/q/metrics is on port 8080, the same listener as the API. A ServiceMonitor for user-workload monitoring uses the Service port and path /q/metrics. Until an administrator enables that monitoring, the curl above is the check. Two replicas do not share the counter. Prometheus adds the series if you scrape both pods.

The reorder point is read in the constructor. Change it, then restart:

oc set data configmap/stock-config reorder-point=1
oc rollout restart deployment/stock-enquiry

GPU-NODE (on-hand 2) is no longer a reorder. Clear the key and readiness should go DOWN while liveness stays up. If the pod restarts, the liveness probe is on the wrong path.

oc set env deployment/stock-enquiry STOCK_API_KEY-
oc get pods
oc apply -f deploy/stock.yaml

Step 6: Native image

The JVM image is the one to debug. The native image starts without a JVM. Build it in a container so the workstation does not need GraalVM, then point the Deployment at that tag. Probe paths stay put.

./mvnw -DskipTests package -Dnative -Dquarkus.native.container-build=true
podman build -f src/main/docker/Dockerfile.native -t stock-enquiry:native .
oc set image deployment/stock-enquiry \
  stock-enquiry=image-registry.openshift-image-registry.svc:5000/stock/stock-enquiry:native
oc rollout status deployment/stock-enquiry
curl -sk "https://$HOST/q/health/live"

Dockerfile.native is generated beside Dockerfile.jvm. A cluster publish is a second build with that Dockerfile path. If the build pod is evicted, raise its memory. Read the running pod with oc adm top pod before you change the Deployment limit.

Troubleshooting

Symptom Likely cause Fix
/q/metrics is 404 Prometheus registry extension missing ./mvnw quarkus:add-extension -Dextensions=micrometer-registry-prometheus, then rebuild
Ready never comes up STOCK_API_KEY or STOCK_WAREHOUSE is empty oc exec deploy/stock-enquiry -- printenv STOCK_API_KEY
Image exits at once Packaged before target/quarkus-app existed ./mvnw package, then oc start-build
HTTP 401 Header missing, or the shell key is stale Read the Secret again and send X-Api-Key

What's next

Spring Boot is the Inventory API these SKUs were copied from. ShopLite on .NET adds HTTP between services, a database per service, and Kafka. OpenTelemetry is the trace half. Service mesh is mTLS once Enquiry actually calls Inventory.