Learn to build fast and light microservices with Spring Boot and GraalVM
Spring has finally arrived and with it the support for Spring native executables using GraalVM.
You will see that Spring has never started faster or been lighter.
GraalVM uses an exciting set of technologies to compile Ahead Of Time (AoT) programs elegantly and effectively.
This results in incredibly fast start-speed, low memory footprint, and small packaging for your application.
Those characteristics are what you are looking for when building a web app with microservices architecture to be deployed on the cloud. It translates into a more efficient application, that uses fewer resources, and has a cheaper hosting cost. The “Holy Graal” of cloud mindset.
That’s the reason why being able to build native Spring executables is a game changer.
In this article, I will guide you to build your first “Hello GrallVM” REST API using spring-graalvm-native.
Spring makes the process very straightforward with the use of Buildpacks. However, in this guide we will build the executable step-to-step using the native-image-maven-plugin, instead.
First of all, you will have to create a new simple Spring Project with the basic WEB dependency.
Create a new class named “HelloController” and copy this code:
Now it’s time to update your pom.xml.
Make sure your spring-boot-version it’s at least 2.4+.
Then add the spring-graalvm-native dependency.
Add the plugins you see on these images, and don’t forget to add the profile section that will be executed during the packaging phase.
Before building the executable, you will have to make sure to have GraalVM installed on your machine. To do so follow this guide from the official repo.
Run the command “mvn -Pnative clean package” while inside the project root folder and wait for the program to be compiled. This will usually take a bit longer than usual because, to compile AoT, GraalVM needs to analyze your project and select what you will use from the SDK. To know more about how this works I suggest you watch this talk.
Once the compilation process is complete, get ready to be amazed.
Run the executable, located in your target/ folder. In my case I will use the command “target/com.scorsaro.hellograalvmdemo.hellograalvmapp”.
And voilà! The service will be up in a supersonic time 0.06s!!!!
It’s okay to be blown away, I was too the first time I launched it (I kind of still am every time I go back to this after working with normal .jar executables).
Now try to run the .jar version by using the command “mvn spring-boot:run” in your root folder:
In my case the same project launched in 1.3s :O!!! You really see the difference now.
Also, check the size of the file you generated as executables, it weights 64mb versus 176mb of the .jar version. Amazing isn’t it?
Before ending this tutorial be sure to verify your service is working as intended by visiting the address at localhost:8080/hello
Congrats! You just prepared and launched your first spring-native-graallvm executables.
GraalVm with Spring is an amazing duo and a valuable tool to have under your belt. Imagine the potential of it for your cloud web-app and your microservices.
Thanks for reading, here you can find the complete project.