* chore: use buildx to build grpc-ping-go Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support grpc-ping-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-clojure multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-dart multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-deno multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-elixir multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-haskell multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-java-micronaut multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-java-quarkus multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-r multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-rserver multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-rust multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-swift multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-vertx multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support iris-classifier multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-python multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support node-heartbeat-source multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support cloudevents-dotnet multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support cloudevents-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support cloudevents-nodejs multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support cloudevents-rust multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support gitwebhook-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-csharp multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-java-spark multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-java-spring multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-kotlin multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-nodejs multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-php multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-python multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-shell multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support knative-routing-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support kong-routing-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support multi-container multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support secrets-go multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support knative-docs multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-r Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support helloworld-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support cloudevents-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support gitwebhook-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support knative-routing-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support kong-routing-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support servingcontainer Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support sidecarcontainer Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support secrets-go Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> * chore: support multi-container Dockerfile multiple architecture Signed-off-by: kahirokunn <okinakahiro@gmail.com> --------- Signed-off-by: kahirokunn <okinakahiro@gmail.com> |
||
---|---|---|
.. | ||
.mvn/wrapper | ||
src | ||
.dockerignore | ||
.editorconfig | ||
.gitignore | ||
Dockerfile | ||
Dockerfile.native | ||
README.md | ||
index.md | ||
mvnw | ||
mvnw.cmd | ||
pom.xml | ||
service.yaml |
README.md
Hello World - Java (Quarkus)
A simple JAX-RS REST API application that is written in Java and uses Quarkus.
This samples uses Docker to build locally. The app reads in a TARGET
env
variable and then prints "Hello World: ${TARGET}!". If a value for TARGET
is
not specified, the "NOT SPECIFIED" default value is used.
Before you begin
You must meet the following requirements to run this sample:
- Have a Kubernetes cluster running with the Knative Serving component installed. For more information, see the Knative installation instructions.
- An installed version of the following tools:
- A Docker Hub account to which you are able to upload your sample's container image.
Getting the code
You can either clone a working copy of the sample code from the repository, or following the steps in the Recreating the sample code to walk through the steps of updating all the files.
Cloning the sample code
Use this method to clone and then immediate run the sample. To clone the sample code, run the following commands:
git clone https://github.com/knative/docs.git knative/docs
cd knative/docs/code-samples/community/serving/helloworld-java-quarkus
You are now ready to run the sample locally.
Recreating the sample code
Use the following steps to obtain an incomplete copy of the sample code for which you update and create the necessary build and configuration files:
-
From the console, create a new empty web project using the Maven archetype commands:
mvn io.quarkus:quarkus-maven-plugin:0.13.3:create \ -DprojectGroupId=com.redhat.developer.demos \ -DprojectArtifactId=helloworld-java-quarkus \ -DclassName="com.redhat.developer.demos.GreetingResource" \ -Dpath="/"
-
Update the
GreetingResource
class insrc/main/java/com/redhat/developer/demos/GreetingResource.java
to handle the "/" mapping and also add a@ConfigProperty
field to provide the TARGET environment variable:package com.redhat.developer.demos; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/") public class GreeterResource { @ConfigProperty(name = "TARGET", defaultValue="World") String target; @GET @Produces(MediaType.TEXT_PLAIN) public String greet() { return "Hello " + target + "!"; } }
-
Update
src/main/resources/application.properties
to configuration the application to default to port 8080, but allow the port to be overridden by thePORT
environmental variable:# Configuration file # key = value quarkus.http.port=${PORT:8080}
-
Update
src/test/java/com/redhat/developer/demos/GreetingResourceTest.java
test to reflect the change:package com.redhat.developer.demos; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.is; @QuarkusTest public class GreetingResourceTest { @Test public void testHelloEndpoint() { given() .when().get("/") .then() .statusCode(200) .body(is("Hello World!")); } }
-
Remove
src/main/resources/META-INF/resources/index.html
file since it's unncessary for this example.rm src/main/resources/META-INF/resources/index.html
-
Remove
.dockerignore
file since it's unncessary for this example.rm .dockerignore
-
In your project directory, create a file named
Dockerfile
and copy the code block below into it.FROM quay.io/rhdevelopers/quarkus-java-builder:graal-1.0.0-rc15 as builder COPY . /project WORKDIR /project # uncomment this to set the MAVEN_MIRROR_URL of your choice, to make faster builds # ARG MAVEN_MIRROR_URL=<your-maven-mirror-url> # e.g. #ARG MAVEN_MIRROR_URL=http://192.168.64.1:8081/nexus/content/groups/public RUN /usr/local/bin/entrypoint-run.sh mvn -DskipTests clean package FROM fabric8/java-jboss-openjdk8-jdk:1.5.4 USER jboss ENV JAVA_APP_DIR=/deployments COPY --from=builder /project/target/lib/* /deployments/lib/ COPY --from=builder /project/target/*-runner.jar /deployments/app.jar ENTRYPOINT [ "/deployments/run-java.sh" ]
If you want to build Quarkus native image, then copy the following code block in to file called
Dockerfile.native
FROM quay.io/rhdevelopers/quarkus-java-builder:graal-1.0.0-rc15 as builder COPY . /project # uncomment this to set the MAVEN_MIRROR_URL of your choice, to make faster builds # ARG MAVEN_MIRROR_URL=<your-maven-mirror-url> # e.g. # ARG MAVEN_MIRROR_URL=http://192.168.64.1:8081/nexus/content/groups/public RUN /usr/local/bin/entrypoint-run.sh mvn -DskipTests clean package -Pnative FROM registry.fedoraproject.org/fedora-minimal COPY --from=builder /project/target/helloworld-java-quarkus-runner /app ENTRYPOINT [ "/app" ]
-
Create a new file,
service.yaml
and copy the following service definition into the file. Make sure to replace{username}
with your Docker Hub username.apiVersion: serving.knative.dev/v1 kind: Service metadata: name: helloworld-java-quarkus spec: template: spec: containers: - image: docker.io/{username}/helloworld-java-quarkus env: - name: TARGET value: "Quarkus Sample v1"
Locally testing your sample
-
Run the application locally:
mvn wrapper:wrapper ./mvnw compile quarkus:dev
Go to
http://localhost:8080/
to see yourHello World!
message.
Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample folder) you're ready to build and deploy the sample app.
-
Use Docker to build the sample code into a container. To build and push with Docker Hub, run these commands replacing
{username}
with your Docker Hub username:# Build and push the container on your local machine. docker buildx build --platform linux/arm64,linux/amd64 -t "{username}/helloworld-java-quarkus" --push . # (OR) # Build and push the container on your local machine. - Quarkus native mode docker buildx build --platform linux/arm64,linux/amd64 -t "{username}/helloworld-java-quarkus" --push . -f Dockerfile.native
-
After the build has completed and the container is pushed to docker hub, you can deploy the app into your cluster. Ensure that the container image value in
service.yaml
matches the container you built in the previous step. Apply the configuration usingkubectl
:kubectl apply --filename service.yaml
-
Now that your service is created, Knative will perform the following steps:
- Create a new immutable revision for this version of the app.
- Network programming to create a route, ingress, service, and load balancer for your app.
- Automatically scale your pods up and down (including to zero active pods).
-
To find the URL for your service, use
kubectl get ksvc helloworld-java-quarkus NAME URL helloworld-java-quarkus http://helloworld-java-quarkus.default.1.2.3.4.sslip.io
-
Now you can make a request to your app and see the result. Replace the URL below with the URL returned in the previous command.
curl http://helloworld-java-quarkus.default.1.2.3.4.sslip.io Namaste Knative World!
Removing the sample app deployment
To remove the sample app from your cluster, delete the service record:
kubectl delete --filename service.yaml