Deploy a Micronaut app to Oracle Cloud

Learn how to deploy a Micronaut application to Oracle Cloud.

Authors: Burt Beckwith

Micronaut Version: 2.5.0

1. Getting Started

In this guide we are going to create a Micronaut app written in Groovy.

2. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

  • An Oracle Cloud account (create a free trial account at signup.oraclecloud.com)

3. Solution

We recommend that you follow the instructions in the next sections and create the app step by step. However, you can go right to the completed example.

4. Writing the App

Create an app using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=gradle --lang=groovy
If you don’t specify the --build argument, Gradle is used as a build tool.
If you don’t specify the --lang argument, Java is used as a language.

The previous command creates a Micronaut app with the default package example.micronaut in a folder named micronautguide.

5. Sample Controller

Next we’ll create a simple controller that we can use to access the application once it’s deployed.

src/main/groovy/example/micronaut/HelloController.groovy
package example.micronaut

import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get

@Controller
class HelloController {

    @Get(produces = MediaType.TEXT_PLAIN)
    String index() {
        'Micronaut on Oracle Cloud'
    }
}

6. Create an Oracle Cloud Compute Instance

Login to your Oracle Cloud tenancy, and from the Oracle Cloud Home Page select "Create a VM Instance":

createvm.1

Enter "micronaut-demo" as the name of the instance leaving the "Placement" and "Shape" as "Always Free Eligible" (or any small image shape):

createvm.2

Under "Add SSH keys" choose "Generate SSH key pair" and then click "Save Private Key" to save the private key locally on disk:

createvm.3

The private key will download as a file named "ssh-key-(date).key".

Leave the remaining settings as their defaults and click the "Create" button:

createvm.4

Take note of the IP address of your instance - click the "Copy" link next to "Public IP Address".

createvm.5

Click on the "Subnet" link ("Default Subnet …​"):

createvm.6

Under "Security Lists" click on the "Default Security List":

createvm.7

Click "Add Ingress Rules":

createvm.8

Enter "0.0.0.0/0" as the "Source CIDR", 8080 as the "Destination Port Range", and "micronaut" as the Description, and click "Add Ingress Rules".

createvm.9

7. Deploy to Oracle Cloud

First ensure that the private key you downloaded has the correct permissions:

$ chmod 400 /path/to/ssh-key-*.key

Create an executable jar including all dependencies:

$ ./gradlew assemble

Push the JAR file to your VM:

$ scp -i /path/to/ssh-key-*.key build/libs/micronautguide-0.1-all.jar opc@[VM IP Address]:/home/opc/application.jar

To run on the VM, first SSH in:

$ ssh -i /path/to/ssh-key-*.key opc@[YOUR IP]

Then install Java:

$ yum check-update
$ sudo yum install graalvm21-ee-11-native-image.x86_64

Open up the firewall to port 8080:

$ sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
$ sudo firewall-cmd --reload

Finally, start the application:

$ java -jar application.jar

Verify that the application is running by invoking the controller at http://[VM IP Address]:8080 in a browser or using cURL:

$ curl -i http://[VM IP Address]:8080
HTTP/1.1 200 OK
Date: Mon, 3 May 2021 02:40:37 GMT
content-type: text/plain
content-length: 25
connection: keep-alive

Micronaut on Oracle Cloud

8. Cleaning up

To shut down the VM instance, click "More Actions" on the "Instance Details" page, then click "Terminate"

createvm.10

Click "Terminate Instance" to confirm termination:

createvm.11

9. Next steps

Explore more features with Micronaut Guides.

10. Help with Micronaut

Object Computing, Inc. (OCI) sponsored the creation of this Guide. A variety of consulting and support services are available.