Topic: GraalVM Level: Advanced
Spring on GraalVM Implementation
In this consecutive post (prior post), we shall exploit the theory of the previous post on GraalVM, Native Image, and Spring Native and set up a Spring Boot project running on GraalVM that leverages static analysis and linkage during the build time with ahead-of-time compilation delivering a windows native executable.
Prerequisites
- GraalVM JDK 11 distribution (https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-22.3.0) - ensure you revise the JAVA_HOME and PATH appropriately
- Native Image executable (for windows, https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16)
- Maven build tool
- IntelliJ IDE
- MySQL
- Windows 10 OS
We will commence by downloading a Spring template with Spring Boot >=2.7.5 version running on JDK 11 with the dependencies for GraalVM Native Support, Spring Web, Spring Data JPA, and MySQL Driver as shown below,
Click on the Generate button and then extract the downloaded project template and import it into the IntelliJ IDE.
Up next we will create the following classes with the objective of contacting an endpoint with firstname (ie., http://localhost:8080/v1/petclinic/{firstname}) provided to obtain the details correspondingly from the underlying database.
- Application properties for auto-configuring the Datasource bean on the context startup
- A RestController for exposing the GET endpoint to obtain the user data details
- A JPARepository implementation for performing database CRUD operations
- A domain model for mapping the extracted database resultset
Let's review the pom.xml for mentionable pointers,
The spring-native dependency spawned via the GraalVM Native support is responsible for performing build-time static analysis linking and lazy loading (for more details refer the prior post)
For producing the native executable via the maven build we rely on spring-aot-maven-plugin and the build profile native with native-maven-plugin to be executed as a build goal during the maven package phase
Now we define a rest controller with endpoint v1/petclinic to perform a GET REST HTTP call on the firstname passed as a variable to the URL and return the Owner domain model in JSON by default
With the application code ready, let's compile and create an executable for the windows native platform (for which we would require Virtual Studio Build Tools Utility - C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build>vcvars64.bat).
Then we can run the maven build for the native profile to generate the executable as below,
mvn -f 'C:/Users/Vivek/Downloads/spring-native-demo' -Pnative -DskipTests clean install
It might take a while to complete and assemble a native executable, however, it is worth the wait compensated by the minimal startup time on the executable
On more of how-to details on the native executable refer here
The final project structure with the generated executable artifacts residing in the /target directory as below,
With the prime focus on the *.exe file formats on the /target directory of the project let's run to validate the startup time of the precompiled application executable (in this case spring-native-demo.exe)
This was a instantaneous startup with 0.571 seconds.
Time for cross validating the results, let's hit the exposed REST HTTP endpoint from a browser (http://localhost:8080/v1/petclinic/Jeff) and viola!
We get the JSON domain model response.
Now let's reconcile with the database for the provided firstname Jeff (here lookup for the firstname and it matches with the above JSON REST HTTP GET response)
With this shippable executable, we can deploy it directly onto the platform without requiring to have a JVM, also with the benefit of instantaneous startup, performance, scaling, and garbage collection.
The complete source code can be found on GitHub (https://github.com/raxb/spring-native-demo)
Spring on JDK 11 - Java HotSpot JVM
Let's evaluate the application startup from JDK11 with HotSpot JVM, (Traditional Approach)
Now update the project module setting to use JDK 11,
mvn -f 'C:/Users/Vivek/Downloads/spring-native-demo' spring-boot:run
On running the application with the above maven command let's focus on the startup time of the Spring Boot artifact (spring-native-demo-0.0.1-SNAPSHOT.jar) and the results are 4.463 seconds (compared to native startup 0.571 seconds) giving a boost of ~8X times.
Platform-specific native executables from Graal VMs Native Image with AOT compilation brings evidently a significant gain to application startup and performance, in turn opening up unexplored domains of prospects to be studied and experimented further.
References
- https://lancedom.blogspot.com/2022/10/spring-on-graalvm.html
- https://github.com/raxb/spring-native-demo
- https://www.graalvm.org/22.2/reference-manual/native-image/guides/build-spring-boot-app-into-native-executable/
- https://www.infoq.com/articles/native-java-spring-boot/
- https://www.graalvm.org/22.2/reference-manual/native-image/#install-native-image
- https://github.com/jonashackt/spring-boot-graalvm
- https://docs.spring.io/spring-native/docs/0.12.1/reference/htmlsingle/#getting-started-native-build-tools
- https://medium.com/graalvm/using-graalvm-and-native-image-on-windows-10-9954dc071311
- https://medium.com/trendyol-tech/native-spring-boot-applications-with-graalvm-part-2-build-native-image-performance-results-4badfa16b41b
- https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-22.3.0
Disclaimer:
This is a personal blog. Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated. Any views or opinions are not intended to malign any religion, ethnic group, club, organization, company, or individual. All content provided on this blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
Downloadable Files and ImagesAny downloadable file, including but not limited to pdfs, docs, jpegs, pngs, is provided at the user’s own risk. The owner will not be liable for any losses, injuries, or damages resulting from a corrupted or damaged file.- Comments are welcome. However, the blog owner reserves the right to edit or delete any comments submitted to this blog without notice due to :
- Comments deemed to be spam or questionable spam.
- Comments including profanity.
- Comments containing language or concepts that could be deemed offensive.
- Comments containing hate speech, credible threats, or direct attacks on an individual or group.
Comments
Post a Comment