Creating a Service with Micronaut

Micronaut is a framework on JVM to develop microservices and serverless (FaaS) based applications. Micronaut support Java, Groovy and Kotlin languages.

Let's quickly get our hands wet with creating a service using Micronaut.

Installing Micronaut CLI

If you are are on a *nix platform and not using SDKMAN, I highly recommend getting it installed. Use the following command to install SDKMAN.

$ curl -s "https://get.sdkman.io" | bash

Now install the Micronaut CLI

sdk install micronaut

Get into the Micronaut interactive shell using the mn command.

| Starting interactive mode...
| Enter a command name to run. Use TAB for completion:
mn>

Scaffolding a project

Get into the Micronaut interactive shell and create the service using create-app command followed by the service name, say "hello-service".

mn> create-app hello-service
| Generating Java project...
| Application created at /Users/naresha/practice/micronaut/ga/hello-service
| Initializing application. Please wait...

If everything goes fine, you should find the directory "hello-service" created with the following files. By default Micronaut creates a java project with gradle build. Gradle wrapper is also created for you - so you don't need have gradle pre-installed to build and run the project.

➜  hello-service tree
.
├── Dockerfile
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── micronaut-cli.yml
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       └── service
    │   │           └── Application.java
    │   └── resources
    │       ├── application.yml
    │       └── logback.xml
    └── test
        └── java
            └── hello
                └── service

12 directories, 10 files

Creating a controller

Micronaut default applications are HTTP based. We create controllers to handle HTTP methods. The CLI comes with a command to create controllers. Let's go ahead and create a controller.

mn> create-controller Hello
| Rendered template Controller.java to destination src/main/java/hello/service/HelloController.java
| Rendered template ControllerTest.java to destination src/test/java/hello/service/HelloControllerTest.java

Note that the CLI took care of suffixing 'Hello' with 'Controller' to create 'HelloController'. It also created a test class where you can write the tests. Let's see what is available in the generated controller.

package hello.service;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.HttpStatus;

@Controller("/hello")
public class HelloController {

    @Get("/")
    public HttpStatus index() {
        return HttpStatus.OK;
    }
}

Running your service

Without further delay, let's run our service. I will invoke the gradle wrapper script and invoke the 'run' task.

hello-service ./gradlew run
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :compileJava
Note: Creating bean classes for 1 type elements

> Task :run
10:00:06.647 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 1103ms. Server Running: http://localhost:8080

The service is now running on port 8080. Let's call the service using the endpoint from the controller, which is mentioned in our controller as '/hello'.

➜  ~ curl -i http://localhost:8080/hello
HTTP/1.1 200 OK
Date: Tue, 9 Apr 2019 10:02:44 GMT
connection: keep-alive
transfer-encoding: chunked

Also, note the startup time. The server started in 1103ms, which included downloading several dependencies.

I will be exploring more of Micronaut in my upcoming posts.

Show Comments