Skip to content

Latest commit

 

History

History

greeting

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

greeting-service

This project uses Quarkus, the Supersonic Subatomic Java Framework.

If you want to learn more about Quarkus, please visit its website: https://quarkus.io/ .

Find more Quarkus QuickStart samples here

Prerequisites

  1. A working container runtime (Docker, podman)

  2. JDK 21 installed with JAVA_HOME configured appropriately

    sdk install java 21-graal
    export JAVA_HOME=$HOME/.sdkman/candidates/java/current
    export GRAALVM_HOME=$HOME/.sdkman/candidates/java/21-graal
  3. Install the native-image tool using gu install:

    ${GRAALVM_HOME}/bin/gu install native-image
  4. brew install httpie

  5. Adding an empty META-INF/beans.xml to src/main/resources of dependency project, sothat POJO classes will also be indexed by Quarkus

    Ref: https://stackoverflow.com/questions/55513502/how-to-create-a-jandex-index-in-quarkus-for-classes-in-a-external-module

scaffolding projects

your can also use code.quarkus.io webApp to generate a new project

cd apps
mvn io.quarkus:quarkus-maven-plugin:2.11.2.Final:create \
    -DprojectGroupId=micro.apps \
    -DprojectArtifactId=greeting-service \
    -DprojectVersion=0.1.0 \
    -DclassName="micro.apps.service.GreetingResource" \
    -Dpath="/greeting" \
    -Dextensions="kotlin, config-yaml, resteasy-jsonb, oidc, kubernetes, jib, grpc" \
    -DbuildTool=gradle
cd ..

Running the application in dev mode

You can run your application in dev mode that enables live coding using:

gradle :services:greeting:quarkusDev
# You can also run a Quarkus application in debug mode with a suspended JVM using:
gradle :services:greeting:quarkusDev -Dsuspend -Ddebug
# Then, attach your debugger to localhost:5005.

In development mode, Quarkus starts by default with debug mode enabled, listening to port 5005 without suspending the JVM.

Packaging and running the application

The application can be packaged using

gradle :services:greeting:quarkusBuild

It produces the greeting-service-0.1.0-SNAPSHOT-runner.jar file in the /build directory. Be aware that it’s not an _ über-jar_ as the dependencies are copied into the build/lib directory.

If you want to build an über-jar, execute the following command:

gradle :services:greeting:quarkusBuild --uber-jar

The application is now runnable using java -jar build/greeting-service-0.1.0-SNAPSHOT-runner.jar.

Creating a native executable

You can create a native executable using:

# ignore error: The native binary produced by the build is not a Linux binary
gradle :services:greeting:build -Dquarkus.package.type=native

You can then execute your native executable with: ./apps/greeting-service/build/greeting-service-1.6.5-SNAPSHOT-runner

Or, if you don't have GraalVM installed, you can run the native executable build in a container using:

gradle :services:greeting:build -Dquarkus.package.type=native -Dquarkus.native.container-build=true
gradle :services:greeting:build -Dquarkus.package.type=native -Dquarkus.native.container-build=true -Dquarkus.native.native-image-xmx=8g

If you want to learn more about building native executables, please consult https://quarkus.io/guides/gradle-tooling.

Docker

quarkusBuild command also creates docker image if quarkus-container-image-jib plugin is enabled.

Check with docker images

quarkusBuild command also creates kubernetes YAML if quarkus-kubernetes plugin is enabled.

Check apps/greeting-service/build/kubernetes/kubernetes.yml

You can run image with docker run -i --rm -p 8080:8080 xmlking/greeting-service:1.6.5-SNAPSHOT

Manual Docker Build

# build jar/native first
gradle :services:greeting:quarkusDev
# or native
gradle :services:greeting:buildNative --docker-build=true

Then build docker image

cd apps/greeting-service/

# DOCKER_REGISTRY=us.gcr.io
DOCKER_REGISTRY=docker.pkg.github.com
DOCKER_CONTEXT_PATH=xmlking/micro-apps
TARGET=greeting-service
VERSION=1.6.5-SNAPSHOT

IMANGE_NAME=${DOCKER_REGISTRY:+${DOCKER_REGISTRY}/}${DOCKER_CONTEXT_PATH}/${TARGET}:${VERSION}

# with jvm 
docker build -f src/main/docker/Dockerfile.jvm -t $IMANGE_NAME .
# or with native
docker build -f src/main/docker/Dockerfile.native -t $IMANGE_NAME .

docker run -i --rm -p 8080:8080 $IMANGE_NAME

# check
docker inspect  $IMANGE_NAME

# push
docker push $IMANGE_NAME

# remove temp images after build
docker image prune -f

Testing

REST

gradle :services:greeting:quarkusDev
open http://localhost:8080/q/swagger-ui/

Swagger UI: http://localhost:8080/q/swagger-ui/ Health UI: http://localhost:8080/q/health-ui

http :8080/q/health
http :8080/q/health/live
http :8080/q/health/ready

http :8080/q/openapi
http :8080/q/metrics
http :8080/q/metrics/application

http :8080/api/v1/greeting
http :8080/api/v1/greeting/world

http :8080/api/fruits

http :8080/api/v1/account/id/abc123
http :8080/api/v1/account/user-info 'Authorization:Bearer GOOGLE.TOKEN'

GRPC

# no TLS
grpcurl -plaintext \
-protoset <(buf build -o -) \
-d '{ "message":  "sumo" }' 0.0.0.0:9000 micro.apps.proto.echo.v1.EchoService/Echo

grpcurl -plaintext \
-protoset <(buf build -o -) \
-d '{ "message":  "sumo" }' 0.0.0.0:9000 micro.apps.proto.echo.v1.EchoService/EchoStream

Unit tests

gradle :services:greeting:check # Runs all checks
gradle :services:greeting:test # Runs the unit tests.
gradle :services:greeting:testNative # Runs native image tests

Gradle

gradle :services:greeting:listExtensions
gradle :services:greeting:addExtension --extensions="health,metrics,openapi"
gradle :services:greeting:addExtension --extensions="hibernate-validator"
gradle :services:greeting:addExtension --extensions="jdbc,agroal,non-exist-ent"
gradle :services:greeting:addExtension --extensions="container-image-jib, kubernetes"

gradle :services:greeting:quarkusDev -Dsuspend -Ddebug

gradle :services:greeting:buildNative
gradle :services:greeting:testNative

Reference