Micronaut Azure

Micronaut projects for Microsoft Azure

Version:

1 Introduction

This project provides integrations between Micronaut and Microsoft Azure.

2 Azure Function Support

The Azure function module provides support for writing Serverless functions with Micronaut that target the Azure Function environment.

2.1 Simple Azure Functions

There are two modules, the first of which (micronaut-azure-function) is more low level and allows you to define functions that can be dependency injected with Micronaut.

To get started follow the instructions to create an Azure Function project with Gradle or with Maven.

Then add the following dependency to the project:

implementation("io.microaut.azure:micronaut-azure-function:1.0.1")
<dependency>
    <groupId>io.microaut.azure</groupId>
    <artifactId>micronaut-azure-function</artifactId>
    <version>1.0.1</version>
</dependency>

And ensure the Micronaut annotation processors are configured:

annotationProcessor("io.microaut.azure:micronaut-inject-java")
<annotationProcessorPaths>
    <path>
        <groupId>io.microaut.azure</groupId>
        <artifactId>micronaut-inject-java</artifactId>
    </path>
</annotationProcessorPaths>

You can then write a function that subclasses AzureFunction and it will be dependency injected when executed. For example:

package example;

import com.microsoft.azure.functions.annotation.BlobOutput;
import com.microsoft.azure.functions.annotation.BlobTrigger;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.StorageAccount;
import io.micronaut.azure.function.AzureFunction;
import io.micronaut.context.event.ApplicationEvent;
import io.micronaut.context.event.ApplicationEventPublisher;

import javax.inject.Inject;

public class BlobFunction extends AzureFunction { (1)
    @Inject
    ApplicationEventPublisher eventPublisher; (2)

    @FunctionName("copy")
    @StorageAccount("AzureWebJobsStorage")
    @BlobOutput(name = "$return", path = "samples-output-java/{name}") (3)
    public String copy(@BlobTrigger(
            name = "blob",
            path = "samples-input-java/{name}") String content) {
        eventPublisher.publishEvent(new BlobEvent(content)); (4)
        return content;
    }

    public static class BlobEvent extends ApplicationEvent {
        public BlobEvent(String content) {
            super(content);
        }
    }
}
package example

import com.microsoft.azure.functions.annotation.BlobOutput
import com.microsoft.azure.functions.annotation.BlobTrigger
import com.microsoft.azure.functions.annotation.FunctionName
import com.microsoft.azure.functions.annotation.StorageAccount
import io.micronaut.azure.function.AzureFunction
import io.micronaut.context.event.ApplicationEvent
import io.micronaut.context.event.ApplicationEventPublisher

import javax.inject.Inject

class BlobFunction extends AzureFunction { (1)
    @Inject
    ApplicationEventPublisher eventPublisher (2)

    @FunctionName("copy")
    @StorageAccount("AzureWebJobsStorage")
    @BlobOutput(name = '$return', path = "samples-output-java/{name}") (3)
    String copy(@BlobTrigger(
            name = "blob",
            path = "samples-input-java/{name}") String content) {
        eventPublisher.publishEvent(new BlobEvent(content)) (4)
        return content
    }

    static class BlobEvent extends ApplicationEvent {
        BlobEvent(String content) {
            super(content)
        }
    }
}
package example

import com.microsoft.azure.functions.annotation.BlobOutput
import com.microsoft.azure.functions.annotation.BlobTrigger
import com.microsoft.azure.functions.annotation.FunctionName
import com.microsoft.azure.functions.annotation.StorageAccount
import io.micronaut.azure.function.AzureFunction
import io.micronaut.context.event.ApplicationEvent
import io.micronaut.context.event.ApplicationEventPublisher
import javax.inject.Inject

class BlobFunction : AzureFunction() { (1)
    @Inject
    lateinit var eventPublisher : ApplicationEventPublisher (2)

    @FunctionName("copy")
    @StorageAccount("AzureWebJobsStorage")
    @BlobOutput(name = "\$return", path = "samples-output-java/{name}") (3)
    fun copy(@BlobTrigger(name = "blob", path = "samples-input-java/{name}") content: String): String {
        eventPublisher.publishEvent(BlobEvent(content)) (4)
        return content
    }

    class BlobEvent(content: String) : ApplicationEvent(content)
}
1 The class subclasses AzureFunction. Note that a zero argument public constructor is required
2 Use can dependency inject fields with @Inject. In this case we publish an event.
3 You can specify the function bindings as per the Azure Function API
4 Injected objects can be used in your function code

2.2 Azure HTTP Functions

An additional module exists called micronaut-azure-function-http that allows you to write regular Micronaut controllers and have them executed using Azure Function. To get started add the micronaut-azure-function-http module.

implementation("io.microaut.azure:micronaut-azure-function-http:1.0.1")
<dependency>
    <groupId>io.microaut.azure</groupId>
    <artifactId>micronaut-azure-function-http</artifactId>
    <version>1.0.1</version>
</dependency>

You then need to define a function that subclasses AzureHttpFunction and overrides the invoke method:

package example;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;

import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import io.micronaut.azure.function.http.AzureHttpFunction;
import java.util.Optional;

public class MyHttpFunction extends AzureHttpFunction { (1)
    @FunctionName("ExampleTrigger") (2)
    public HttpResponseMessage invoke(
            @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST}, (3)
                    route = "{*route}", (4)
                    authLevel = AuthorizationLevel.ANONYMOUS) (5)
                    HttpRequestMessage<Optional<String>> request, (6)
            final ExecutionContext context) {
        return super.route(request, context); (7)
    }
}
package example

import com.microsoft.azure.functions.ExecutionContext
import com.microsoft.azure.functions.HttpMethod
import com.microsoft.azure.functions.HttpRequestMessage
import com.microsoft.azure.functions.HttpResponseMessage

import com.microsoft.azure.functions.annotation.AuthorizationLevel
import com.microsoft.azure.functions.annotation.FunctionName
import com.microsoft.azure.functions.annotation.HttpTrigger
import io.micronaut.azure.function.http.AzureHttpFunction

class MyHttpFunction extends AzureHttpFunction { (1)
    @FunctionName("ExampleTrigger") (2)
    HttpResponseMessage invoke(
            @HttpTrigger(
                    name = "req",
                    methods = [HttpMethod.GET, HttpMethod.POST], (3)
                    route = "{*route}", (4)
                    authLevel = AuthorizationLevel.ANONYMOUS) (5)
                    HttpRequestMessage<Optional<String>> request, (6)
            final ExecutionContext context) {
        return super.route(request, context) (7)
    }
}
package example

import com.microsoft.azure.functions.ExecutionContext
import com.microsoft.azure.functions.HttpMethod
import com.microsoft.azure.functions.HttpRequestMessage
import com.microsoft.azure.functions.HttpResponseMessage
import com.microsoft.azure.functions.annotation.AuthorizationLevel
import com.microsoft.azure.functions.annotation.FunctionName
import com.microsoft.azure.functions.annotation.HttpTrigger
import io.micronaut.azure.function.http.AzureHttpFunction
import java.util.Optional

class MyHttpFunction : AzureHttpFunction() { (1)
    @FunctionName("ExampleTrigger") (2)
    fun invoke(
            @HttpTrigger(name = "req",
                    methods = [HttpMethod.GET, HttpMethod.POST], (3)
                    route = "{*route}", (4)
                    authLevel = AuthorizationLevel.ANONYMOUS) (5)
            request: HttpRequestMessage<Optional<String>>,  (6)
            context: ExecutionContext): HttpResponseMessage {
        return super.route(request, context) (7)
    }
}
1 The function class subclasses AzureHttpFunction and includes a zero argument constructor.
2 The function name can be whatever you prefer
3 You can choose to handle ony specific HTTP methods
4 In general you want a catch all route as in the example, but you can customize it.
5 The auth level specifies who can access the function. Using ANONYMOUS allows everyone.
6 The received request optionally contains the raw bytes
7 The body of the method should just invoke the route method of the super implementation.

With this in place you can write regular Micronaut controllers as documented in the user guide for the HTTP server and incoming function requests will be routed to the controllers and executed.

This approach allows you to develop a regular Micronaut application and deploy slices of the application as Serverless functions as desired.

3 Repository

You can find the source code of this project in this repository:

4 Release History

1.0.0

  • First Release