background
Since Spring Boot 2.4, support for GraalVM has gradually been available to improve the Boot, memory, and response experience described above. The Spring Native project supports packaging Spring applications as Native images using GraalVM. Native images have faster startup times (<100ms) and lower memory consumption than JVM executables. However, building native images takes more time than jVM-based images.
Environment set up
hardware
It’s a MAC
software
Install the docker
After the use of docker engine compilation image use. How to install? Download and install the docker website directly, very simple!
Boot your application with Spring Native
When booting a project from Spring Initializer (start.spring.io/), you can add Spring Native to your applicationThe generated project will include dependencies on the SpringNative project and the Spring AOT plug-in, which is used to compile application source code into a native executable while improving compatibility and footprint
The build.gradle file is shown below
Plugins {id 'org.springFramework. boot' version '2.6.1' id 'io.spring. Dependency -management' version '1.0.11.RELEASE' id 'Java' id 'org. Springframework. Experimental. Aot' version '0.11.0 - RC1} group =' com. Example 'version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' Configurations {compileOnly {extendsFrom annotationProcessor}} repositories { maven { url 'https://repo.spring.io/milestone' } mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() } bootBuildImage { builder = 'paketobuildpacks/builder:tiny' environment = ['BP_NATIVE_IMAGE': 'true'] }Copy the code
Contrast test
Executable JAR runs
First, open a terminal window, navigate to the root folder of your project, and then run the following command.
$ ./gradlew bootRun
Copy the code
** As can be seen from the figure above, it takes 6.655s to start demo service!! **
Without Spring AOT, applications will start faster than the corresponding version. Let’s see if the application works.
Open your browser http://localhost:8080Copy the code
Since we have not configured any resources, seeing the above screen means that the service is running properly!!
Native image running
Let’s try to build and run a native image using Spring Native and GraalVM.
Building native images using the Spring Boot plug-in is simple. Make sure the Docker engine is running, then execute the following command. Note that this will take a few minutes, largely depending on the CPU and memory characteristics of your laptop.
$ ./gradlew bootBuildImage
Copy the code
Builder Lifecycle ‘Creator’ failed with status code 145 Please increase the docker memory to 16GB, because according to the official website of Spring-Native, native-IMG consumes a lot of memory
If the above command execution is successful, the result will be a docker. IO/library/demo: 0.0.1 – the SNAPSHOT image you can run with docker.
$docker run - name spring native - graalvm - p - 8080:8080 docker. IO/library/demo: 0.0.1 - the SNAPSHOTCopy the code
Applications using Spring Native typically start in less than 100ms, depending on the resources available on your machine.
conclusion
From the comparison test above, we can see that SpringBoot boot is very fast, but the compilation is slow, which is acceptable in the case of large scale deployment distribution cost is much higher than the compilation cost. Also from the service monitoring, we can see that gc and memory are optimized, which is very good. Let’s start deploying our online service via Spring-Native