Deploy a Micronaut app to Microsoft Azure

Learn how to deploy a Micronaut application to Microsoft Azure.

Authors: Bruno Borges

Micronaut Version: 1.1.0

1 Micronaut Training

Micronaut Training - Developed and delivered by the folks who created and actively maintain the Micronaut framework!.

2 Getting Started

In this guide, you are going to deploy a Micronaut application to Microsoft Azure App Service for Web Apps.

3 Costs

This guide uses services available in the Always Free Tier of Microsoft Azure Free Account. If you don’t have one already, create your account to complete this guide.

4 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • Azure CLI installed and authenticated

  • JDK 8 installed with JAVA_HOME configured appropriately (cannot be Java 9 or later)

5 How to Complete

To get started do the following:

Download and unzip the source or clone the following GitHub repository:

git clone https://github.com/micronaut-guides/micronaut-azure-cloud.git

This Micronaut guide contains two folders:

  • initial Initial project. A simple Micronaut app as you may have created with Micronaut CLI.``

  • complete A completed example. It is the result of working through the steps presented by the guide and applying those changes to the initial folder.

To complete the guide, go to the initial folder

cd initial

and follow the instructions in the next sections.

or you can go right to the completed example:

cd complete

Although you can go right to the completed example, in order to deploy the app you would need to complete several configuration steps in Microsoft Azure:

After you’ve checked out the links above, follow below for the next steps on setting up the Maven plugin for Azure deployment.

6 Maven Plugin for Azure App Service

First, make sure you have the Azure CLI installed and authenticated:

$ az login

Then, open the pom.xml file and add the following <plugin> inside the <plugins> section of the Maven <build> definition of the POM:

pom.xml
      <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.6.0</version>
      </plugin>

Save the file.

Now you can run the following goal to help you configure the required settings:

mvn azure-webapp:config

Make sure you select the following options:

  • OS: linux

  • javaVersion: jre8

  • runtimeStack: jre8

This is how it looks like:

[INFO] --- azure-webapp-maven-plugin:1.6.0:config (default-cli) @ example-micronaut ---
[WARNING] The plugin may not work if you change the os of an existing webapp.
Define value for OS(Default: Linux):
1. linux [*]
2. windows
3. docker
Enter index to use: 1
Define value for javaVersion(Default: jre8):
1. jre8 [*]
2. java11
Enter index to use: 1
Define value for runtimeStack(Default: TOMCAT 8.5):
1. TOMCAT 9.0
2. jre8
3. TOMCAT 8.5 [*]
4. WILDFLY 14
Enter index to use: 2
Please confirm webapp properties
AppName : example-micronaut-1560245627178
ResourceGroup : example-micronaut-1560245627178-rg
Region : westeurope
PricingTier : Premium_P1V2
OS : Linux
RuntimeStack : JAVA 8-jre8
Deploy to slot : false
Confirm (Y/N)? :
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Check your pom.xml to see the new configuration done for you.

7 Sample Controller

Create a controller which we can invoke once the app is deployed.

src/main/java/example/micronaut/HelloController.java
package example.micronaut;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/")
public class HelloController {

    @Get(value = "/", produces = MediaType.TEXT_PLAIN)
    public String index() {
        return "Micronaut on Azure";
    }
}

8 Deploying the app

To deploy the app to Azure run:

mvn package azure-webapp:deploy

With a BUILD SUCCESS, you should see a message containing the URL of your newly deployed web app:

...
[INFO] Authenticate with Azure CLI 2.0
[INFO] Target Web App doesn't exist. Creating a new one...
[INFO] Creating App Service Plan 'ServicePlan2ef7a87d-fb15-4d3e'...
[INFO] Successfully created App Service Plan.
[INFO] Successfully created Web App.
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources to /azure/projects/micronaut-azure-cloud/initial/target/azure-webapp/example-micronaut-1560242864679
[INFO] Trying to deploy artifact to example-micronaut-1560242864679...
[INFO] Renaming /azure/projects/micronaut-azure-cloud/initial/target/azure-webapp/example-micronaut-1560242864679/example-micronaut-0.1.jar to app.jar
[INFO] Deploying the zip package example-micronaut-1560242864679.zip...
[INFO] Exception occurred during deployment: java.net.SocketTimeoutException: timeout, retry immediately(1/3)...
[INFO] Successfully deployed the artifact to https://example-micronaut-1560242864679.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:59 min
[INFO] Finished at: 2019-06-11T02:02:15-07:00
[INFO] ------------------------------------------------------------------------

9 Logging

To see the log of your application running on Azure, you have to first enable storage of your app logging, and then perform a tail for live streaming.

To enable logging storage, use the following Azure CLI command. Check your pom.xml for the generated <resourceGroup> and the <appName>

az webapp log config --name [appName] -g [resourceGroup] --web-server-logging filesystem

Once this is done, you can then tail the log live from the cloud:

az webapp log tail --name [appName] -g [resourceGroup]

If you don’t see any log, try changing the level of the project’s Logback configuration in logback.xml to a higher value such as debug, then run the Maven deployment command again:

mvn package azure-webapp:deploy

And then run the tail command above once again.

10 Cleaning Up

After you’ve finished this guide, you can clean up the resources you created on Azure so you won’t take the risk of being billed because of them in the future. The following sections describe how to delete or turn off these resources.

Delete the resource group and all of its resources created along this guide with the following command:

az group delete -g [resourceGroup]

Answer y to confirm.

You can add the --no-wait parameter to not wait for the command to finish.

11 Learn More

If you want to learn more about Microsoft Azure for Java development, visit https://docs.microsoft.com/java/azure.