Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Native Applications - Combining Spring and GraalVM Native Image Technology

Spring Native Applications - Combining Spring and GraalVM Native Image Technology

D65e990ab4df0d0a75c3b0ae5d036bc5?s=128

Martin Lippert

November 05, 2020
Tweet

Transcript

  1. ©2020 VMware, Inc. Spring Native Applications Combining Spring and GraalVM

    native image technology Martin Lippert Spring Tools Lead, MAPBU, VMware November 2020 (based on work by Sébastien Deleuze, Andy Clement, and others)
  2. ©2020 VMware, Inc. https://graalvm.org/: High-performance polyglot VM Basic idea: •

    One virtual machine that can execute all languages (Java, JavaScript, R, Ruby, Python, C, C++, Kotlin, Scala, etc.) • You can run all those languages without any boundaries between them 2 What is GraalVM ?
  3. ©2020 VMware, Inc. 3 GraalVM overview Java Scala Kotlin JavaScript

    Ruby R Python C++ WebAssembly OpenJDK NodeJS Oracle Database MySQL Native GraalVM
  4. ©2020 VMware, Inc. 4 GraalVM overview Java Scala Kotlin JavaScript

    Ruby R Python C++ WebAssembly OpenJDK NodeJS Oracle Database MySQL Native GraalVM 4 GraalVM overview This piece of the project is our focus here
  5. ©2020 VMware, Inc. 5 What is GraalVM Native Image technology?

    static analysis AOT compilation + snapshotting intermediate model Your Java code JDK SubstrateVM Binary Compiled Code Initial Heap
  6. ©2020 VMware, Inc. 6 Two main benefits Reduced memory consumption

    Instant startup
  7. ©2020 VMware, Inc. 7 Two main benefits Reduced memory consumption

    Instant startup Scale to zero You only pay when your application is used Serverless for any kind of workload including regular web applications Good fit with platforms like Knative 3x to 5x memory reduction (RSS) Cheaper cloud instances Critical with systems split into multiple microservices
  8. ©2020 VMware, Inc. 8 Trace-offs 0 20 40 60 80

    100 Startup Memory footprint Latency / Throughput Maturity Build JVM Native
  9. ©2020 VMware, Inc. • Static analysis of your application from

    the main entry point • Configuration required for: • Reflection • Proxies • Resources • Classpath fixed at build time • No class lazy loading • Some code will run at build time 9 Key differences between JVM and Native
  10. ©2020 VMware, Inc. 10 Two main editions GraalVM Community Edition

    GraalVM Enterprise Edition
  11. ©2020 VMware, Inc. GraalVM native is a source of inspiration

    for the JVM ecosystem 11
  12. ©2020 VMware, Inc. An upcoming Java platform standard with Project

    Leyden 12
  13. ©2020 VMware, Inc. Still in “early adopter” mode but matures

    quickly 13
  14. ©2020 VMware, Inc. Spring Native Applications The path towards natively

    compiled Spring applications 14
  15. ©2020 VMware, Inc. Good fit with Spring Boot stand-alone deployment

    model 15
  16. ©2020 VMware, Inc. Compiling your application to a native executable

    requires configuration (reflection/proxy/resources) 16
  17. ©2020 VMware, Inc. The Spring team is doing most of

    the hard work 17
  18. ©2020 VMware, Inc. Our goal is to support compilation of

    existing or new Spring Boot applications to native executables. Unchanged. 18
  19. ©2020 VMware, Inc. “We are excited about the great partnership

    between the Spring and GraalVM engineering teams to support native ahead-of-time compilation for millions of Spring Boot applications. This is a game changer enabling low memory footprint and instant startup for these workloads.” Thomas Wuerthinger, GraalVM founder & project lead 19 Collaboration between the Spring and GraalVM teams
  20. ©2020 VMware, Inc. 20 Leverage the Spring Boot container image

    support > mvn spring-boot:build-image or > gradle bootBuildImage
  21. ©2020 VMware, Inc. 21 Using Paketo Buildpacks…

  22. ©2020 VMware, Inc. 22 … to create lightweight container images

  23. ©2020 VMware, Inc. How to get started 23

  24. ©2020 VMware, Inc. 24

  25. ©2020 VMware, Inc. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.0-RC1</version> <relativePath/> <!-- lookup

    parent from repository --> </parent> 25 Make sure to use Spring Boot 2.4 latest milestone
  26. ©2020 VMware, Inc. @SpringBootApplication(proxyBeanMethods = false) public class DemoApplication {

    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 26 Update configuration to avoid using proxies
  27. ©2020 VMware, Inc. <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <builder>paketobuildpacks/builder:tiny</builder> <env>

    <BP_BOOT_NATIVE_IMAGE>1</BP_BOOT_NATIVE_IMAGE> <BP_BOOT_NATIVE_IMAGE_BUILD_ARGUMENTS> -Dspring.native.remove-yaml-support=true -Dspring.spel.ignore=true </BP_BOOT_NATIVE_IMAGE_BUILD_ARGUMENTS> </env> </image> </configuration> </plugin> 27 Configure Maven plugin
  28. ©2020 VMware, Inc. <dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-graalvm-native</artifactId> <version>0.8.2</version> </dependency> 28 Add

    the spring-graalvm-native dependency
  29. ©2020 VMware, Inc. > mvn spring-boot:build-image Successfully built image 'docker.io/library/demo:0.0.1-

    SNAPSHOT' Total time: 60 s 29 Build the native application • No need for a GraalVM native local install • Build happens in a container • And produces a container
  30. ©2020 VMware, Inc. > docker run -p 8080:8080 docker.io/library/demo:0.0.1-SNAPSHOT .

    ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: Started application in 0.05 seconds (JVM running for 0.009) 30 Run the native application
  31. ©2020 VMware, Inc. You can also build a native executable

    without using containers 31
  32. ©2020 VMware, Inc. <plugin> <groupId>org.graalvm.nativeimage</groupId> <artifactId>native-image-maven-plugin</artifactId> <version>20.2.0</version> <configuration> <mainClass>com.example.demo.DemoApplication</mainClass> <buildArgs>-Dspring.native.remove-yaml-support=true

    -Dspring.spel.ignore=true</buildArgs> </configuration> <executions> <execution> <goals> <goal>native-image</goal> </goals> <phase>package</phase> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> 32 Configure Maven plugin in a native profile
  33. ©2020 VMware, Inc. > mvn -Pnative clean package Total time:

    60 s 33 Build the native application • Need for a GraalVM native local install • Build happens directly on your host (Linux, Mac and Windows are supported) • Produces a native executable • No cross compilation
  34. ©2020 VMware, Inc. > target/com.example.demo.demoapplication . ____ _ __ _

    _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: Started application in 0.05 seconds (JVM running for 0.009) 34 Run the native application
  35. ©2020 VMware, Inc. 35 Petclinic Sample On the JDK Native

    application petclinic- jdbc Build: Memory(RSS): Startup time: 9s 417M 2.6s Build: Memory(RSS): Startup time: 194s 101M 0.158s +2050% -75% -94%
  36. ©2020 VMware, Inc. 36 More progress being made… Sample SpringOne

    2019 SpringOne 2020 commandlinerunner Build: Exec. size: Memory(RSS): 90s 48M 29M Build: Exec. size: Memory(RSS): 50s 22M 24M -45% -55% -17% webflux-netty Build: Exec. size: Memory(RSS): 193s 81M 95M Build: Exec. size: Memory(RSS): 79s 43M 47M -60% -47% -50% webmvc-tomcat Build: Exec size: Memory(RSS): 203s 105M 70M Build: Exec. size: Memory(RSS): 67s 42M 51M -67% -60% -27%
  37. ©2020 VMware, Inc. You can specify additional native configuration 37

  38. ©2020 VMware, Inc. • Create a META-INF/native-image/resource-config.json file • Wildcard

    patterns can be used • Similar config for reflection and proxies • See GraalVM documentation for more details 38 For example additional resources { "resources": [ { "pattern": "myresource.*" } ], "bundles": [ { "name": "your.pkg.Bundle" } ] }
  39. ©2020 VMware, Inc. • An agent can assist in generating

    these files, available with GraalVM java -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=META-INF/native-image Demo • Agent has improved greatly during 2020, thanks to the GraalVM team • Run application with agent attached to produce .json files • Exercise all code paths (manually or via tests) - producing (merging) many of these collected configurations 39 Tracing agent to collect necessary information
  40. ©2020 VMware, Inc. Where are we? 40

  41. ©2020 VMware, Inc. 41 Timeline GraalVM fixes and improvements We

    are here
  42. ©2020 VMware, Inc. 42 Timeline GraalVM fixes and improvements Incubating

    support in spring-graalvm- native We are here
  43. ©2020 VMware, Inc. 43 Incubator for GraalVM native support in

    Spring https://github.com/spring-projects-experimental/spring-graalvm-native
  44. ©2020 VMware, Inc. • Incubating Spring Boot native application support

    • It includes a plugin for the GraalVM native image builder • Analyses the Spring Boot application at build time • Computes the most optimal native image configuration • Challenge is doing that with static analysis • Also perform some build time transformation for: • Optimized footprint • Compatibility 44 spring-graalvm-native
  45. ©2020 VMware, Inc. • GraalVM 20.2 baseline • Use Spring

    Boot 2.4.0 latest milestone (2.4.0-RC1) • Significant footprint improvements • Wider range of supported technology • New experimental functional mode • Spring Boot lightweight container integration • Maven • Gradle 45 We have just released 0.8.2
  46. ©2020 VMware, Inc. • Actuator • Cache • Data JPA,

    MongoDB, R2DBC, Redis • JDBC • Logging (Logback) • Thymeleaf • Validation • Web (Spring MVC with Tomcat) • Webflux (Netty) • Wavefront • Spring Cloud Function 46 Starters supported in 0.8.x
  47. ©2020 VMware, Inc. 47 Reference documentation available

  48. ©2020 VMware, Inc. 48 Timeline GraalVM fixes and improvements Spring

    Boot support Incubating support in spring-graalvm- native We are here
  49. ©2020 VMware, Inc. The road ahead 49

  50. ©2020 VMware, Inc. • First release with beta support •

    Spring Boot 2.4.0 and GraalVM 20.3 baseline • Subset of starters supported • Breaking change will happen (with upgrade paths) • Wider support • Spring Security • Spring Batch • Development-oriented container image 50 spring-graalvm-native 0.9.0 (December 2020)
  51. ©2020 VMware, Inc. More starters supported in upcoming 0.9.x releases

    51
  52. ©2020 VMware, Inc. Spring Boot 3, based on Spring Framework

    6, is expected to provide first-class support for native application deployment, as well as an optimized footprint on the JVM. 52
  53. ©2020 VMware, Inc. Thank You Martin Lippert @martinlippert lippertm@vmware.com