Blog Posts BPMN DMN

Creating your first cloud-agnostic serverless application with Java

Blog: Drools & jBPM Blog

If you are new to Serverless Workflow or serverless in general, 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

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:

<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.

The post Creating your first cloud-agnostic serverless application with Java appeared first on KIE Community.

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="https://www.businessprocessincubator.com/content/creating-your-first-cloud-agnostic-serverless-application-with-java/?feed=html" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples

BPMN.org

XPDL.org

×