Deploy to Google Cloud Compute

Deploy a Micronaut application to Google Cloud Compute instance.

Authors: John Shingler

Micronaut Version: 4.3.7

1. Getting Started

In this guide, we will create a Micronaut application written in Groovy.

You will deploy a Micronaut application to Google Cloud Compute Engine, a computing and hosting service.

2. Costs

This guide uses paid services; you may need to enable Billing in Google Cloud to complete some steps in this guide.

3. What you will need

To complete this guide, you will need the following:

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

5. 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 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.1. Controller

In order to create a microservice that responds with "Hello World" you first need a controller.

Create a Controller:

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

import groovy.transform.CompileStatic
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.MediaType

@CompileStatic
@Controller("/hello") (1)
class HelloController {
    @Get (2)
    @Produces(MediaType.TEXT_PLAIN) (3)
    String index() {
        "Hello World" (4)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /hello.
2 The @Get annotation maps the index method to an HTTP GET request on /hello.
3 By default, a Micronaut response uses application/json as Content-Type. We are returning a String, not a JSON object, so we set it to text/plain.
4 A String "Hello World" is returned as the result

5.2. Test

Create a test to verify that when you make a GET request to /hello you get Hello World as a response:

src/test/groovy/example/micronaut/HelloControllerSpec.groovy
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Specification

import jakarta.inject.Inject

@MicronautTest (1)
class HelloControllerSpec extends Specification {

    @Inject
    @Client("/")  (2)
    HttpClient client

    void "test hello world response"() {
        when:
        HttpRequest request = HttpRequest.GET('/hello')  (3)
        String rsp = client.toBlocking().retrieve(request)

        then:
        rsp == "Hello World"
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject the HttpClient bean and point it to the embedded server.
3 Creating HTTP Requests is easy thanks to the Micronaut framework fluid API.

6. Google Cloud Platform

Signup for the Google Cloud Platform

6.1. Cloud SDK

Install the Cloud SDK CLI for your operating system.

Cloud SDK includes the gcloud command-line tool. Run the init command in your terminal:

gcloud init

Log in to your Google Cloud Platform:

gcloud auth login

6.2. Google Cloud Platform Project

Create a new project with a unique name (replace xxxxxx with alphanumeric characters of your choice):

gcloud projects create micronaut-guides-xxxxxx
In GCP, project ids are globally unique, so the id you used above is the one you should use in the rest of this guide.

Change your project:

gcloud config set project micronaut-guides-xxxxxx

If you forget the project id, you can list all projects:

gcloud projects list

7. Google Cloud Compute

7.1. Create the VM Instance

You can create the Google Cloud Compute VM via the CLI.

gcloud compute instances create \
  guide-micronaut-jdbc-compute \
  --machine-type=e2-micro \
  --zone=us-east1-b
If you’ve omitted the default zone in the setup steps, the command will respond requesting a zone for your instance.

You will see the following output:

Created [https://www.googleapis.com/compute/v1/projects/micronaut-guides-jdbc/zones/us-east1-b/instances/guide-micronaut-jdbc-compute].
NAME                          ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP    EXTERNAL_IP     STATUS
guide-micronaut-jdbc-compute  us-east1-b  e2-micro                   10.xxx.xxx.4   34.xxx.xxx.200  RUNNING

Make note of your external IP address; you will need this later.

7.2. Open Port 8080

gcloud compute firewall-rules create allow-8080 --allow tcp:8080

You will see the following output:

NAME        NETWORK  DIRECTION  PRIORITY  ALLOW     DENY  DISABLED
allow-8080  default  INGRESS    1000      tcp:8080        False

7.3. Generate JAR

mvn package -Dpackaging=jar

7.4. Copy the JAR to the Instance

gcloud compute scp target/micronautguide-0.1.jar \
    guide-micronaut-jdbc-compute:.
If you do not have an SSH key file for gcloud, you will be prompted to set a passphrase to create a public/private RSA key pair.

7.5. Start Micronaut Application

Before we can start the application, we need to access our newly created instance.

gcloud compute ssh guide-micronaut-jdbc-compute

Now we are connected to our instance.

Let’s make sure it is up-to-date and install the Java JRE.

sudo apt update
apt install default-jre

Now we can run our Micronaut application.

java -jar micronautguide-0.1.jar

7.6. Running Application

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

curl -i http://34.xxx.xxx.200:8080/hello
HTTP/1.1 200 OK
date: Tue, 8 Feb 2022 23:41:33 GMT
Content-Type: text/plain
content-length: 11
connection: keep-alive

Hello World

7.7. Stopping the Instance

gcloud compute instances stop guide-micronaut-jdbc-compute

8. Cleaning Up

After you’ve finished this guide, you can clean up the resources you created on Google Cloud Platform so you won’t be billed for them in the future. The following sections describe how to delete or turn off these resources.

8.1. Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

Deleting a project has the following consequences:

  • If you used an existing project, you’ll also delete any other work you’ve done in the project.

  • You can’t reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.

  • If you are exploring multiple tutorials and quickstarts, reusing projects instead of deleting them prevents you from exceeding project quota limits.

8.1.1. Via the CLI

To delete the project using the Cloud SDK, run the following command, replacing YOUR_PROJECT_ID with the project ID:

gcloud projects delete YOUR_PROJECT_ID

8.1.2. Via the Cloud Platform Console

In the Cloud Platform Console, go to the Projects page.

In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click Delete project

In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting or turning off specific resources

You can individually delete or turn off some of the resources that you created during the tutorial.

9. Next steps

10. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

11. 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…​).