Deploy a Micronaut application to Oracle Cloud

Learn how to deploy a Micronaut application to Oracle Cloud.

Authors: Burt Beckwith

Micronaut Version: 4.6.3

1. Getting Started

In this guide, we will create a Micronaut application 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 application step by step. However, you can go right to the completed example.

4. Writing the Application

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

mn create-app example.micronaut.micronautguide --build=maven --lang=groovy
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory 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() {
        'the Micronaut framework 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:

./mvnw package

Push the JAR file to your VM:

scp -i /path/to/ssh-key-*.key target/micronautguide-0.1.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 graalvm22-ee-11.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

the Micronaut framework 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. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).