In conjunction with the minor release of Micronaut 1.0.1, Object Computing, Inc. (OCI) also released Micronaut for Spring 1.0 M1. As stated in the press release:
Micronaut for Spring adds the ability to use the long-established Spring annotation-based programming model to build Micronaut applications and libraries that work with both Micronaut and Spring.
Using Ahead-of-Time compilation, Micronaut for Spring allows for: integrating Spring components into a Micronaut application; running Spring applications as Micronaut applications; and exposing Micronaut beans to a Spring application.
Jonathan Giles, senior software engineer at Microsoft, recently discussed why developers should consider migrating to Micronaut:
Micronaut takes a different approach to enabling everything we developers take for granted in Spring Boot and MicroProfile. Rather than do runtime annotation processing as Spring Boot and MicroProfile, Micronaut uses annotation processors at compile time to generate additional classes that are compiled alongside your code. This means startup time is reduced due to the substantially lower amount of overhead that is required to scan the classpath of your project. In fact, Micronaut tries to avoid reflection as much as possible, only using it where absolutely necessary.
Formerly known as Project Particle, Micronaut is a full-stack JVM-based framework for creating microservice-based, cloud-native and serverless applications that can be written in Java, Groovy, and Kotlin. Graeme Rocher, principal software engineer and Grails and Micronaut product lead at OCI, first introduced Micronaut at the Greach conference in March 2018. The road to version 1.0 included three release candidates within a less-than-three-week timeframe just before the official release of version 1.0 in late October 2018.
Example Application
An example application, included in the Micronaut for Spring repository, demonstrates how to use Micronaut for Spring including tools to convert the application to a GraalVM native image.
The Application.java
file, shown below, takes a Spring Boot application at the source code level that may be executed as a Micronaut application:
1 @SpringBootApplication
2 public class Application {
3
4 public static void main(String... args) {
5 Micronaut.run(Application.class);
6 // SpringApplication.run(Application.class);
7 }
The only difference between a Spring Boot application and a Micronaut application is shown in line 5. Line 6, commented for comparison, would normally be used for a Spring Boot application.
The command:
./gradlew bootRun
builds and initiates the Micronaut server, where:
curl -X GET http://localhost:8080/greeting?name=Mike
will return:
{"id":1,"content":"Hola, Mike!"}
GraalVM
It is now possible to execute Spring applications on GraalVM that are fully compliant with the Spring annotation-based programming model. The example application can be converted to a GraalVM native image with Micronaut for Spring.
The following two commands starts the process:
./gradlew assemble
java -cp build/libs/greeting-service-all.jar io.micronaut.graal.reflect.GraalClassLoadingAnalyzer
The Micronaut GraalClassLoadingAnalyzer
class is used to analyze the classloading requirements of a Micronaut application.
The GraalVM Java distribution ships with the utility, native-image
, used to build a native image of an application. There are a number of configurable parameters as shown in a partial listing of the call to native-image
:
native-image --no-server \
--allow-incomplete-classpath \
--class-path build/libs/greeting-service-all.jar \
-H:ReflectionConfigurationFiles=build/reflect.json \
-H:EnableURLProtocols=http \
-H:Name=greeting-service \
-H:Class=greeting.example.Application \
A convenient shell script, build-native-image.sh
, is included in the example application to automate the process. The generated native image executable file, greeting-service
, may be invoked to start the server:
./greeting-service
Designed for an instantaneous startup, the native image of the application was initiated in 42ms:
Resources
- Natively Compiling Micronaut Microservices Using GraalVM for Insanely Faster Startups by Jonathan Giles (October 3, 2018)
- The Road to Micronaut 1.0: A JVM-Based Full-Stack Framework by InfoQ (October 19, 2018)
- Micronaut 1.0 GA Released by Graeme Rocher (October 23, 2018)