GraalVMs native-image ermöglicht es, JVM Bytecode direkt in Maschinencode zu übersetzen. Das daraus resultierende Executable benötigt keine JVM zum Laufen, startet schneller und verbraucht weniger Speicher als eine traditionelle JVM-Anwendung – ein Segen für Cloud Computing, bei dem jeder CPU-Cycle und Megabyte an RAM bezahlt werden muss. Wäre es nicht großartig, wenn unser Lieblingsframework, Spring Boot, GraalVM direkt out of the box unterstützen würde? In diesem Talk zeigt Moritz Halbritter, Spring Boot committer, was mit Spring Boot 3 und Spring Framework 6 alles möglich sein wird und erlaubt auch einen Blick unter die Motorhaube, um zu sehen, wie das alles implementiert ist.
5. Native compilation
Features
● Memory efficiency (MB)
● Startup time (ms)
● Competing with other languages (Golang,
Node.JS)
● Well suited for CLIs, functions, scale to zero
● Container density on your platform
Challenges
● May need additional configuration
● Specific tooling required
● No Java agent support
● Not all JVM vendors support it
6. Building a native image with GraalVM
Process all classes
in a fixed classpath
Using the “native-image" binary
Perform
static analysis
Compile code + data
into a single binary
+
Reachability
Metadata
7. Missing reachability metadata
MyService service = //…
Method destroyMethod = findMethodAnnotatedWith(service.getClass(), Destroy.class);
// by default, GraalVM doesn’t ship reflection information,
// destroyMethod will be null
if (destroyMethod != null) {
destroyMethod.invoke(service, context);
}
8. Reachability metadata example
[
{
"name" : "com.example.DefaultMyService",
"methods" : [
{ "name" : "destroy", "parameterTypes" : ["com.example.Context"] }
]
}
//...
]
META-INF/native-image/reflect-config.json
See reflection configuration format
9. How Spring adapts for native
Perform
static analysis
Compile code + data
into a single binary
Infer metadata if
needed
Generate optimized
version of the
application context
Process all classes
in a fixed classpath
Reachability
Metadata
+
Also applies to resources, JDK proxies… See class metadata features
11. Native compilation with Spring
App compiled
Compiled sources
resources
AOT is done
Compiled sources
resources
AOT sources
GraalVM config
Compiled AOT
sources
Native binary
Native application
:generateAotSources
+
:aotClasses
:nativeCompile
Application Jar
AOT optimized app
:bootJar
12. Generating AOT sources
Context refresh
for AOT
Generate source code
+ hints
Running this code will
re-create the same
state at runtime
Detect classpath,
environment, and
main class
Only bean definitions
are created,
no bean instances!
14. Execute your app in AOT mode on the JVM
AOT process is generating Java source files
● Easiest and fastest way to test the AOT-generated code
● Brings performance improvements (more to come)
15. Run your test suite in native mode
AOT process also applies to the test context
● Work in progress
● Reuse your existing test suite
● No need to only rely on functional tests
16. Contribute hints for 3rd party libraries
The new GraalVM reachability metadata repository
● Common hints repository for Java libraries
● Hints pulled by the Native Build Tools plugins
● Spring team already contributing hints
● Open for contribution to all developers
● https://github.com/oracle/graalvm-reachability-metadata
17. Ways to check native support for your app
● Execute in AOT mode on JVM
● Build native-image with the Native Build Tools
● Run existing test suite in native (coming soon)
18. Want to help?
● Contribute to the Graalvm reachability metadata (3rd party lib support)
https://github.com/oracle/graalvm-reachability-metadata
19. JVM efficiency 🚀
Work with GraalVM team 🤗
Unlocking new spaces 💪
Project Leyden 🔥
21. Roadmap
Latest milestones
● Spring Framework 6.0.0-M5
● Spring Boot 3.0.0-M4
GA releases
● Spring Boot 3.0.0: Nov 24th
● Spring Framework 6.0.0: Nov 16th?