A new, flexible microservices framework has recently appeared on the scene: Micronaut. Let's take a look at what it is, what it offers, and how to quickly get started.
Join the DZone community and get the full member experience.Join For Free
I have a book on my shelf titled Service-Oriented Architecture by Thomas Erl. This is the fifth printing of the book; printed in June 2006. I've probably had it close to that long.
I purchased the book because I have been interested in the web service architecture for a very long time. At the time, and ever since, it has reminded me of the early architecture applied to Unix applications, small units of code that solved a single problem and could be chained together to address more significant problems.
Except, in the case of web services, it wasn't that simple.
We entered a world of WSDLs and massive XML files. In my opinion, what we had was neither easily maintainable nor quick to write. In recent years, development has moved on to pass data around using JSON instead of XML. Services have become discoverable, instead of forcing a contract in a WSDL. With those changes and other excellent decisions, suddenly it became much easier to write web services that are maintainable and quick to write.
From some of my previous posts, you can get the idea that I promote the idea of developing maintainable code rapidly, so I was pretty excited when I learned that the same group that was responsible for Grails was working on a similar project for web services. Hello, Micronaut.
In this post, I provide an introduction to the Micronaut framework and its features to provide a foundation for you to try it out yourself.
Micronaut is a microservices framework that does so much more than just web services (be still, my geeky little heart).
With the rise of web services and containerized applications, a useful framework can be just what you need in your toolbox. Micronaut promises fast startup times, a reduced memory footprint, minimal use of reflection, minimal use of proxies, and easy unit testing (taken from the user guide on the Micronaut website).
The fast startup partially comes from the fact that, whenever it is able, it processes the annotations at compile time as opposed to run-time. I am hoping this also gives us errors caused by how we've configured annotations faster, at compile time instead of run-time.
To give you an idea, here is a quick introduction to working with the Micronaut command line interface. After installing the framework, you can create a Java web service with this command:
mn create-app <project-name>
This will create a web service application in Java. Using the
-l flag, you can change the language to Groovy or Kotlin. I'm fond of the Groovy language but have never used Kotlin.
mn create-app <project-name> -l=groovy
Setting the language to Groovy also changes the unit test that is created from JUnit in the Java version to Spock. I am hoping in the future there will be an option to choose the test framework separately. Although I would likely always use Spock in Groovy applications, I would like to be able to switch for Java applications.
For me, one of the exciting parts of the Groovy selection is the ability to use GORM. GORM is an Object-Relational Mapper used by Grails. It has always been one of the parts of Grails I've enjoyed. I was able to get the GORM objects I created for a Grails projects copied over to a Micronaut application with minimal changes. This allowed me to get a service set up in front of an existing database in a matter of minutes.
Once an application is created, the
mn command line application is useful for creating objects for the application. Use the following commands in the project directory:
mn create-bean <object-name>
This will create a class that is a Singleton that can be injected into other classes.
mn create-job <object-name>
This will create a class that is annotated with Scheduled, and allow it to be scheduled for repeatable tasks.
mn create-client <object-name>
This command generates a simple client interface to a Micronaut service.
mn create-controller <object-name>
Use this command to create a
This creates the executable portion of the code, for instance, the endpoint. It also creates a test class for the Controller. As a proponent of doing unit testing early, I find this to be extremely helpful.
Since the makers of Micronaut have provided excellent documentation and tutorials, I have tried to introduce you to the framework in hopes to get you interested enough to try it out.
This post scratches the surface of what Micronaut can do. I'll be spending the next few weeks and months digging into the framework and hopefully bring you more insights into what you can accomplish with it. Thanks to OCI for developing a great tool.
Believe in Good Code.
Published at DZone with permission of Rik Scarborough, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.