DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building a Simple RAG Application With Java and Quarkus
  • Unlocking the Power of Oracle NoSQL With Quarkus: Seamless Integration for Cloud-Age Applications
  • Keep Your Application Secrets Secret
  • Simplify Java Persistence Using Quarkus and Hibernate Reactive

Trending

  • Using Java Stream Gatherers To Improve Stateful Operations
  • Advancing Your Software Engineering Career in 2025
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Efficient API Communication With Spring WebClient
  1. DZone
  2. Coding
  3. Java
  4. Creating Your First Cloud-Agnostic Serverless Application with Java

Creating Your First Cloud-Agnostic Serverless Application with Java

Run through the steps to create your first serverless Java application that runs on any cloud.

By 
Helber Belmiro user avatar
Helber Belmiro
DZone Core CORE ·
Sep. 27, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.7K Views

Join the DZone community and get the full member experience.

Join For Free

If you are new to serverless, creating a simple application for a serverless infrastructure is a good place to start.

In this article, you will run through the steps to create your first serverless Java application that runs on any cloud.

What Is Serverless?

Contrary to what the name says, there are still servers in serverless, but you don’t need to worry about managing them. You just need to deploy your containers and the serverless infrastructure is responsible for providing resources to your application scale up or down.

The best part is that it automatically scales up when there is a high demand or scales to zero when there is no demand. This will reduce the amount of money you spend with the cloud.

What Will You Create?

You will use Quarkus to create a simple Java application that returns a greeting message to an HTTP request and deploy it to Knative.

Why Knative?

In the beginning, serverless applications used to consist of small pieces of code that were run by a cloud vendor, like AWS Lambda. In this first phase, the applications had some limitations and were closely coupled to the vendor libraries.

Knative enables developers to run serverless applications on a Kubernetes cluster. This gives you the flexibility to run your applications on any cloud, on-premises, or even mix all of them.

Why Quarkus?

Because serverless applications need to start fast.

Since the biggest advantage of serverless is scale up and down (even zero) according to demand, serverless applications need to start fast when scaling up, otherwise, requests would be denied.

One of the greatest characteristics of Quarkus applications is their super-fast start-up.

Also, Quarkus is Kubernetes Native, which means that it’s easy to deploy Quarkus applications to Kubernetes without having to understand the intricacies of the underlying Kubernetes framework.

Requirements

  • A local Knative installation. See Install Knative using quickstart
    • This article uses minikube as the local Kubernetes cluster
  • kn CLI installed. See Knative CLI tools
  • JDK 11+ installed with JAVA_HOME configured appropriately
  • Apache Maven 3.8.1+
  • GraalVM (optional to deploy a native image)

Create a Quarkus Application

NOTE: If you don’t want to create the application, you can just clone it from GitHub and skip to Deploy your application to Knative.

 
mvn io.quarkus.platform:quarkus-maven-plugin:2.11.2.Final:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=knative-serving-quarkus-demo
cd knative-serving-quarkus-demo


Run Your Application Locally

To verify that you created the project correctly, run the project locally by running the following command:

mvn quarkus:dev


After downloading the dependencies and building the project, you should see an output similar to:

__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2022-08-15 16:50:25,135 INFO  [io.quarkus] (Quarkus Main Thread) knative-serving-quarkus-demo 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.11.2.Final) started in 1.339s. Listening on: http://localhost:8080
2022-08-15 16:50:25,150 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-08-15 16:50:25,150 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx]


On a different terminal window or in the browser, you can access the application by sending a request to the http://localhost:8080/hello endpoint:

curl -X 'GET' 'http://localhost:8080/hello' -H 'accept: text/plain'


If you see the following output, then you have successfully created your application:

Hello from RESTEasy Reactive


Hit Ctrl + C to stop the application.

Prepare Your Application for Deployment to Knative

Add the Required Dependencies

Add the following dependencies to the pom.xml file:

XML
 
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-kubernetes</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-container-image-jib</artifactId>
</dependency>


Configure the Application for Deployment to Knative

Add the following configuration to the src/main/resources/application.properties file:

quarkus.kubernetes.deployment-target=knative
quarkus.container-image.group=dev.local/hbelmiro


NOTE: In the quarkus.container-image.group property, replace hbelmiro with your container registry username.

Deploy Your Application to Knative

Start the Minikube Tunnel

NOTE: This step is only necessary if you are using minikube as the local Kubernetes cluster.

On a different terminal window, run the following command to start the minikube tunnel:

minikube tunnel --profile knative


You should see an output similar to the following:

Status:    
    machine: knative
    pid: 223762
    route: 10.96.0.0/12 -> 192.168.49.2
    minikube: Running
    services: [kourier]
    errors: 
        minikube: no errors
        router: no errors
        loadbalancer emulator: no errors


Leave the terminal window open and running the above command.

Configure the Container CLI to Use the Container Engine Inside Minikube

eval $(minikube -p knative docker-env)


Deploy the Application

Run the following command to deploy the application to Knative:

mvn clean package -Dquarkus.kubernetes.deploy=true


You should see an output similar to the following:

[INFO] [io.quarkus.kubernetes.deployment.KubernetesDeployer] Deploying to knative server: https://192.168.49.2:8443/ in namespace: default.
[INFO] [io.quarkus.kubernetes.deployment.KubernetesDeployer] Applied: Service knative-serving-quarkus-demo.
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 8952ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


Check the Knative Deployed Services

Run the following command to check the Knative deployed services:

kn service list


You should see your application listed on the deployed services like the following:

NAME                           URL                                                                   LATEST                               AGE   CONDITIONS   READY   REASON
knative-serving-quarkus-demo   http://knative-serving-quarkus-demo.default.10.106.207.219.sslip.io   knative-serving-quarkus-demo-00001   23s   3 OK / 3     True


IMPORTANT: In the above output, check the READY status of the application. If the status is not True, then you need to wait for the application to be ready, or there is a problem with the deployment.

Send a Request to the Deployed Application

Use the URL returned by the above command to send a request to the deployed application.

curl -X 'GET' 'http://knative-serving-quarkus-demo.default.10.106.207.219.sslip.io/hello' -H 'accept: text/plain'


You should see the following output:

Hello from RESTEasy Reactive


Going Native

You can create a native image of your application to make it start even faster. To do that, deploy your application by using the following command:

mvn clean package -Pnative -Dquarkus.native.native-image-xmx=4096m -Dquarkus.native.remote-container-build=true -Dquarkus.kubernetes.deploy=true


IMPORTANT: -Dquarkus.native.native-image-xmx=4096m is the amount of memory Quarkus can use to generate the native image. You should adjust it or completely remove it depending on your local machine’s specifications.

Now You Are Ready to Run Serverless Applications Using Java

Easy, isn’t it? Quarkus and Knative give you the freedom to run serverless applications using Java on-premises or in the cloud, no matter the vendor. You can even mix more than one cloud vendor with your on-premises infrastructure. This flexibility brings you agility and reduces your costs with infrastructure.

Quarkus application Cloud Java (programming language)

Published at DZone with permission of Helber Belmiro. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Simple RAG Application With Java and Quarkus
  • Unlocking the Power of Oracle NoSQL With Quarkus: Seamless Integration for Cloud-Age Applications
  • Keep Your Application Secrets Secret
  • Simplify Java Persistence Using Quarkus and Hibernate Reactive

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!