Create Inventory Service with Quarkus

45 MINUTE EXERCISE

In this lab you will learn about building microservices using Quarkus.

CoolStore Architecture

What is Quarkus?

Quarkus

Quarkus is a Kubernetes Native Java stack tailored for GraalVM & OpenJDK HotSpot, crafted from the best of breed Java libraries and standards.

Container First: Quarkus tailors your application for GraalVM and HotSpot. Amazingly fast boot time, incredibly low RSS memory (not just heap size!) offering near instant scale up and high density memory utilization in container orchestration platforms like Kubernetes. We use a technique we call compile time boot.

Unifies Imperative and Reactive: Combine both the familiar imperative code and the non-blocking reactive style when developing applications

Developer Joy: A cohesive platform for optimized developer joy:

  • Unified configuration

  • Zero config, live reload in the blink of an eye

  • Streamlined code for the 80% common usages, flexible for the 20%

  • No hassle native executable generation

Best of Breed Libraries and Standards: Quarkus brings a cohesive, fun to use full-stack framework by leveraging best of breed libraries you love and use wired on a standard backbone.

Quarkus Maven Project

The inventory-quarkus project has the following structure which shows the components of the Quarkus project laid out in different subdirectories according to Maven best practices:

Inventory Project

The '/projects/workshop/labs/inventory-quarkus' folder contents:

  • the Maven structure

  • a com.redhat.cloudnative.InventoryResource resource exposed on /hello

  • an associated unit test

  • a landing page that is accessible on http://localhost:8080 after starting the application

  • example Dockerfile files for both native and jvm modes in src/main/docker

  • the application configuration file

Look at the pom.xml. You will find the import of the Quarkus BOM, allowing you to omit the version on the different Quarkus dependencies. In addition, you can see the quarkus-maven-plugin responsible of the packaging of the application and also providing the development mode.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-bom</artifactId>
            <version>${quarkus.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-maven-plugin</artifactId>
            <version>${quarkus.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

If we focus on the dependencies section, you can see the following extensions:

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-hibernate-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-jdbc-h2</artifactId>
    </dependency>
Table 1. Quarkus Extensions
Name Description

JSON REST Services

It allows you to develop REST services to consume and produce JSON payloads

Hibernate ORM

The de facto JPA implementation and offers you the full breath of an Object Relational Mapper.

Datasources (H2)

Using datasources is the main way of obtaining connections to a database.

Examine 'src/main/java/com/redhat/cloudnative/InventoryResource.java' file:

package com.redhat.cloudnative;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class InventoryResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}

It’s a very simple REST endpoint, returning "hello" to requests on "/hello".

With Quarkus, there is no need to create an Application class. It’s supported, but not required. In addition, only one instance of the resource is created and not one per request. You can configure this using the different Scoped annotations (ApplicationScoped, RequestScoped, etc).

Enable the Development Mode

quarkus:dev runs Quarkus in development mode. This enables hot deployment with background compilation, which means that when you modify your Java files and/or your resource files and refresh your browser, these changes will automatically take effect. This works too for resource files like the configuration property file. Refreshing the browser triggers a scan of the workspace, and if any changes are detected, the Java files are recompiled and the application is redeployed; your request is then serviced by the redeployed application. If there are any issues with compilation or deployment an error page will let you know.

First, in your Workspace,

  • IDE Task

  • CLI

Click on 'Terminal' → 'Run Task…​' → 'Inventory - Compile (Dev Mode)'

Che - RunTask

Execute the following commands in the '>_ workshop_tools' terminal window

cd /projects/workshop/labs/inventory-quarkus
mvn compile quarkus:dev -Ddebug=false
To open a '>_ workshop_tools' terminal window, click on 'Terminal' → 'Open Terminal in specific container' → 'workshop-tools'

When pop-ups appear, confirm you want to expose the 8080 port.

Che - Expose Port

And finally click on 'Open Link'.

Che - Open Link

Your browser will be redirect on your Inventory Service running inside your Workspace.

Che - Quarkus Preview

If you have the following result on the 'Preview' window, please click on the refresh icon of this same window,

Che - Preview Not Available

Modify the 'src/main/resources/META-INF/resources/index.html' file as follows

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Inventory Service</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
            integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css"
            href="https://cdnjs.cloudflare.com/ajax/libs/patternfly/3.24.0/css/patternfly.min.css">
        <link rel="stylesheet" type="text/css"
            href="https://cdnjs.cloudflare.com/ajax/libs/patternfly/3.24.0/css/patternfly-additions.min.css">
    </head>
    <body>
        <div class="jumbotron">
            <div class="container">
                <h1 class="display-3"><img src="https://camo.githubusercontent.com/be1e4ea465298c7e05b1378ff38d463cfef120a3/68747470733a2f2f64657369676e2e6a626f73732e6f72672f717561726b75732f6c6f676f2f66696e616c2f504e472f717561726b75735f6c6f676f5f686f72697a6f6e74616c5f7267625f3132383070785f64656661756c742e706e67" alt="Quarkus" width="400"> Inventory Service</h1>
                <p>This is a Quarkus Microservice for the CoolStore Demo. (<a href="/api/inventory/329299">Test it</a>)
                </p>
            </div>
        </div>
        <div class="container">
            <footer>
                <p>&copy; Red Hat 2020</p>
            </footer>
        </div>
    </body>
</html>

Refresh your browser and you should have the following content without rebuilding your JAR file

Inventory Quarkus

Now let’s write some code and create a domain model and a RESTful endpoint to create the Inventory service

Create a Domain Model

Create the 'src/main/java/com/redhat/cloudnative/Inventory.java' file as follows:

package com.redhat.cloudnative;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import java.io.Serializable;

@Entity (1)
@Table(name = "INVENTORY") (2)
public class Inventory implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id (3)
    private String itemId;

    @Column
    private int quantity;

    public Inventory() {
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Inventory [itemId='" + itemId + '\'' + ", quantity=" + quantity + ']';
    }
}
1 @Entity marks the class as a JPA entity
2 @Table customizes the table creation process by defining a table name and database constraint
3 @Id marks the primary key for the table

You don’t need to press a save button! Che automatically saves the changes made to the files.

Update the 'src/main/resources/application.properties' file to match with the following content:

quarkus.datasource.url=jdbc:h2:mem:inventory;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
quarkus.datasource.driver=org.h2.Driver
quarkus.datasource.username=sa
quarkus.datasource.password=sa
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.sql-load-script=import.sql

%prod.quarkus.package.uber-jar=true (1)
1 An uber-jar contains all the dependencies required packaged in the jar to enable running the application with java -jar. By default, in Quarkus, the generation of the uber-jar is disabled. With the %prod prefix, this option is only activated when building the jar intended for deployments.

Update the 'src/main/resources/import.sql' file as follows:

INSERT INTO INVENTORY(itemId, quantity) VALUES (100000, 0);
INSERT INTO INVENTORY(itemId, quantity) VALUES (329299, 35);
INSERT INTO INVENTORY(itemId, quantity) VALUES (329199, 12);
INSERT INTO INVENTORY(itemId, quantity) VALUES (165613, 45);
INSERT INTO INVENTORY(itemId, quantity) VALUES (165614, 87);
INSERT INTO INVENTORY(itemId, quantity) VALUES (165954, 43);
INSERT INTO INVENTORY(itemId, quantity) VALUES (444434, 32);
INSERT INTO INVENTORY(itemId, quantity) VALUES (444435, 53);

Create a RESTful Service

Quarkus uses JAX-RS standard for building REST services.

Modify the 'src/main/java/com/redhat/cloudnative/InventoryResource.java' file to match with:

package com.redhat.cloudnative;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/api/inventory")
@ApplicationScoped
public class InventoryResource {

    @Inject
    EntityManager em;

    @GET
    @Path("/{itemId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Inventory getAvailability(@PathParam("itemId") String itemId) {
        Inventory inventory = em.find(Inventory.class, itemId);
        return inventory;
    }
}

The above REST service defines an endpoint that is accessible via HTTP GET at for example /api/inventory/329299 with the last path param being the product id which we want to check its inventory status.

Refresh your browser and click on 'Test it'. You should have the following output:

{"itemId":"329299","quantity":35}

The REST API returned a JSON object representing the inventory count for this product. Congratulations!

Stop the Development Mode

In your Workspace, stop the service as follows:

  • IDE Task

  • CLI

Enter Ctrl+c in the existing '>_ Inventory Compile (Dev Mode)' terminal window

Enter Ctrl+c in the existing '>_ workshop_tools' terminal window

Deploy on OpenShift

It’s time to deploy your service on OpenShift using odo.

In your Workspace,

  • IDE Task

  • CLI

Click on 'Terminal' → 'Run Task…​' → 'Inventory - Build'

Che - RunTask

Execute the following commands in the '>_ workshop_tools' terminal window

cd /projects/workshop/labs/inventory-quarkus
mvn clean package -DskipTests
To open a '>_ workshop_tools' terminal window, click on 'Terminal' → 'Open Terminal in specific container' → 'workshop-tools'

Once this completes, prepare your application code/binary for OpenShift.

  • IDE Task

  • CLI

Click on 'Terminal' → 'Run Task…​' → 'Inventory - Create Component'

Che - RunTask

Execute the following commands in the '>_ workshop_tools' terminal window

cd /projects/workshop/labs/inventory-quarkus
odo component create \
    java:11 \
    inventory \
    --app coolstore \
    --binary target/inventory-quarkus-1.0.0-SNAPSHOT-runner.jar \
    --s2i \
    --project my-project%USER_ID%
To open a '>_ workshop_tools' terminal window, click on 'Terminal' → 'Open Terminal in specific container' → 'workshop-tools'

The output should be as follows:

Validation
 ✓  Validating component [21ms]

Please use `odo push` command to create the component with source deployed

Now the configuration file 'config.yaml' is in the local directory of the inventory component ('/projects/workshop/labs/inventory-quarkus/.odo') that contains information about the component for deployment.

Then, expose the service to Internet by creating an OpenShift Route for the component.

  • IDE Task

  • CLI

Click on 'Terminal' → 'Run Task…​' → 'Inventory - Expose'

Che - RunTask

Execute the following commands in the '>_ workshop_tools' terminal window

cd /projects/workshop/labs/inventory-quarkus
odo url create \
    inventory \
    --port 8080
To open a '>_ workshop_tools' terminal window, click on 'Terminal' → 'Open Terminal in specific container' → 'workshop-tools'

Finally, push the component to the OpenShift cluster:

  • IDE Task

  • CLI

Click on 'Terminal' → 'Run Task…​' → 'Inventory - Push'

Che - RunTask

Execute the following commands in the '>_ workshop_tools' terminal window

cd /projects/workshop/labs/inventory-quarkus
odo push
To open a '>_ workshop_tools' terminal window, click on 'Terminal' → 'Open Terminal in specific container' → 'workshop-tools'

You should get as output:

Validation
 ✓  Checking component [72ms]

Configuration changes
 ✓  Retrieving component data [36ms]
 ✓  Applying configuration [95ms]

Applying URL changes
 ✓  URL inventory: http://inventory-coolstore-my-project%USER_ID%.%APPS_HOSTNAME_SUFFIX%/ created

Pushing to component inventory of type binary
 ✓  Checking file changes for pushing [1ms]
 ✓  Waiting for component to start [6ms]
 ✓  Syncing files to the component [248ms]
 ✓  Building component [3s]

Once this completes, your application should be up and running. OpenShift runs the different components of the application in one or more pods which are the unit of runtime deployment and consists of the running containers for the project.

Test your Service

In the OpenShift Web Console, from the Developer view, click on the 'Open URL' icon of the Inventory Service

OpenShift - Inventory Topology

Your browser will be redirect on your Inventory Service running on OpenShift.

Inventory Quarkus

Then click on 'Test it'. You should have the following output:

{"itemId":"329299","quantity":35}

Well done! You are ready to move on to the next lab.